0 day streak
javascript / intermediate
Snippet

Dynamic Metadata Generation

The generateMetadata function allows you to create SEO-friendly tags dynamically based on route parameters or fetched data.

snippet.js
javascript
1
2
3
4
5
6
7
export async function generateMetadata({ params }) {
const product = await getProduct(params.id);
return {
title: product.name,
description: `Buy ${product.name} at the best price.`
};
}
nextjs
Breakdown
1
export async function generateMetadata
Defines an asynchronous function that Next.js uses to resolve page metadata.
2
const product = await getProduct(params.id)
Fetches dynamic data using the ID provided in the URL parameters.