Multiple Open Graph variants
A route needs light and dark social-card images selected by id.
Next.js 16+ · Function
Generates multiple image variants for one route segment, such as alternate icons or social images. Each returned variant requires an id that the image-generation function receives.
Export generateImageMetadata from an image special file to describe multiple image variants for one route segment. Each returned item supplies a required id plus optional accessibility text, dimensions, and content type; the default image function receives that id as a promise.
export function generateImageMetadata({ params }: { params: { slug: string } }) { return [{ id: '1' }] }| Name | Type | Description |
|---|---|---|
params | object | Optional dynamic route parameters from the root segment through the segment containing generateImageMetadata. |
An array of image metadata objects. Every item requires an id; supported metadata includes alt, size, and contentType.
import { ImageResponse } from 'next/og'
type Props = {
params: Promise<{ slug: string }>
id: Promise<string>
}
export async function generateImageMetadata() {
return [
{ id: 'light', alt: 'Light theme', size: { width: 1200, height: 630 }, contentType: 'image/png' },
{ id: 'dark', alt: 'Dark theme', size: { width: 1200, height: 630 }, contentType: 'image/png' },
]
}
export default async function Image({ id }: Props) {
const imageId = await id
return new ImageResponse(
<div style={{ background: imageId === 'dark' ? '#111' : '#fff', color: imageId === 'dark' ? '#fff' : '#111', padding: 40 }}>
{imageId} theme
</div>,
)
}Returns two image variants; Next.js passes the selected metadata item's id to the image generator as a promise.
A route needs light and dark social-card images selected by id.
An application publishes several icon sizes or representations from one route segment.
Image metadata depends on dynamic route parameters.
It returns an array of image metadata objects, allowing one route segment to expose multiple image variants. Each object requires an id, while alt, size, and contentType describe the variant.
Follow-up: What reaches the image generator? The selected item's id is provided as a promise, so the generator should await it before choosing the output.
The function receives an optional params object containing dynamic route parameters from the root segment down to the file's segment. In the documented Next.js 16 behavior, the default image generator—not generateImageMetadata—receives params asynchronously.
Follow-up: How should a Next.js 16 image generator read route data? Await its params promise before using the route parameters.
Place the export in an image Metadata API special file such as an Open Graph, Twitter, icon, or Apple icon file. It is useful when a route needs multiple generated images instead of one hard-coded metadata value.
Follow-up: Why use it for icons? It can produce different icon versions while keeping the metadata values associated with each generated image.
ImageResponseopengraph-imagetwitter-imageiconapple-icondynamic route parameters