P PasteCode
Prompt

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
.md .json Difficulty: Medium Updated Jun 8, 2026

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

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.readFileSync to load the .ttf file at build time — this is fine in an Astro static build.
  • The generated PNGs will be in the dist/og/ folder after astro 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.ts and call NextResponse with 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 build generates a .png file under dist/og/ for each blog post.
  • Each PNG is 800 x 420 pixels.
  • The blog post layout includes og:image pointing to the correct /og/<slug>.png path.
  • Opening the OG image URL in a browser shows a styled card with the post title.

Test Commands

Terminal window
bun add satori sharp
bun run build
ls 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-type

Common 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:image to a relative path like ./og/slug.png — it must be an absolute URL in production.
  • Using canvas package which requires native bindings incompatible with many CI environments.

Fix Prompt

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.