export async function generateMetadata({
params,
}: {
params: Promise<{ slug: string }>;
}) {
const { slug } = await params;
const post = await getPost(slug);
return {
title: post.title,
description: post.excerpt,
openGraph: { images: [post.coverUrl] },
};
}
Use the Metadata API in layout.tsx or page.tsx.
There are two ways:
- Static values: export a
metadataobject with fields liketitle,descriptionandopenGraph. - Values from data or params: export an async
generateMetadatafunction that returns the same shape.
Next.js merges metadata from the root layout down to the page. So a layout can define title.template, like "%s | Store", and each page fills in its own title. Next.js renders the final tags into the head.
Both exports work only in Server Components. They replace next/head from the Pages Router.
- Export a static
metadataobject or an asyncgenerateMetadatafunction. - These exports work only in layouts and pages that are Server Components.
- Metadata merges from the root down, and
title.templatesets a title pattern. - The Metadata API replaces
next/head.