GenerateMetadata lets you create page titles and descriptions that change based on the page content. This helps your website show the right info in search results and browser tabs.
You want each blog post to have its own title and description.
You have a product page that shows different product names and details.
You want to update metadata based on user input or URL parameters.
You want better SEO by customizing metadata for each page dynamically.
Syntax
NextJS
exportasyncfunction generateMetadata({ params }) {
// fetch or compute data based on paramsreturn {
title: `Title for ${params.id}`,
description: `Description for ${params.id}`
};
}
The function must be async if you fetch data.
Return an object with metadata fields like title and description.
Examples
Static metadata without parameters.
NextJS
exportasyncfunction generateMetadata() {
return {
title: 'Home Page',
description: 'Welcome to the home page'
};
}
This example shows a page component that uses generateMetadata to set the page title and description based on the URL parameter slug. The page content also uses the slug to display the post.
NextJS
import React from'react';
exportasyncfunction generateMetadata({ params }) {
const post = {
title: `Post about ${params.slug}`,
description: `This is a detailed post about ${params.slug}.`
};
return post;
}
exportdefaultfunction PostPage({ params }) {
return (
<main>
<h1>{`Post: ${params.slug}`}</h1>
<p>{`Content for post ${params.slug} goes here.`}</p>
</main>
);
}
OutputSuccess
Important Notes
Always return an object with metadata fields from generateMetadata.
Use params to access dynamic route parts.
Metadata updates help search engines and improve user experience.
Summary
GenerateMetadata creates dynamic page titles and descriptions.
It runs on the server before the page renders.
Use it to improve SEO and show relevant info per page.
Practice
(1/5)
1. What is the main purpose of the generateMetadata function in Next.js?
easy
A. To fetch data on the client after page load
B. To handle client-side routing between pages
C. To create dynamic page titles and descriptions before rendering
D. To style components dynamically using CSS
Solution
Step 1: Understand the role of generateMetadata
This function runs on the server before the page renders to set metadata dynamically.
Step 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 C
Quick Check:
Dynamic metadata = To create dynamic page titles and descriptions before rendering [OK]
Hint: Remember: generateMetadata sets SEO info before page loads [OK]
Common Mistakes:
Confusing generateMetadata with client-side data fetching
Thinking it handles routing or styling
Assuming it runs on the client
2. Which of the following is the correct syntax to export a generateMetadata function in a Next.js page?
C. The return value must be a string, not an object
D. The console.log runs after return and is unreachable
Solution
Step 1: Analyze function flow
The return statement ends the function immediately, so code after it won't run.
Step 2: Identify unreachable code
The console.log after return is unreachable and will never execute.
Final Answer:
The console.log runs after return and is unreachable -> Option D
Quick Check:
Code after return is unreachable = The console.log runs after return and is unreachable [OK]
Hint: Code after return is ignored, place logs before return [OK]
Common Mistakes:
Thinking function must be async always
Expecting return to accept strings only
Confusing export types
5. You want to create dynamic metadata for a blog post page that shows the post title and author. Which generateMetadata implementation correctly handles this using params and fetch to get post data asynchronously?
hard
A. 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}` };
}
B. export function generateMetadata({ params }) {
const post = fetch(`/api/posts/${params.id}`).then(res => res.json());
return { title: post.title, description: `By ${post.author}` };
}
C. export async function generateMetadata() {
const post = await fetch('/api/posts').then(res => res.json());
return { title: post.title, description: post.author };
}
D. export async function generateMetadata({ params }) {
return { title: params.title, description: params.author };
}
Solution
Step 1: Identify async data fetching
To get post data dynamically, generateMetadata must 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 with params.id and 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 A
Quick 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]
Hint: Use async/await with params to fetch dynamic metadata [OK]
Common Mistakes:
Not awaiting fetch causing unresolved promises
Ignoring params and fetching all posts
Returning params directly without fetching data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.