October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

OpenGraph, Twitter Cards & Dynamic Social Previews in Next.js 16

Use Next.js 16 App Router metadata and image conventions to create route-specific Open Graph and Twitter previews, with guidance on inheritance, alt text, and caching.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Next.js 16’s App Router, set route-specific social titles and descriptions with metadata or generateMetadata, and provide preview artwork either as image files or generated image routes. For generated images, await the route’s params promise before using its values. These patterns let shared links reflect the page being shared, while the social platforms themselves control how previews ultimately appear.

Choose how each route gets its metadata

Next.js supports metadata in Server Components through either a static metadata object or a route-aware generateMetadata function. The documentation states: “The metadata object and generateMetadata function exports are only supported in Server Components.” See the Next.js metadata documentation.

As an Amazon Associate I earn from qualifying purchases.

  • Use a static metadata export when a route’s title, description, and social details do not depend on route data.
  • Use generateMetadata when values depend on parameters, external data, or metadata supplied by a parent route. It can also accept parent metadata so you can compose route-specific values with shared ones.
  • Use file-based metadata conventions when the preview artwork is best represented by a file placed in the relevant App Router segment.

Next.js resolves metadata while rendering. If a route can be prerendered and metadata does not introduce dynamic behavior, the resulting metadata is included in the initial HTML. This is useful for preview consumers that read the page’s HTML, but does not guarantee that a social platform will display the preview exactly as authored.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set Open Graph and Twitter Card fields

Open Graph metadata can describe the page title, description, URL, site name, locale, and preview images, including image dimensions and alt text. Twitter Card metadata can specify the card type, title, description, and images. The Next.js metadata reference shows a Twitter image with an absolute URL; use a publicly reachable image URL when supplying an image explicitly.

A static route can export both sets of fields. For example, place this in a Server Component page when the values are fixed:

import type { Metadata } from 'next'

export const metadata: Metadata = {
  title: 'Product name',
  description: 'A concise description of this product.',
  openGraph: {
    title: 'Product name',
    description: 'A concise description of this product.',
    images: ['https://example.com/product-preview.jpg'],
  },
  twitter: {
    card: 'summary_large_image',
    title: 'Product name',
    description: 'A concise description of this product.',
    images: ['https://example.com/product-preview.jpg'],
  },
}

Replace the example values and URL with information and an image that belong to your site. For route-dependent values, return equivalent metadata from generateMetadata after loading the route data.

Prevent nested routes from losing Open Graph fields

Metadata can inherit from a parent route, but nested objects are not always merged field by field. In particular, when a child defines its own openGraph object, that object replaces the parent’s entire openGraph object. A child that sets only a route-specific title can therefore lose parent Open Graph image or site-wide fields. The behavior is documented in the metadata inheritance reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Compose shared values explicitly in each child’s metadata. A common object makes the intent clear:

const sharedOpenGraph = {
  siteName: 'Example Site',
  images: ['https://example.com/default-preview.jpg'],
}

export const metadata = {
  openGraph: {
    ...sharedOpenGraph,
    title: 'Route-specific title',
    description: 'Route-specific description.',
  },
}

For data-dependent routes, apply the same composition inside generateMetadata; include every shared field the child needs rather than relying on an implicit nested merge.

Choose static image files or generated previews

Use a static file when the artwork is fixed; generate an image when it needs to incorporate a slug, title, product, article, or other route data. Next.js creates the corresponding head tags for the route from these file conventions. A more deeply placed file applies to a more specific route than a higher-level file.

Approach Use it when Route and metadata behavior
Static image convention The image is fixed for the route or segment. Place opengraph-image.jpg or twitter-image.jpg in the App Router segment. Next.js supplies the associated metadata tags.
Generated image convention The preview should be assembled from route data or other inputs. Implement opengraph-image.tsx or twitter-image.tsx; the route can export image alt text, size, and content type.
Explicit metadata image You want to specify image URLs as part of route metadata. Set openGraph.images and/or twitter.images in the metadata export. This makes image selection explicit in the metadata code.

The supported static image extensions are JPG/JPEG, PNG, and GIF. Keep an Open Graph image file at or below 8 MB and a Twitter image file at or below 5 MB; exceeding the documented limit causes a build failure. These limits and conventions are described in the Next.js Open Graph and Twitter image documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Generate a route-specific image in Next.js 16

In Next.js 16, the generated image function receives params as a promise. Await it before accessing a route segment. For a route such as app/products/[slug]/opengraph-image.tsx, a minimal pattern is:

import { ImageResponse } from 'next/og'

type Props = {
  params: Promise<{ slug: string }>
}

export default async function Image({ params }: Props) {
  const { slug } = await params

  return new ImageResponse(
    <div
      style={{
        display: 'flex',
        width: '100%',
        height: '100%',
        alignItems: 'center',
        justifyContent: 'center',
        background: 'white',
        fontSize: 48,
      }}
    >
      Product: {slug}
    </div>,
  )
}

This example displays the slug, not a polished design or fetched product title. In a real route, load the relevant data and render its meaningful title or other identifying content. The image convention supports generated files named opengraph-image.tsx and twitter-image.tsx; generated routes can also export alt, size, and contentType. Consult the image convention documentation for the supported exports and response details.

If you use generateImageMetadata to create multiple image variants, Next.js 16 also passes the chosen image id to the image function as a promise. Await it before reading the selected variant, as described in the generateImageMetadata reference.

Provide useful image alt text

For convention-based images, a static image can have a companion opengraph-image.alt.txt or twitter-image.alt.txt file. A generated image can export alt. Use text that describes the image’s relevant content, rather than treating alt text as another place to repeat the page title. The available options are documented in the image convention reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand generated-image caching

Generated image routes are statically optimized and cached by default. Dynamic APIs, uncached data, or dynamic route configuration can change that behavior. Decide whether a preview must vary at request time before depending on data that prevents static optimization; for many pages, a build-time image generated from stable route data is simpler. See the Next.js image convention documentation for the conditions affecting caching.

Check the result at the route level

  • Confirm that the target route returns the intended title, description, Open Graph fields, and Twitter Card fields in its rendered HTML.
  • For convention-based images, verify the route-specific image file is in the intended segment and that its extension and file size meet the documented requirements.
  • For generated images, confirm the route parameters and any variant identifier are awaited before use, and that the resulting image contains the expected route content.
  • Review nested metadata for child-level openGraph objects that replace parent fields, then explicitly compose shared values where needed.

These checks establish what Next.js is producing. Social platforms control their own fetching, caching, and presentation, so correct framework metadata alone does not establish exactly when a platform will refresh a preview or how it will render it.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.