Prompt to Generate OG Images at Build Time
AI agent prompt to generate per-page Open Graph images at build time in Astro or Next.js using Satori and sharp, with no external service.
CursorClaude CodeCodexWindsurf AstroNext.jsTypeScript
Give this prompt to your agent to generate a unique OG image PNG for every page at build time using Satori and sharp — avoiding the latency and cost of runtime OG image services like Vercel OG.
Main Prompt
You are working in an existing Astro 5 static site with TypeScript.
Task: generate a static Open Graph PNG image for every blog post at build time.
Requirements:- Install `satori` and `sharp`.- Create `src/lib/generate-og.ts` that exports `generateOgImage({ title, description }: OgData): Promise<Buffer>`. - Use Satori to render a JSX template (800 x 420 px) with: site name top-left, post title (large, bold, white), description (smaller, gray), a solid dark background (#0f172a). - Convert the Satori SVG output to PNG using `sharp(Buffer.from(svg)).png().toBuffer()`. - Use only system-safe fonts or load `src/fonts/Inter-Bold.ttf` (create a note to add the file).- Create `src/pages/og/[slug].png.ts` as an Astro endpoint: - `getStaticPaths`: call `getCollection('blog')` and return a path per post. - `GET`: call `generateOgImage` with the post's title and description, set `Content-Type: image/png`, return the buffer.- In the blog post layout, set `<meta property="og:image" content={ogImageUrl} />` where `ogImageUrl` is constructed as `/og/${post.slug}.png`.- Do NOT use `@vercel/og`, `next/og`, or any cloud image generation service.- Do NOT use Canvas or puppeteer.
Stop and list all planned file changes before writing any code.Implementation Notes
- Satori accepts JSX-like objects (React element syntax), but it does NOT use the React runtime.
Pass the element as a plain object tree using
satori’s first argument directly. - Font loading: Satori requires at least one font. Use
fs.readFileSyncto load the.ttffile at build time — this is fine in an Astro static build. - The generated PNGs will be in the
dist/og/folder afterastro build. Check their size; at 800x420 they should be under 100 KB each if using a flat color background. - For Next.js instead of Astro: use a Route Handler at
app/og/[slug]/route.tsand callNextResponsewith the PNG buffer.
Expected File Changes
src/lib/generate-og.ts (new)src/pages/og/[slug].png.ts (new — Astro endpoint)src/layouts/BlogPost.astro (edited — add og:image meta)src/fonts/Inter-Bold.ttf (add manually — not auto-generated)package.json (edited — add satori, sharp)Acceptance Criteria
astro buildgenerates a.pngfile underdist/og/for each blog post.- Each PNG is 800 x 420 pixels.
- The blog post layout includes
og:imagepointing to the correct/og/<slug>.pngpath. - Opening the OG image URL in a browser shows a styled card with the post title.
Test Commands
bun add satori sharpbun run buildls dist/og/file dist/og/my-first-post.png # should output "PNG image data, 800 x 420"curl -I http://localhost:4321/og/my-first-post.png | grep content-typeCommon AI Mistakes
- Importing React and trying to render with
ReactDOMServer— Satori does not use React’s runtime. - Forgetting to load a font, which causes Satori to throw
"No font found for the first character". - Setting
og:imageto a relative path like./og/slug.png— it must be an absolute URL in production. - Using
canvaspackage which requires native bindings incompatible with many CI environments.
Fix Prompt
Satori throws a font error or the PNG is blank. Fix in order:1. Add font loading: `const fontData = fs.readFileSync('src/fonts/Inter-Bold.ttf'); fonts: [{ name: 'Inter', data: fontData, weight: 700 }]` in the satori call.2. Make sure the JSX-like element passed to satori is a plain object, not a JSX expression — call `satori(element, options)` directly.3. For the og:image meta tag, use an absolute URL: `const base = import.meta.env.SITE; ogImageUrl = new URL(\`/og/\${slug}.png\`, base).toString();`Show only the corrected diff.