惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
博客园_首页
Vercel News
Vercel News
WordPress大学
WordPress大学
美团技术团队
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Y
Y Combinator Blog
博客园 - 【当耐特】
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
腾讯CDC
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Recent Announcements
Recent Announcements
雷峰网
雷峰网
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
Jina AI
Jina AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
P
Privacy & Cybersecurity Law Blog
Recorded Future
Recorded Future
Help Net Security
Help Net Security
N
News and Events Feed by Topic
博客园 - Franky
P
Proofpoint News Feed
L
LINUX DO - 热门话题
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
D
Docker
Google DeepMind News
Google DeepMind News
有赞技术团队
有赞技术团队
IT之家
IT之家
Security Latest
Security Latest
L
LangChain Blog
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks

Flavio Copes

Workers Cache: a cache in front of your Cloudflare Worker Cloudflare Drop: drag a folder, get a live site Temporary Cloudflare accounts: agents can now deploy without signing up Moondream 3.1 on Workers AI: fast vision at the edge inferencecost.dev: what will AI inference cost you at 10k users? Sitebase: all the features your website needs, in one place StackPlan: figure out where to deploy your app, and what it How the Cloudflare Pages build cache works The Summer of Code New: 90 free tools for developers How I added search to my static site with Pagefind How to rebuild a Cloudflare site on a schedule Cloudflare Email Workers: run code when an email arrives Cloudflare Turnstile: stop bots without annoying CAPTCHAs Cloudflare Workers: secrets and environments Cloudflare Workers observability: logs and traces Cloudflare Analytics Engine: store and query metrics The AI Workshop (July 2026 cohort) Cloudflare Cron Triggers: run a Worker on a schedule Cloudflare Durable Objects: state that lives in one place Cloudflare Queues: run work in the background Cloudflare R2: object storage without egress fees Cloudflare KV: a key-value store for your Workers Cloudflare D1: a SQL database for your Workers Serving a website with Cloudflare Workers static assets Wrangler: the Cloudflare Workers command line tool Executor: one gateway to connect your AI agent to every tool Cloudflare Workers: your first serverless function Vercel eve: an open framework for building AI agents Flue: the open framework for building AI agents Val Town: write and deploy code in seconds A hands-on guide to The Agency, a collection of AI agents The AI Workshop (June 2026 cohort) The AI Workshop (May 2026 cohort) The AI Workshop (Apr 2026 cohort)
How I generate an Open Graph image for every post
Flavio Copes · 2026-07-07 · via Flavio Copes

By Flavio Copes

How I generate a unique Open Graph preview image for every blog post at build time in Astro, using astro-og-canvas, instead of one shared image.

~~~

When you share a link on X, LinkedIn, or in a chat, the preview card you see comes from the page’s Open Graph image.

For years I used a single image for my whole site. Every shared link looked the same.

Now each post gets its own image, with the post title on it, generated automatically at build time. Here’s how.

I use astro-og-canvas. You give it a title and a description, and it draws a card and saves it as a PNG.

It runs during the build, so the images are plain static files. No runtime, no external service.

The image route

Astro can generate files from a route. I created src/pages/og/[...route].ts and pointed it at my posts:

import { OGImageRoute } from 'astro-og-canvas'
import { getCollection } from 'astro:content'

const posts = await getCollection('posts')
const pages = Object.fromEntries(posts.map((post) => [post.id, post.data]))

export const { getStaticPaths, GET } = await OGImageRoute({
  param: 'route',
  pages,
  getImageOptions: (path, page) => ({
    title: page.title,
    description: page.description,
  }),
})

This generates one image per post at /og/<slug>.png.

Notice the await before OGImageRoute. It’s an async function, and if you forget it, Astro can’t find getStaticPaths and the build fails. That one tripped me up.

The last step is telling each page to use its image. In my <head> I set the Open Graph and Twitter tags to the generated URL:

<meta property="og:image" content="https://flaviocopes.com/og/the-slug.png" />
<meta name="twitter:image" content="https://flaviocopes.com/og/the-slug.png" />

Now every post has a unique card. You can see one by opening any /og/<slug>.png directly.

By the way, long titles get cut off on these cards. I made a character limits checker that shows how your title fits in OG cards, search results, and more.

A word on build time

Generating an image for every post adds time to the build. With 1700+ posts, that’s real.

astro-og-canvas caches each image, so unchanged posts aren’t redrawn. But on Cloudflare Pages there’s a catch with where that cache lives, and I cover it in the next post.

~~~

Related posts about astro: