A blog post or product page shared with your generic site logo gets fewer clicks than one that shows its own title. Next.js has two built-in ways to give every route its own image: point the metadata at an image URL, or generate the image in an opengraph-image file next to the page. This guide covers both for the App Router, plus how to check the result the way a crawler sees it.
First, set metadataBase
Crawlers need absolute image URLs. Set metadataBase once in the root layout, and Next.js turns relative image paths in your metadata into absolute ones. Without it, Next.js falls back to a default origin that is often wrong in production.
app/layout.tsx
// app/layout.tsx
import type { Metadata } from "next";
export const metadata: Metadata = {
metadataBase: new URL("https://example.com"),
openGraph: { siteName: "Example" },
twitter: { card: "summary_large_image" },
};
Option 1: generateMetadata with an image URL
If your images already exist (uploaded by an editor, stored in a CMS, or rendered by an image service), return their URL from generateMetadata. In Next.js 16 params is a Promise, so await it before you use it.
Child segments override the openGraph object of their parents rather than merging into it, so repeat any field you need (such as siteName) on pages that set their own openGraph.
To draw the image from the page's own data, add an opengraph-image.tsx file to the route segment. Next.js serves it as an image route and adds the og:image, og:image:width, og:image:height, og:image:type and og:image:alt tags for you. ImageResponse from next/og turns JSX and inline styles into a PNG.
Add a twitter-image.tsx file with the same code if you want a separate image for X; otherwise X uses og:image through its fallback. The image is generated at build time for static routes and cached, and on request for dynamic ones.
Layout rules that catch people out
ImageResponse supports a subset of CSS. Flexbox works and every element with more than one child needs display: flex. CSS grid and most advanced selectors do not.
Styles are inline objects. Class names and Tailwind classes from your app's stylesheet are not applied.
Custom fonts must be loaded as files (TTF, OTF or WOFF) and passed in the fonts option. If your titles use non-Latin scripts, load a font that covers them or the glyphs render as boxes.
Keep the PNG small. Messaging apps tend to drop preview images above roughly 300 KB, so prefer flat colours over large photos. The OG image size guide has the per-platform details.
Test it like a crawler
Your browser is not a good test: it runs JavaScript and caches aggressively. After you deploy, request the page with a crawler user agent and look for the tags in the raw HTML:
Terminal
curl -s -A "Twitterbot/1.0" https://example.com/blog/hello | grep -o '<meta[^>]*og:[^>]*>'
Then open the og:image URL directly to make sure it renders, and run the page through the free OG scanner and preview simulator to see the card on 30 platforms. If a platform still shows an older card, follow the cache refresh guide.
When to hand it off
Generating images in your app is free to start, but every template change means a code change and a redeploy, and every new script or brand colour is yours to maintain. If you would rather design templates visually and get a cached image URL per page, Shareglow's OG image generator renders them on the edge and the Next.js fix page shows the metadata snippet to wire it in.