Route-specific SEO metadata
A product, article, or profile route needs its title and description to reflect the current dynamic segment.
Next.js 16+ · Function
generateMetadata dynamically defines route metadata in a Next.js App Router page or layout. Return a Metadata object, optionally using route data, fetched data, or resolved parent metadata.
Export an asynchronous generateMetadata function from a Server Component page or layout. It can inspect route inputs and the parent segment's resolved metadata, then return a Metadata object; Next.js resolves the result into the document head.
export async function generateMetadata(props: Props, parent: ResolvingMetadata): Promise<Metadata>| Name | Type | Description |
|---|---|---|
props | Props | Route props such as params and searchParams, supplied according to the page or layout context. |
parent | ResolvingMetadata | A promise-like resolved metadata value for the parent route segment, useful when extending inherited metadata. |
A Promise resolving to a Metadata object containing one or more metadata fields.
import type { Metadata, ResolvingMetadata } from 'next'
type Props = {
params: Promise<{ id: string }>
}
export async function generateMetadata(
{ params }: Props,
parent: ResolvingMetadata,
): Promise<Metadata> {
const { id } = await params
const previousImages = (await parent).openGraph?.images || []
return {
title: `Product ${id}`,
openGraph: {
images: ['/products/cover.jpg', ...previousImages],
},
}
}
export default function Page() {
return <h1>Product</h1>
}This server-side route function derives metadata from route parameters and combines a new Open Graph image with metadata inherited from the parent segment.
A product, article, or profile route needs its title and description to reflect the current dynamic segment.
A page loads record data and uses its fields to build title, description, or social preview values.
A nested route needs to add social images without discarding images configured by an ancestor layout.
generateMetadata is the dynamic alternative to a static metadata export. It returns a Metadata object, allowing values to depend on route data or fetched data.
Follow-up: How can a child segment preserve parent Open Graph images? Await the ResolvingMetadata parent argument, read its openGraph.images value, and prepend or append the child images.
The function can receive route props and a ResolvingMetadata parent value. Returning Metadata lets Next.js resolve metadata across layouts and pages and create the corresponding head tags.
Follow-up: Why might a static metadata export be preferable? If values are fixed, a static export avoids unnecessary dynamic computation and clearly expresses build-time metadata.
It is supported only in Server Components and is exported from a layout.js or page.js file. The return value should contain one or more metadata fields.
Follow-up: What changed in v15.2.0? generateMetadata gained streaming support, so metadata can be streamed under the documented runtime behavior.
MetadataResolvingMetadatagenerateStaticParamsmetadatalayout.jspage.js