# Dynamic Metadata

> Fetching database records to populate SEO tags.

Subject: Next.js | Lesson 54 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/dynamic-metadata
Previous: [Static Metadata](https://codewithanimation.in/nextjs/static-metadata.md) | Next: [generateStaticParams](https://codewithanimation.in/nextjs/generate-static-params.md)

## The generateMetadata Function

For dynamic routes like `/product/[id]`, exporting a static `metadata` object doesn't work because you don't know which product the user is requesting yet.

Instead, you export an async `generateMetadata` function. Next.js will pass the URL `params` into this function, allowing you to fetch the specific database record and return a dynamic metadata object.

## Dynamic Social Previews

When someone pastes a link to a specific product on Twitter or iMessage, the crawler bots don't execute JavaScript. They only read the raw HTML `<head>`.

If you try to set the document title dynamically using React Client Components (like `useEffect`), the crawler bots will only see a blank title.

`generateMetadata` ensures the exact product name is injected into the HTML `<head>` on the server before it is sent to the bot.

## Implementation

```tsx
export async function generateMetadata({ params }) {
  const product = await fetchProduct(params.id);
  return { title: `${product.name} | Acme Store` };
}
```

## Common mistake: The Blocking TTFB Trap

Next.js must send the HTML `<head>` before it can send the `<body>`.

This means `generateMetadata` strictly blocks the page from rendering. If your database query inside `generateMetadata` is slow, the user's Time to First Byte (TTFB) is destroyed.

You should ensure that queries inside `generateMetadata` are heavily cached or extremely fast.

## Try it: Dynamic Head Injection

Interact with the product list to see how Next.js executes `generateMetadata` on the server, halts the render to fetch data, and constructs the HTML head dynamically.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/dynamic-metadata

## Check yourself

### Question 1

What is the primary performance risk of using `generateMetadata` with a slow database query?

- A. It causes hydration mismatches in the browser.
- B. It strictly blocks the page render (TTFB), causing the user to stare at a white screen.
- C. It prevents Next.js from caching the route.
- D. It causes the page to render twice on the client.

Correct answer: B

Because SEO tags must be sent in the `<head>`, Next.js cannot begin streaming the page `<body>` until `generateMetadata` finishes its await. Slow queries here directly delay the entire page load.

- Why not A: Metadata does not affect React hydration.
- Why not C: generateMetadata itself does not force dynamic rendering.
- Why not D: It only executes on the server.

## Remember this

- Export `async function generateMetadata()` for dynamic SEO tags.
- It receives the URL `params` just like a page component.
- `generateMetadata` blocks the page render until it finishes.
- React automatically deduplicates `fetch` requests, so you can safely fetch the same product in `generateMetadata` and `page.tsx`.
