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?