Nextjs Fundamentals · Optimization & Metadata

Metadata

`metadata` / `generateMetadata`

Metadata

Next.js lets a route declare its <head> metadata. Export a static metadata object from a layout or page:

import type { Metadata } from 'next';

export const metadata: Metadata = {
  title: 'My Blog',
  description: 'Notes on building for the web.',
};

When the metadata depends on data, export an async generateMetadata function instead. It receives the route's props — including params and searchParams, which are promises in Next.js 16 — and returns a Metadata object:

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { slug } = await params;
  const post = await getPost(slug);
  return { title: post.title, description: post.description };
}

Both exports are supported only in Server Components, and a route uses one or the other, never both. If you fetch the same data in generateMetadata and the page, wrap it in React's cache so the request happens once.

For dynamically rendered pages, Next.js streams metadata: it injects the tags once generateMetadata resolves without blocking the UI.

In short: export metadata for static tags, or generateMetadata for data-driven ones.

You are about to answer a short quiz on metadata / generateMetadata.

  1. The metadata object and generateMetadata are only supported in Server Components.
  2. For data-dependent tags you export an async ___ function.
  3. A route may export both metadata and generateMetadata.
  4. In Next.js 16, generateMetadata receives params as a ___.
  5. Next.js can stream metadata for dynamically rendered pages.