What is the Metadata API in the App Router?

The Metadata API defines <head> tags from layouts and pages, either statically with export const metadata: Metadata = {...} or dynamically with export async function generateMetadata(). Next.js merges metadata from the root down to the page and renders the tags for you. File conventions such as icon.png, opengraph-image.tsx, sitemap.ts and robots.ts cover the rest.

Static and dynamic

  • metadata object: for values known in advance.
  • generateMetadata({ params, searchParams }, parent): for values from data; can read and extend the parent metadata. fetch calls are memoized, so the page can request the same data without duplication.
  • Only in Server Components (layouts and pages), not in Client Components.

Common fields

title (string or { default, template, absolute }), description, metadataBase (resolves relative URLs), alternates (canonical, languages), openGraph, twitter, robots, icons, manifest, verification, keywords, authors, other.

Viewport is separate

themeColor, colorScheme and viewport moved to export const viewport or generateViewport in Next.js 14.

Merging rules

Metadata is shallowly merged from root to page. Nested objects such as openGraph are replaced, not deep-merged, so spread shared values from a common object if needed.

File-based metadata

  • favicon.ico, icon.(png|svg), apple-icon.png: multiple formats in one folder emit multiple <link> tags (16.2).
  • opengraph-image.(png|tsx), twitter-image: static files or generated with ImageResponse.
  • sitemap.ts, robots.ts, manifest.ts.

With Cache Components

Uncached data or runtime APIs in generateMetadata follow the same rules as pages: cache it or let it stream.

Code example

tsx
1// app/layout.tsx
2import type { Metadata, Viewport } from "next";
3
4export const metadata: Metadata = {
5 metadataBase: new URL("https://acme.dev"),
6 title: { default: "Acme", template: "%s | Acme" },
7 description: "Tools for modern teams",
8 openGraph: { siteName: "Acme", type: "website", locale: "en_US" },
9 twitter: { card: "summary_large_image", creator: "@acme" },
10 robots: { index: true, follow: true },
11};
12
13export const viewport: Viewport = {
14 themeColor: [
15 { media: "(prefers-color-scheme: light)", color: "#ffffff" },
16 { media: "(prefers-color-scheme: dark)", color: "#0b1020" },
17 ],
18};
19
20// app/pricing/page.tsx
21// export const metadata: Metadata = { title: "Pricing" }; // -> "Pricing | Acme"

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app