web-og-images
Dynamic Open Graph image generation for Next.js using @vercel/og. Covers per-page OG images, reusable templates, Twitter/X and LinkedIn optimization, font loading at the Edge, and caching strategy.
1. When to Use Dynamic vs Static OG Images
Use a static image when:
- The page content never changes (homepage, about, pricing)
- You want zero cold-start latency
- You have a single brand image that works everywhere
Place static images in public/ and reference them directly in metadata:
tsx
// app/layout.tsx
export const metadata = {
openGraph: {
images: [{ url: '/og-default.png', width: 1200, height: 630 }],
},
};
Use a dynamic image when:
- Each page has unique content (blog posts, product pages, user profiles)
- You want the title/description/author rendered into the image
- You need per-page social previews that drive click-through
Dynamic images are generated on demand via an Edge Function route and cached by the CDN after the first request.
Decision matrix:
| Signal | Static | Dynamic | |
|---|
| Same image across all pages | Yes | No | |
| Content changes per URL | No | Yes | |
| Build-time known count < 100 | Maybe static gen | Either | |
| Personalized or user-specific | No | Yes | |
2. Setup
Install the package:
bash
npm install @vercel/og
# or
pnpm add @vercel/og
@vercel/og exports ImageResponse, which accepts JSX and renders it to a PNG using Satori + resvg at the Edge. No Node.js APIs — CSS-subset only (flexbox, no grid).
Edge Runtime requirement: OG routes must declare export const runtime = 'edge'. The default Node.js runtime is not supported by ImageResponse.
Supported CSS:
- Flexbox layout (not Grid)
position: absoluteborder-radius, box-shadowfont-family, font-size, font-weight, colorbackground-color, background-image (linear/radial gradient)object-fit, object-position- No
overflow: hidden on non-flex containers
3. Basic OG Image Route
app/
og/
route.tsx ← handles GET /og
tsx
// app/og/route.tsx
import { ImageResponse } from '@vercel/og';
import type { NextRequest } from 'next/server';
export const runtime = 'edge';
export async function GET(request: NextRequest) {
return new ImageResponse(
(
<div
style={{
display: 'flex',
width: '100%',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
background: 'linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%)',
}}
>
<h1 style={{ color: '#e94560', fontSize: 72, fontWeight: 700 }}>
My Site
</h1>
</div>
),
{
width: 1200,
height: 630,
}
);
}
Visit /og in your browser during development to preview the output immediately.
4. Dynamic OG from URL Params
Pass page-specific data via search params so a single route serves all pages.
tsx
// app/og/route.tsx
import { ImageResponse } from '@vercel/og';
import type { NextRequest } from 'next/server';
export const runtime = 'edge';
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const title = searchParams.get('title') ?? 'Untitled';
const description = searchParams.get('description') ?? '';
const author = searchParams.get('author') ?? '';
// Clamp lengths to prevent overflow
const safeTitle = title.slice(0, 70);
const safeDescription = description.slice(0, 120);
return new ImageResponse(
(
<div
style={{
display: 'flex',
flexDirection: 'column',
width: '100%',
height: '100%',
padding: '60px 80px',
background: '#ffffff',
justifyContent: 'space-between',
}}
>
{/* Top bar */}
<div style={{ display: 'flex', alignItems: 'center' }}>
<span style={{ fontSize: 24, color: '#6366f1', fontWeight: 700 }}>
mysite.com
</span>
</div>
{/* Main content */}
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
<h1
style={{
fontSize: safeTitle.length > 40 ? 52 : 68,
fontWeight: 800,
color: '#111827',
lineHeight: 1.1,
margin: 0,
}}
>
{safeTitle}
</h1>
{safeDescription && (
<p style={{ fontSize: 28, color: '#6b7280', margin: 0, lineHeight: 1.4 }}>
{safeDescription}
</p>
)}
</div>
{/* Bottom bar */}
{author && (
<div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
<span style={{ fontSize: 22, color: '#374151' }}>by {author}</span>
</div>
)}
</div>
),
{ width: 1200, height: 630 }
);
}
Reference from metadata:
tsx
// app/blog/[slug]/page.tsx
import type { Metadata } from 'next';
export async function generateMetadata({ params }: { params: { slug: string } }): Promise<Metadata> {
const post = await getPost(params.slug);
const ogUrl = new URL('/og', process.env.NEXT_PUBLIC_SITE_URL);
ogUrl.searchParams.set('title', post.title);
ogUrl.searchParams.set('description', post.excerpt);
ogUrl.searchParams.set('author', post.author.name);
return {
title: post.title,
openGraph: {
title: post.title,
description: post.excerpt,
images: [{ url: ogUrl.toString(), width: 1200, height: 630 }],
},
twitter: {
card: 'summary_large_image',
images: [ogUrl.toString()],
},
};
}
5. Blog Post OG Template
A richer template with author avatar, date, category badge, and site logo — all composed with flexbox.
tsx
// app/og/blog/route.tsx
import { ImageResponse } from '@vercel/og';
import type { NextRequest } from 'next/server';
export const runtime = 'edge';
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL ?? 'http://localhost:3000';
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const title = (searchParams.get('title') ?? 'Untitled').slice(0, 80);
const author = searchParams.get('author') ?? 'Anonymous';
const date = searchParams.get('date') ?? '';
const category = searchParams.get('category') ?? '';
const avatarUrl = searchParams.get('avatar') ?? '';
// Fetch logo as ArrayBuffer so it works at the Edge
const logoData = await fetch(new URL('/logo.png', SITE_URL)).then((r) =>
r.arrayBuffer()
);
return new ImageResponse(
(
<div
style={{
display: 'flex',
flexDirection: 'column',
width: '100%',
height: '100%',
background: '#0f172a',
padding: '64px 72px',
justifyContent: 'space-between',
}}
>
{/* Header: logo + category */}
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={logoData as unknown as string} width={140} height={40} alt="logo" />
{category && (
<div
style={{
display: 'flex',
background: '#6366f1',
borderRadius: 999,
padding: '6px 20px',
fontSize: 18,
color: '#fff',
fontWeight: 600,
textTransform: 'uppercase',
letterSpacing: 1,
}}
>
{category}
</div>
)}
</div>
{/* Title */}
<h1
style={{
fontSize: title.length > 50 ? 52 : 68,
fontWeight: 800,
color: '#f8fafc',
lineHeight: 1.15,
margin: '0',
maxWidth: '90%',
}}
>
{title}
</h1>
{/* Author row */}
<div style={{ display: 'flex', alignItems: 'center', gap: 20 }}>
{avatarUrl && (
<img
src={avatarUrl}
width={56}
height={56}
style={{ borderRadius: '50%', objectFit: 'cover' }}
alt={author}
/>
)}
<div style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
<span style={{ fontSize: 22, fontWeight: 700, color: '#f1f5f9' }}>
{author}
</span>
{date && (
<span style={{ fontSize: 18, color: '#94a3b8' }}>
{new Date(date).toLocaleDateString('en-US', {
month: 'long',
day: 'numeric',
year: 'numeric',
})}
</span>
)}
</div>
</div>
</div>
),
{ width: 1200, height: 630 }
);
}
Usage URL: /og/blog?title=My+Post&author=Jane&date=2024-01-15&category=Engineering&avatar=https://...
6. Product OG Template
Combines a product photo on the right with metadata on the left — a split layout.
tsx
// app/og/product/route.tsx
import { ImageResponse } from '@vercel/og';
import type { NextRequest } from 'next/server';
export const runtime = 'edge';
function StarRating({ rating }: { rating: number }) {
return (
<div style={{ display: 'flex', gap: 4 }}>
{Array.from({ length: 5 }).map((_, i) => (
<span
key={i}
style={{ fontSize: 28, color: i < Math.round(rating) ? '#f59e0b' : '#d1d5db' }}
>
★
</span>
))}
</div>
);
}
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const name = (searchParams.get('name') ?? 'Product').slice(0, 60);
const price = searchParams.get('price') ?? '';
const rating = parseFloat(searchParams.get('rating') ?? '0');
const reviewCount = searchParams.get('reviews') ?? '';
const imageUrl = searchParams.get('image') ?? '';
const badge = searchParams.get('badge') ?? ''; // e.g. "New" or "Sale"
return new ImageResponse(
(
<div
style={{
display: 'flex',
width: '100%',
height: '100%',
background: '#ffffff',
}}
>
{/* Left: product info */}
<div
style={{
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
padding: '60px 64px',
flex: 1,
gap: 24,
}}
>
{badge && (
<div
style={{
display: 'flex',
alignSelf: 'flex-start',
background: '#ef4444',
color: '#fff',
borderRadius: 8,
padding: '4px 16px',
fontSize: 18,
fontWeight: 700,
textTransform: 'uppercase',
}}
>
{badge}
</div>
)}
<h1 style={{ fontSize: 52, fontWeight: 800, color: '#111827', margin: 0, lineHeight: 1.2 }}>
{name}
</h1>
{rating > 0 && (
<div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
<StarRating rating={rating} />
{reviewCount && (
<span style={{ fontSize: 20, color: '#6b7280' }}>
({reviewCount} reviews)
</span>
)}
</div>
)}
{price && (
<div style={{ display: 'flex', alignItems: 'baseline', gap: 8 }}>
<span style={{ fontSize: 56, fontWeight: 800, color: '#111827' }}>
{price}
</span>
</div>
)}