0
0
NextJSframework~8 mins

GenerateMetadata for dynamic metadata in NextJS - Performance & Optimization

Choose your learning style9 modes available
Performance: GenerateMetadata for dynamic metadata
MEDIUM IMPACT
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).
Setting page metadata dynamically based on user or route data
NextJS
export async function generateMetadata() {
  return { title: 'Static or cached title' };
}

// Fetch dynamic data on client or use ISR to update metadata periodically
Using static or cached metadata avoids blocking server rendering and speeds up LCP.
📈 Performance Gainreduces blocking time to near zero, improves LCP by 100-300ms
Setting page metadata dynamically based on user or route data
NextJS
export async function generateMetadata() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return { title: data.title };
}
Fetching data inside generateMetadata blocks the server from sending metadata until the fetch completes, delaying LCP.
📉 Performance Costblocks rendering for 100-300ms depending on API response time
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Dynamic metadata with live fetchMinimal DOM impact0 reflowsBlocks initial paint until data arrives[X] Bad
Static or cached metadataMinimal DOM impact0 reflowsNon-blocking paint[OK] Good
Rendering Pipeline
The generateMetadata function runs on the server during the rendering pipeline before sending HTML to the client. If it waits on async data, it delays Style Calculation and Layout stages.
Server Data Fetch
HTML Generation
Style Calculation
Layout
⚠️ BottleneckServer Data Fetch inside generateMetadata
Core Web Vital Affected
LCP
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).
Optimization Tips
1Avoid slow API calls inside generateMetadata to prevent blocking server rendering.
2Use static or cached metadata when possible to speed up LCP.
3Fetch dynamic data client-side or use ISR to update metadata without blocking.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of fetching data inside generateMetadata in Next.js?
AIt causes excessive client-side JavaScript execution.
BIt delays the server from sending metadata, increasing LCP.
CIt increases the number of DOM nodes on the page.
DIt triggers multiple reflows on the client.
DevTools: Performance
How to check: Record a performance profile while loading the page and look for long tasks or delays before the first contentful paint.
What to look for: Look for server-side blocking time or delayed LCP caused by metadata generation.