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

Next.js 16+ · Function

generateMetadata

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.

Since
v13.2.0

How it works

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.

Syntax

export async function generateMetadata(props: Props, parent: ResolvingMetadata): Promise<Metadata>

Parameters

NameTypeDescription
propsPropsRoute props such as params and searchParams, supplied according to the page or layout context.
parentResolvingMetadataA promise-like resolved metadata value for the parent route segment, useful when extending inherited metadata.

Returns

A Promise resolving to a Metadata object containing one or more metadata fields.

Key points

  • Choose generateMetadata when metadata must be computed dynamically rather than declared as a fixed object.
  • The function's result follows Next.js's Metadata shape, including fields such as title, description, and Open Graph data.
  • You provide metadata configuration; Next.js turns the resolved values into document head elements.
  • The API applies at route-segment boundaries through layout and page modules.
  • Dynamic metadata generation belongs on the server, not in client-marked modules.
  • The documented version history records streaming support beginning in Next.js 15.2.0.

Common mistakes

  • Do not export generateMetadata from a Client Component.
  • Use the export in a layout or page file rather than an arbitrary component.
  • Streaming metadata is available from v15.2.0 onward; older assumptions about blocking head generation may be inaccurate.

Example: Dynamic product metadata

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.

When to use it

Route-specific SEO metadata

A product, article, or profile route needs its title and description to reflect the current dynamic segment.

Data-driven metadata

A page loads record data and uses its fields to build title, description, or social preview values.

Inherited Open Graph metadata

A nested route needs to add social images without discarding images configured by an ancestor layout.

Interview questions

QWhat problem does generateMetadata solve?

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.

QWhat inputs can generateMetadata use?

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.

QWhere can generateMetadata be declared?

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.

  • Metadata
  • ResolvingMetadata
  • generateStaticParams
  • metadata
  • layout.js
  • page.js

Source: generateMetadata. Checked 2026-10-15.