Dynamic Open Graph Images in Next.js 16: How We Build opengraph-image.tsx, Check the Satori Version next/og Ships, and Keep Link Previews Correct
Table of Contents
About the Author

Ahmed Mahmoud
Author & Developer
Software engineer passionate about web development and user experience design.
Founder of Devya · eng-ahmed.com ↗Headline: In Next.js 16, an opengraph-image.tsx file in a route segment generates that route's share image with ImageResponse from next/og and writes the og:image tags automatically. The renderer is the Satori copy bundled inside Next.js, not the latest Satori release, so the CSS and font limits a project gets are set by its installed Next.js version.
A link preview is the first thing most people see of an article: the card in Slack, LinkedIn, WhatsApp or X. At Devya our team used to export one PNG per post from a design tool. That stopped scaling once content shipped daily and in two languages, so we moved share cards into code with the opengraph-image file convention. The basic setup was quick. These notes cover what took longer: a WOFF2 font the bundled renderer could not parse, a README that described a newer engine than the one we were running, and a page that kept showing its layout's image.
Key takeaways
• opengraph-image.tsx is a special Route Handler: Next.js serves the image it returns and adds og:image, og:image:width, og:image:height, og:image:type and og:image:alt to the route's head.
• Generated OG images are statically optimized by default: Next.js renders them at build time unless the image function uses a request-time API or uncached data.
• next/og bundles its own Satori. Next.js 16.2.9 ships @vercel/og 0.11.1 with Satori 0.25.0, which lays out with Yoga flexbox and reads TTF, OTF and WOFF fonts, not WOFF2.
• A page's explicit openGraph.images overrides any opengraph-image file, including files inherited from parent segments.
• Set metadataBase in the root layout. Without it, Next.js resolves social image URLs against a fallback origin and can log a warning.
What does opengraph-image.tsx actually generate?
An opengraph-image.tsx file is a Next.js metadata file convention: a default-exported function in a route segment that returns an image Response, which Next.js serves and links from that segment's og:image tags. ImageResponse from next/og is the usual return value; it turns JSX and inline styles into a PNG. A sibling twitter-image.tsx does the same job for the twitter:image tags.
Three optional exports become meta tags: alt becomes og:image:alt, size becomes og:image:width and og:image:height, and contentType becomes og:image:type. In a dynamic segment such as app/blog/[slug], the image function receives params as a Promise, the same as pages in Next.js 15 and 16. Our per-post card awaits params, loads the post by slug, and returns an ImageResponse sized 1200 by 630 with a flex column holding the title and the site name.
Two details in that file came from mistakes. We read the font once at module scope with top-level await and readFile from a path under process.cwd(), the pattern the Next.js docs use, so a render does not go back to the disk. We also pass every title through a small fitTitle() helper that counts code points, truncates past 90 characters, and steps the font size down from 84px to 68px to 56px as titles get longer, because nothing in Satori shrinks text to fit its box.
The alt export is a constant, so every post shares one alt text. For per-post alt text, generateImageMetadata returns a list of image descriptors, each with an id plus its own alt, size and contentType, and the image function then receives that id as a Promise.
Which Satori version does next/og actually run?
next/og runs the copy of Satori vendored inside the installed next package, not the newest Satori release. Satori is Vercel's library that converts JSX and a subset of CSS into SVG; @vercel/og wraps it with Resvg, a Rust SVG renderer, to produce the PNG. Next.js compiles @vercel/og into next/dist/compiled, so adding a newer satori to package.json changes nothing for ImageResponse.
We found this out by reading the Satori README on GitHub. Its main branch describes Taffy for flexbox, grid and block layout, WOFF2 fonts, and HarfBuzz shaping for complex scripts such as Arabic. Our project runs Next.js 16.2.9, and two grep commands against node_modules/next/dist/compiled/@vercel/og showed what it really bundles: the package.json version field and the satori@ version string inside index.node.js.
The answer was @vercel/og 0.11.1 with Satori 0.25.0, which ships yoga.wasm for layout and still throws "Expected div to have explicit display: flex, display: contents, or display: none if it has more than one child node." The Next.js ImageResponse reference matches the bundled version: flexbox and a subset of CSS, no display: grid, and TTF, OTF or WOFF fonts only. We now treat the Next.js docs as the contract and the Satori README as a preview of a later release.
For our bilingual sites this matters most for Arabic. The newer Satori README documents right-to-left line layout, but we do not rely on it through next/og until we have rendered an Arabic title with the bundled version and looked at the PNG.
When should we call Satori directly instead of next/og?
Calling satori with @resvg/resvg-js from a Route Handler makes sense when a project needs a newer engine than Next.js ships. The trade-offs we weigh:
• Satori version: next/og uses whatever the installed Next.js bundles (0.25.0 in 16.2.9); a direct install is pinned and upgraded in the project's own package.json.
• Meta tags: next/og generates them from the file convention and its exports; a direct setup adds openGraph.images by hand in generateMetadata.
• Fonts: next/og reads TTF, OTF or WOFF and bundles Geist Regular as the default; a direct setup reads whatever formats that Satori release parses.
• Caching: next/og images are statically optimized by default; a direct Route Handler sets Cache-Control itself.
• Upgrades: next/og moves when Next.js moves; a direct setup upgrades independently.
Why does a page show the wrong og:image?
A page shows the wrong share image when page-level metadata outranks the image file. The Next.js docs define the order: a page's explicit openGraph.images or twitter.images, from metadata or generateMetadata, overrides the matching opengraph-image or twitter-image file, whether that file sits in the same segment or is inherited from a parent. A page's colocated image file can replace images set by a layout, but only when the page omits images.
The second trap is merging. Next.js merges metadata from layouts and pages shallowly, so a page that sets openGraph with only a title replaces the layout's entire openGraph object, including its description and siteName. Our team keeps shared Open Graph fields in one exported constant and spreads it into each page, the same workaround the docs show.
The third is the origin. Crawlers need an absolute og:image URL, and Next.js builds it from metadataBase. Without metadataBase, social images resolve against a fallback origin, which outside Vercel is http://localhost:3000, and Next.js logs a warning that is easy to scroll past. We set metadataBase once in the root layout from a SITE_URL environment variable, and we set twitter.card to summary_large_image there too. X reads Open Graph tags when the twitter: equivalents are missing, so one opengraph-image file covers both platforms, but we would rather declare the large-image card than assume it.
How do we check link previews before sharing a URL?
The reliable check is to fetch the page HTML, read its og:image, and fetch that URL. First we read the next build output, where each route is marked ○ for static, ● for prerendered with generateStaticParams, or ƒ for rendered on demand. An opengraph-image route marked ƒ renders on every crawler hit, which usually means the image function touched a request-time API or uncached data.
Then a short check-og.mts script runs against the started server. For each path it fetches the HTML, matches the og:image meta tag, decodes & back to &, fetches the image URL, and fails unless the response is OK with an image/ content type. On Node.js 24 it runs as node scripts/check-og.mts with no build step, because Node strips TypeScript types natively.
One property of the check is easy to miss: og:image is absolute and built from metadataBase, so checking a preview deployment fetches the production image unless SITE_URL points at the preview.
Social platforms cache previews, so a fixed image does not repair links that were already shared. Facebook's Sharing Debugger has a Scrape Again button and LinkedIn's Post Inspector refetches a URL on request; other apps refresh on their own schedule. Open Graph images do not change search rankings either. They change how a shared link looks, which is reason enough to test them like any other output.
FAQ
Q: Do we need a separate twitter-image file in Next.js?
A: Usually not. X falls back to Open Graph tags when twitter: tags are missing, so one opengraph-image file covers both; add twitter-image only when X needs a different design, and set twitter.card to summary_large_image for the large layout.
Q: Can a Next.js Open Graph image use display: grid?
A: Not through next/og in Next.js 16.2.9. The bundled Satori 0.25.0 lays out with Yoga flexbox, so grid layouts must be rebuilt with nested flex containers or rendered by calling a newer satori package directly.
Q: Why does a custom font not load in ImageResponse?
A: The usual cause is a WOFF2 file, which the Satori bundled in next/og for Next.js 16.2.9 cannot parse. Convert the font to TTF or OTF, read it with readFile from a path under process.cwd(), and pass one fonts entry per weight.
Q: Are opengraph-image files generated at build time or per request?
A: At build time by default. Next.js statically optimizes generated images unless the image function uses a request-time API or uncached data, in which case the route renders on demand and next build marks it ƒ.
Q: Does an Open Graph image help SEO?
A: Not for rankings. The og:image tag controls the preview card that Slack, LinkedIn, X and messaging apps show for a shared link, which affects whether people click it.
Further Reading
Frontend Engineering
Dark Mode Without the Flash in Next.js 16: How We Ship a Blocking Theme Script, color-scheme, and a Hydration-Safe Toggle
Dark mode that flashes white on reload is a timing bug: the server cannot read localStorage. We explain the blocking theme script we use in Next.js 16, why we keep the theme out of cookies to preserve static rendering, and how we build a toggle that hydrates without mismatches.
Frontend Engineering
sitemap.ts and robots.ts in Next.js 16: How We Control Google and AI Search Crawlers
How we generate robots.txt and sitemap.xml with robots.ts and sitemap.ts in Next.js 16, which AI crawlers we allow for search versus training, the user-agent group rule that silently drops Disallow lines, and how we set lastModified so crawlers trust it.