Creating Share Images for Next.js
Generate dynamic OpenGraph (1200x630) and Twitter (1200x600) images using next/og ImageResponse.
Choosing an Approach
- File-based route (
app/opengraph-image.tsx): Best for static pages with known titles at build time. Export runtime, alt, size, contentType, and a default Image function.
- API route (
app/api/og/route.tsx): Best for dynamic content (blog posts, CMS). Accept slug and/or title as query params. Reference in metadata via generateMetadata().
Use export const runtime = "edge" for both approaches.
File Naming Convention
| File |
Purpose |
Dimensions |
opengraph-image.tsx |
Facebook, LinkedIn, iMessage |
1200x630 |
twitter-image.tsx |
Twitter/X cards |
1200x600 |
app/api/og/route.tsx |
Dynamic API route |
1200x630 |
Place file-based routes in the relevant route directory (e.g., app/about/opengraph-image.tsx for /about).
Layout Pattern
- Use
flexDirection: "column" with justifyContent: "space-between" to separate content from branding
- Title and subtitle (e.g., author name) go top-left in a stacked flex column
- Title: large, bold/medium weight, dark color
- Subtitle: same size or smaller, lighter weight, muted color (e.g.,
#888)
- Keep text left-aligned with
textWrap: "balance" and constrain width with maxWidth
- Use
letterSpacing: "-0.02em" for tight, editorial feel at large sizes
- Padding:
48px on all sides works well at 1200x630
Avatar / Logo (Optional)
If the project has an avatar or logo, place it in the bottom-right corner using a flex container with justifyContent: "flex-end". Load images via fetch + arrayBuffer, convert to base64 data URI for the src. Use borderRadius: "50%" for circular avatars. Cache loaded assets in a Map to avoid refetching.
Custom Fonts
Load .ttf files from public/fonts/ using new URL("../../../public/fonts/YourFont.ttf", import.meta.url). Pass the ArrayBuffer to ImageResponse via the fonts option. Cache the font buffer after first load. Match the weight in the fonts config to the actual font file weight.
Title Case
If titles come from a CMS, apply smart title case:
- Lowercase small words (a, an, the, and, but, for, in, of, etc.) unless first or last
- Always capitalize brand names correctly (WordPress, JavaScript, GitHub, macOS, etc.)
- Uppercase known acronyms (AI, API, CSS, HTML, UI, UX)
- Handle hyphenated words by capitalizing each part independently
Metadata Integration
Reference the OG route in generateMetadata():
export function generateMetadata({ params }) {
return {
openGraph: {
images: [`/api/og?slug=${params.slug}`],
},
};
}
For static pages, pass the title directly: /api/og?title=About.
Satori Rules
These are hard requirements of the next/og rendering engine (Satori):
- Every element needs
display: "flex" — this is the only layout mode
- Inline styles only — no CSS classes, no external stylesheets, no CSS variables
- All text must be in elements with explicit style props
- Use hex colors — no
rgb(), hsl(), or CSS variables
- No
gap on older versions — test before relying on it; fallback to margin
Common Issues
| Issue |
Solution |
| Text not rendering |
Add display: "flex" to the text wrapper |
| Layout broken |
Ensure all containers have display: "flex" |
| Colors wrong |
Use hex colors, not CSS variables |
| Font not loading |
Check the relative path from route file to public/fonts/ |
| Image not showing |
Convert to base64 data URI, don't use relative paths |
Testing
Preview during development by visiting the route directly in the browser:
http://localhost:3000/api/og?title=Hello+World
After building, verify routes register as dynamic (f prefix) in the build output.
1---2name: og-images3description: Guides creation of OpenGraph and Twitter share images using next/og ImageResponse. Covers layout patterns, custom fonts, avatars, title case, and Satori rules. Use when building OG images, Twitter cards, or social previews.4---5
6# Creating Share Images for Next.js
7
8Generate dynamic OpenGraph (1200x630) and Twitter (1200x600) images using `next/og` ImageResponse.
9
10## Choosing an Approach
11
12- **File-based route** (`app/opengraph-image.tsx`): Best for static pages with known titles at build time. Export `runtime`, `alt`, `size`, `contentType`, and a default `Image` function.
13- **API route** (`app/api/og/route.tsx`): Best for dynamic content (blog posts, CMS). Accept `slug` and/or `title` as query params. Reference in metadata via `generateMetadata()`.
14
15Use `export const runtime = "edge"` for both approaches.
16
17## File Naming Convention
18
19| File | Purpose | Dimensions |
20|------|---------|------------|
21| `opengraph-image.tsx` | Facebook, LinkedIn, iMessage | 1200x630 |
22| `twitter-image.tsx` | Twitter/X cards | 1200x600 |
23| `app/api/og/route.tsx` | Dynamic API route | 1200x630 |
24
25Place file-based routes in the relevant route directory (e.g., `app/about/opengraph-image.tsx` for `/about`).
26
27## Layout Pattern
28
29- Use `flexDirection: "column"` with `justifyContent: "space-between"` to separate content from branding
30- Title and subtitle (e.g., author name) go top-left in a stacked flex column
31- Title: large, bold/medium weight, dark color
32- Subtitle: same size or smaller, lighter weight, muted color (e.g., `#888`)
33- Keep text left-aligned with `textWrap: "balance"` and constrain width with `maxWidth`
34- Use `letterSpacing: "-0.02em"` for tight, editorial feel at large sizes
35- Padding: `48px` on all sides works well at 1200x630
36
37## Avatar / Logo (Optional)
38
39If the project has an avatar or logo, place it in the bottom-right corner using a flex container with `justifyContent: "flex-end"`. Load images via `fetch` + `arrayBuffer`, convert to base64 data URI for the `src`. Use `borderRadius: "50%"` for circular avatars. Cache loaded assets in a `Map` to avoid refetching.
40
41## Custom Fonts
42
43Load `.ttf` files from `public/fonts/` using `new URL("../../../public/fonts/YourFont.ttf", import.meta.url)`. Pass the `ArrayBuffer` to `ImageResponse` via the `fonts` option. Cache the font buffer after first load. Match the `weight` in the fonts config to the actual font file weight.
44
45## Title Case
46
47If titles come from a CMS, apply smart title case:
48- Lowercase small words (a, an, the, and, but, for, in, of, etc.) unless first or last
49- Always capitalize brand names correctly (WordPress, JavaScript, GitHub, macOS, etc.)
50- Uppercase known acronyms (AI, API, CSS, HTML, UI, UX)
51- Handle hyphenated words by capitalizing each part independently
52
53## Metadata Integration
54
55Reference the OG route in `generateMetadata()`:
56
57```tsx
58export function generateMetadata({ params }) {
59 return {
60 openGraph: {
61 images: [`/api/og?slug=${params.slug}`],
62 },
63 };
64}
65```
66
67For static pages, pass the title directly: `/api/og?title=About`.
68
69## Satori Rules
70
71These are hard requirements of the `next/og` rendering engine (Satori):
72
731. **Every element needs `display: "flex"`** — this is the only layout mode
742. **Inline styles only** — no CSS classes, no external stylesheets, no CSS variables
753. **All text must be in elements** with explicit style props
764. **Use hex colors** — no `rgb()`, `hsl()`, or CSS variables
775. **No `gap` on older versions** — test before relying on it; fallback to margin
78
79## Common Issues
80
81| Issue | Solution |
82|-------|----------|
83| Text not rendering | Add `display: "flex"` to the text wrapper |
84| Layout broken | Ensure all containers have `display: "flex"` |
85| Colors wrong | Use hex colors, not CSS variables |
86| Font not loading | Check the relative path from route file to `public/fonts/` |
87| Image not showing | Convert to base64 data URI, don't use relative paths |
88
89## Testing
90
91Preview during development by visiting the route directly in the browser:
92
93```
94http://localhost:3000/api/og?title=Hello+World
95```
96
97After building, verify routes register as dynamic (`f` prefix) in the build output.