Next.js Course
Next.js
/
Intermediate

Dynamic Metadata

Definition

Using the `generateMetadata()` function to dynamically fetch data from a database and use it to construct SEO tags for a specific dynamic route.

Explain Like I'm New

If you have a dynamic route like `[productId]`, you can't hardcode the title. You need to look at the ID, fetch the product from the database, and make the title 'Buy the iPhone 15'. `generateMetadata` pauses the server, does exactly that, and then renders the page.

Real World Example

A blog post page where the browser tab title perfectly matches the title of the article fetched from the CMS.

Common Use Cases

  • •E-commerce product pages
  • •Blog posts
  • •User profiles

Interactive Example

Loading Code Playground...

Interview Questions

basic

  • What function do you export to generate metadata based on route parameters?

intermediate

  • If you fetch a product in `generateMetadata`, and then fetch the exact same product again in your `page.tsx` component, does Next.js query the database twice?

Flash Cards

Question

What function?

Click to reveal answer
Answer

`generateMetadata(props)`

Question

Query twice?

Click to reveal answer
Answer

No! Next.js uses 'Fetch Memoization'. It automatically intercepts the second identical `fetch` call and instantly returns the cached result from the first call, ensuring absolutely zero performance penalty.