What is the purpose of generateMetadata in Next.js?
It is a special function used to create dynamic metadata like page titles and descriptions based on data or route parameters.
Click to reveal answer
intermediate
How does generateMetadata improve SEO in Next.js apps?
By generating metadata dynamically, it helps search engines see relevant titles and descriptions for each page, improving search ranking and user experience.
Click to reveal answer
beginner
What type of data can generateMetadata use to create metadata?
It can use route parameters, fetched data, or any async data to customize metadata for each page.
Click to reveal answer
beginner
True or False: generateMetadata runs on the client side in Next.js.
False. It runs on the server side during rendering to provide metadata before the page loads.
Click to reveal answer
intermediate
What is the return type of generateMetadata in Next.js?
It returns an object containing metadata fields like title, description, and other SEO-related properties.
Click to reveal answer
Where does generateMetadata run in a Next.js app?
AOn the server during rendering
BIn the browser after page load
COnly during client-side navigation
DIn a separate metadata service
✗ Incorrect
generateMetadata runs on the server during rendering to provide metadata before the page is sent to the browser.
What can you use inside generateMetadata to customize metadata?
AOnly static strings
BRoute parameters and fetched data
CBrowser cookies
DClient-side events
✗ Incorrect
You can use route parameters and fetched data to create dynamic metadata tailored to each page.
Which of these is a valid return value from generateMetadata?
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.