Generating Open Graph images in Next.js with ImageResponse
Add an opengraph-image.tsx file to a route, draw the image with JSX and ImageResponse, and Next.js adds the tags. With a per-post example and custom fonts.
By Sohail Hussain21 November 20265 min read
To generate Open Graph images in Next.js, add an opengraph-image.tsx file to a route folder that exports a function returning an ImageResponse from next/og. You draw the image with JSX and inline styles, Next.js renders it to a PNG, and it adds the og:image tags with the right width, height and type to that route's pages automatically. For a blog or product catalogue, one file in the dynamic route folder gives every page its own share image built from its title.
This guide uses the App Router in Next.js 16, current as of October 2026. Every page on this site has a generated image made exactly this way.
Step 1: Do you need generated images at all?
Not always. For a company site with ten fixed pages, one well-designed static image is enough. Save it as app/opengraph-image.png and Next.js uses it for every route that does not have a more specific one. Add opengraph-image.alt.txt next to it for the alt text.
Generated images pay off when there are many pages that will be shared: blog posts, case studies, products, job listings, documentation. Making those by hand does not happen, so the pages end up sharing a generic image. A generated image with the page title on it gets more clicks in a LinkedIn feed or a WhatsApp chat than the same logo on every link.
Share images are part of the search-ready build we include in our website design and development projects. They do not affect rankings, but they decide what a shared link looks like.
What should a good share image show?
Design it for a small thumbnail in a busy feed, not for a 1200-pixel canvas.
- The page title, large. It is the one thing that differs between pages. Use at least 56 pixels on a 1200-pixel canvas so it survives being shown at a third of that width.
- Your name or logo, small. Enough to tell people whose link it is.
- One short label. A category, "Case study" or a product price. Not a paragraph.
- A safe margin. Keep text at least 60 to 70 pixels from every edge, because platforms crop differently.
- Strong contrast. Dark text on a light background, or the reverse. Thin grey text disappears in a thumbnail.
The platforms ask for slightly different shapes, which is why the safe margin matters:
| Platform | Recommended size | Notes |
|---|---|---|
| 1200 × 630 | Recommended for sharp previews on high-resolution screens | |
| 1200 × 627 | Effectively the same ratio as Facebook | |
| X (large card) | 2:1 ratio, under 5 MB | Crops a 1200 × 630 image slightly at top and bottom |
A single 1200 × 630 image works everywhere if nothing important sits near the edges.
Step 2: How do you create the image file?
Put opengraph-image.tsx in the route folder. For per-post images, that is the dynamic segment:
// app/blog/[slug]/opengraph-image.tsx
import { ImageResponse } from "next/og";
import { getPost } from "@/lib/posts";
export const alt = "Acme Engineering blog";
export const size = { width: 1200, height: 630 };
export const contentType = "image/png";
export default async function Image({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params;
const post = await getPost(slug);
return new ImageResponse(
(
<div
style={{
width: "100%",
height: "100%",
display: "flex",
flexDirection: "column",
justifyContent: "space-between",
padding: 72,
background: "#f3f3f1",
color: "#121214",
}}
>
<div style={{ display: "flex", fontSize: 30, fontWeight: 700 }}>Acme Engineering</div>
<div style={{ display: "flex", fontSize: 72, fontWeight: 700, lineHeight: 1.05 }}>
{post?.title ?? "Blog"}
</div>
<div style={{ display: "flex", fontSize: 26, color: "#5c5c63" }}>acme.example</div>
</div>
),
size,
);
}
The three exports become tags: alt sets og:image:alt, size sets the width and height, and contentType sets og:image:type. Since Next.js 16, params is a promise, so it must be awaited. Add a twitter-image.tsx with the same content if you want a different image on X; otherwise the Twitter tags reuse the Open Graph image.
Step 3: What CSS can you use in the image?
Less than in a browser. ImageResponse uses Satori, which converts a subset of HTML and CSS into an SVG and then a PNG. The rules that catch people out:
- Flexbox only.
display: griddoes not work. Absolute positioning does. - Every element with more than one child needs
display: flex. Satori refuses to render adivwith several children and no display value. Setdisplay: "flex"everywhere and the error goes away. - Inline styles only. No class names, no Tailwind, no external stylesheets.
- No automatic text fitting. Long titles overflow. Pick the font size from the title length; this site's template uses 80 pixels for titles of up to 40 characters, 68 up to 70, and 56 beyond that.
- A 500 KB limit on everything bundled into the image, including fonts and images.
The ImageResponse reference links to the full list of supported CSS.
Step 4: How do you use your own font?
Read the font file once at module scope and pass it in the options. Only TTF, OTF and WOFF work; WOFF2, the format most sites serve, does not.
import { readFile } from "node:fs/promises";
import { join } from "node:path";
const interBold = await readFile(join(process.cwd(), "assets/Inter-Bold.ttf"));
// inside Image():
return new ImageResponse(jsx, {
...size,
fonts: [{ name: "Inter", data: interBold, weight: 700, style: "normal" }],
});
Then set fontFamily: "Inter" on the root element. Subset the font to the characters you need if the file is large, because it counts against the 500 KB limit. For Arabic or Devanagari titles, load a font that covers the script, or the text renders as boxes.
Step 5: How do you reuse one design across the site?
Move the JSX into a helper that takes the varying parts. This site has one function in src/lib/og.tsx that takes a label, a title and a footer line, and both the tools pages and the blog call it:
// app/blog/[slug]/opengraph-image.tsx
import { ogImage, ogSize } from "@/lib/og";
import { getPost } from "@/lib/blog";
export const alt = "MSH Technologies blog";
export const size = ogSize;
export const contentType = "image/png";
export default async function Image({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params;
const post = getPost(slug);
return ogImage({ eyebrow: "Blog", title: post?.title ?? "Blog" });
}
One template keeps every share image on brand, and a design change is one edit. The trade-off of this simple setup is one alt text for every post. A per-page alt needs generateImageMetadata, which returns the alt along with an id for each image.
Step 6: How do you test it?
Open the image URL directly: for a post, /blog/my-post/opengraph-image. You should see the PNG. Then view the page source and check the og:image tag. Next.js adds a short hash to the image URL as a query string; that is expected.
Generated images are rendered at build time and cached unless they read request data, so they cost nothing per visit. Finally, paste the page URL into LinkedIn's Post Inspector or Facebook's Sharing Debugger, which also makes them fetch a fresh copy instead of an old cached one. Our Open Graph image size checker confirms the dimensions suit each platform, and the Open Graph generator shows the rest of the tags a complete share card needs.
For the title, description and other tags that go alongside the image, see how to set up metadata and Open Graph in Next.js. For the wider case for this stack, read why we build company websites with Next.js.
Was this guide useful?