Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Next.js 16+ · Function

generateImageMetadata

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.

How it works

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.

Syntax

export function generateImageMetadata({ params }: { params: { slug: string } }) { return [{ id: '1' }] }

Parameters

NameTypeDescription
paramsobjectOptional dynamic route parameters from the root segment through the segment containing generateImageMetadata.

Returns

An array of image metadata objects. Every item requires an id; supported metadata includes alt, size, and contentType.

Key points

  • Use it when a route segment needs several generated image versions or separate images.
  • It lets metadata values be generated rather than hard-coded, which is especially useful for icons.
  • Return an array whose objects describe image metadata such as alternative text and dimensions.
  • The id field is required and identifies the metadata variant.
  • size can specify the generated image dimensions with numeric width and height.

Common mistakes

  • Every returned metadata object must include id; omitting it prevents the generator from identifying the selected variant.

Example: Generate light and dark image variants

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.

When to use it

Multiple Open Graph variants

A route needs light and dark social-card images selected by id.

Generated icon metadata

An application publishes several icon sizes or representations from one route segment.

Route-aware image variants

Image metadata depends on dynamic route parameters.

Interview questions

QWhat does generateImageMetadata return?

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.

QHow are route parameters handled?

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.

QWhen would you use generateImageMetadata?

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.

  • ImageResponse
  • opengraph-image
  • twitter-image
  • icon
  • apple-icon
  • dynamic route parameters

Source: generateImageMetadata. Checked 2026-10-15.