What if your website could update its SEO info all by itself every time content changes?
Why GenerateMetadata for dynamic metadata in NextJS? - Purpose & Use Cases
Imagine you have a website with hundreds of pages, and you want to update the title and description for each page manually in the HTML head.
Every time you add or change content, you must remember to update these details yourself.
Manually updating metadata is slow and easy to forget.
If you miss updating metadata, search engines and social media previews show wrong or outdated information.
This hurts your site's visibility and user experience.
GenerateMetadata lets Next.js create page metadata automatically based on your page content or data.
This means your titles, descriptions, and other metadata update dynamically without extra manual work.
export default function Page() {
return <><head><title>Static Title</title></head><main>Content</main></>;
}export async function generateMetadata() {
return { title: 'Dynamic Title', description: 'Dynamic description based on data' };
}
export default function Page() {
return <main>Content</main>;
}You can create SEO-friendly pages that always show the right metadata without extra manual updates.
An online store updates product pages daily; GenerateMetadata automatically sets each product's title and description based on current product info.
Manual metadata updates are slow and error-prone.
GenerateMetadata automates metadata creation dynamically.
This improves SEO and user experience effortlessly.
Practice
generateMetadata function in Next.js?Solution
Step 1: Understand the role of
This function runs on the server before the page renders to set metadata dynamically.generateMetadataStep 2: Identify its main use
It is used to create dynamic page titles and descriptions for SEO and relevant info.Final Answer:
To create dynamic page titles and descriptions before rendering -> Option CQuick Check:
Dynamic metadata = To create dynamic page titles and descriptions before rendering [OK]
- Confusing generateMetadata with client-side data fetching
- Thinking it handles routing or styling
- Assuming it runs on the client
generateMetadata function in a Next.js page?Solution
Step 1: Recall correct export syntax
Next.js expectsgenerateMetadataas an exported async function returning an object with metadata.Step 2: Check each option
export async function generateMetadata() { return { title: 'Page' } } correctly exports an async function returning an object with a title property.Final Answer:
export async function generateMetadata() { return { title: 'Page' } } -> Option BQuick Check:
Correct export syntax = export async function generateMetadata() { return { title: 'Page' } } [OK]
- Using default export instead of named export
- Returning JSX instead of an object
- Trying to set document.title inside generateMetadata
generateMetadata function, what will be the page title?export async function generateMetadata({ params }) {
return { title: `User: ${params.username}` };
}And the URL is
/user/jane.Solution
Step 1: Understand parameter usage
The function receivesparamswithusernamefrom the URL segment.Step 2: Substitute
Since URL isparams.usernamewith actual value/user/jane,params.usernameis 'jane', so title becomes 'User: jane'.Final Answer:
User: jane -> Option AQuick Check:
Dynamic title uses params.username = User: jane [OK]
- Using literal string 'params.username' instead of value
- Assuming params is undefined without route setup
- Confusing URL path with metadata title
generateMetadata function?export function generateMetadata() {
return { title: 'Home' }
console.log('Metadata generated')
}Solution
Step 1: Analyze function flow
Thereturnstatement ends the function immediately, so code after it won't run.Step 2: Identify unreachable code
Theconsole.logafterreturnis unreachable and will never execute.Final Answer:
The console.log runs after return and is unreachable -> Option DQuick Check:
Code after return is unreachable = The console.log runs after return and is unreachable [OK]
- Thinking function must be async always
- Expecting return to accept strings only
- Confusing export types
generateMetadata implementation correctly handles this using params and fetch to get post data asynchronously?Solution
Step 1: Identify async data fetching
To get post data dynamically,generateMetadatamust be async and await the fetch call.Step 2: Check usage of params and fetch
export async function generateMetadata({ params }) { const post = await fetch(`/api/posts/${params.id}`).then(res => res.json()); return { title: post.title, description: `By ${post.author}` }; } correctly awaits fetch withparams.idand returns metadata using fetched data.Final Answer:
export async function generateMetadata({ params }) { const post = await fetch(`/api/posts/${params.id}`).then(res => res.json()); return { title: post.title, description: `By ${post.author}` }; } -> Option AQuick Check:
Async fetch with params used correctly = export async function generateMetadata({ params }) { const post = await fetch(`/api/posts/${params.id}`).then(res => res.json()); return { title: post.title, description: `By ${post.author}` }; } [OK]
- Not awaiting fetch causing unresolved promises
- Ignoring params and fetching all posts
- Returning params directly without fetching data
Ready to prove it?
Pass the test and this pattern moves unknown
