Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 04 / Topic 2 of 8

generateMetadata: a title that depends on data

A plain metadata object can't know a product's name before the page renders. app/store/(shop)/products/[id]/page.tsx already needed generateStaticParams (Part 2) because its content depends on params; its metadata has the exact same dependency, so it exports an async generateMetadata function instead of a static object:

export async function generateMetadata({ params }: PageProps<"/store/products/[id]">): Promise<Metadata> {
  const { id } = await params;
  const product = findProduct(id);

  if (!product) {
    return { title: "Product not found" };
  }

  return {
    title: product.name,
    description: `${product.name} — ₹${product.price.toLocaleString("en-IN")}, made in ${product.city}.`,
  };
}

For a known, prerendered id, this works exactly as expected, with or without a crawler's user agent:

$ curl -s http://localhost:PORT/store/products/p1 | grep -o '<title>[^<]*</title>'
<title>Jaipur Blue Pottery Vase | Apna Bazaar</title>
$ curl -s -A "Slackbot-LinkExpanding 1.0" http://localhost:PORT/store/products/p1 | grep -o '<title>[^<]*</title>'
<title>Jaipur Blue Pottery Vase | Apna Bazaar</title>

A genuinely surprising result, tested against both next dev and a real next build && next start, for an id that is not in generateStaticParams (/store/products/p999, the same id Part 2 used to demonstrate notFound()):

$ curl -s http://localhost:PORT/store/products/p999 | grep -o '<title>[^<]*</title>'
<title>Apna Bazaar | Next.js Step by Step</title>
$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:PORT/store/products/p999
404

The <head> still shows the store layout's inherited default title, not generateMetadata's own "Product not found" branch — even though the literal string "Product not found" is present somewhere in that same response's raw bytes (confirmed with grep -c "Product not found", three matches). The installed package's own metadata guide explains the mechanism this is brushing up against: "For dynamically rendered pages, Next.js streams metadata separately, injecting it into the HTML once generateMetadata resolves, without blocking UI rendering" — and that streaming is what a plain curl, which never runs the client-side JavaScript that applies a streamed patch, cannot observe landing. For every id this harness actually prerenders (p1, p2, p3), none of this applies — the metadata is already baked into the static HTML at build time, which is why the curl/bot-UA test two paragraphs up came back correct both times. Treat "a prerendered id's metadata is reliable, an unlisted id's <head> metadata may lag behind what generateMetadata actually returned" as the honest, tested takeaway, not a reason to avoid generateMetadata — Apna Bazaar's catalog is small enough that every real product is prerendered.

Full file: examples/06-product-generate-metadata.tsx.

Done here? The practice set for Part 4 — including the solutions — is on the part page.

Search the guide