Dynamic OG Images in Next.js Without Satori Headaches
Compare approaches for building an OG image API in Next.js, from @vercel/og to a screenshot API, with working code examples and tradeoffs.
Every Next.js app with social sharing eventually hits the same wall: you need an OG image that reflects actual page content, not a static PNG sitting in /public. A blog post needs its title. A product page needs its price and image. A user profile needs their name and avatar. Static images don't scale for this.
There are really only two ways to generate OG images dynamically for Next.js: render JSX into an image at the edge, or screenshot a real rendered page. Both work. They solve different problems and fail in different ways.
Option 1: @vercel/og and the Satori Approach
Next.js ships with built in support for generating OG images using ImageResponse, which runs on Satori under the hood. You write JSX, it converts it to SVG, then to PNG, all at the edge.
// app/api/og/route.tsx
import { ImageResponse } from 'next/og'
export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const title = searchParams.get('title') || 'Default Title'
return new ImageResponse(
(
<div style={{
fontSize: 60,
background: '#000',
color: '#fff',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}>
{title}
</div>
),
{ width: 1200, height: 630 }
)
}
This works well for simple text and gradient cards. But it has real limits:
- No CSS Grid. Satori only supports a subset of flexbox. Nested grids, absolute positioning quirks, and some flex properties don't render correctly.
- Font loading is manual. You have to fetch and pass font buffers yourself, including for every weight and style you use.
- No real browser rendering. Custom fonts with ligatures, complex SVG icons, backdrop filters, and box shadows often look different than in an actual browser.
- It can't reuse your actual components. You end up maintaining a second, simplified version of your UI just for OG images instead of reusing the real page.
When Satori Is the Right Choice
If your OG images are simple: a title, a background color, maybe a logo and author name, Satori is fast, free, and runs at the edge with no extra infrastructure. For a blog with a consistent card template, it's often enough.
Option 2: Screenshot the Real Page
The alternative is to build an actual OG template as a normal Next.js page, then screenshot that page with a headless browser. This flips the problem: instead of recreating your design in a constrained JSX-to-SVG renderer, you just render your real page in real Chrome and capture it.
Step 1: Build an OG Template Route
Create a route specifically for OG rendering, separate from your public pages, so it stays lightweight and has no nav, footer, or client-side JS that could slow rendering:
// app/og-template/[slug]/page.tsx
export default async function OgTemplate({ params }) {
const post = await getPost(params.slug)
return (
<div style={{ width: 1200, height: 630, display: 'flex', ... }}>
<h1>{post.title}</h1>
<p>{post.author}</p>
<img src={post.coverImage} />
</div>
)
}
Because this is a normal Next.js page, you get full CSS support: grid, shadows, filters, web fonts, whatever you need. No conversion layer, no missing properties.
Step 2: Screenshot It With an API Call
Instead of standing up Puppeteer or Playwright yourself (and dealing with Chromium binaries, memory leaks, and cold starts on serverless), point a screenshot API at the route. With pxshot, it's one GET request:
https://api.pxshot.dev/screenshot
?url=https://yourapp.com/og-template/my-post-slug
&width=1200
&height=630
&format=png
Drop that URL directly into your og:image meta tag, or pre-generate it and cache the result. Because pxshot renders in actual Chrome, anything that works in a browser (web fonts, SVGs, gradients, background images, CSS animations frozen on first paint) works exactly as designed.
Step 3: Wire It Into Your Metadata
// app/blog/[slug]/page.tsx
export async function generateMetadata({ params }) {
const post = await getPost(params.slug)
const ogImageUrl = `https://api.pxshot.dev/screenshot?url=${encodeURIComponent(
`https://yourapp.com/og-template/${params.slug}`
)}&width=1200&height=630`
return {
openGraph: {
images: [{ url: ogImageUrl, width: 1200, height: 630 }],
},
}
}
Comparing the Two Approaches
- Design flexibility: Screenshot approach wins. Full CSS, no Satori limitations.
- Speed: Satori is faster for very simple cards since it skips browser boot entirely. For anything with images or custom fonts, the gap narrows since those add load time to both.
- Maintenance: Screenshot approach reuses a real page you can preview in a normal browser tab. Satori templates can only be previewed by actually hitting the generated image.
- Infrastructure: Self-hosting Puppeteer adds real ops burden (memory limits, Chromium updates, serverless timeouts). A screenshot API removes that entirely.
Caching Your OG Images Properly
Whichever method you pick, don't regenerate the image on every request. Set cache headers so crawlers and CDNs don't hammer your render endpoint:
- Generate the image once at build time or on first request.
- Store the resulting URL or file in your database or object storage.
- Set
Cache-Control: public, max-age=31536000, immutableif the content never changes, or a shorter TTL plus a version query param if it does. - Invalidate by changing the slug or a version param in the URL rather than trying to purge caches manually.
A Hybrid Pattern That Works Well
Many teams end up mixing both: Satori for simple, high-volume pages (every blog post gets the same template with swapped text), and real page screenshots for high-value pages where design fidelity actually matters, like pricing pages, product launches, or anything with a custom illustration or chart. You don't have to pick one approach for the entire app.
If you're already using pxshot for visual regression testing or PDF generation elsewhere in your stack, reusing it for OG images means one less service to configure and one less API key to manage.
Try it without signing up, there's a live demo on the pxshot.dev homepage where you can paste a URL and see the rendered output immediately. The free plan includes 50 screenshots a month, enough to test OG image generation across a real project before committing to anything.