Performance: GenerateMetadata for dynamic metadata
This affects the page load speed by controlling when and how metadata is generated and sent to the browser, impacting the Largest Contentful Paint (LCP).
export async function generateMetadata() { return { title: 'Static or cached title' }; } // Fetch dynamic data on client or use ISR to update metadata periodically
export async function generateMetadata() { const res = await fetch('https://api.example.com/data'); const data = await res.json(); return { title: data.title }; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Dynamic metadata with live fetch | Minimal DOM impact | 0 reflows | Blocks initial paint until data arrives | [X] Bad |
| Static or cached metadata | Minimal DOM impact | 0 reflows | Non-blocking paint | [OK] Good |
generateMetadata function in Next.js?generateMetadatagenerateMetadata function in a Next.js page?generateMetadata as an exported async function returning an object with metadata.generateMetadata function, what will be the page title?export async function generateMetadata({ params }) {
return { title: `User: ${params.username}` };
}/user/jane.params with username from the URL segment.params.username with actual value/user/jane, params.username is 'jane', so title becomes 'User: jane'.generateMetadata function?export function generateMetadata() {
return { title: 'Home' }
console.log('Metadata generated')
}return statement ends the function immediately, so code after it won't run.console.log after return is unreachable and will never execute.generateMetadata implementation correctly handles this using params and fetch to get post data asynchronously?generateMetadata must be async and await the fetch call.params.id and returns metadata using fetched data.Pass the test and this pattern moves unknown