Performance: GenerateStaticParams for static paths
This affects the build time and initial page load speed by pre-generating static pages for known paths.
Jump into concepts and practice - no test required
export async function generateStaticParams() { const res = await fetch('https://api.example.com/popular-items'); const data = await res.json(); return data.map(item => ({ id: item.id.toString() })); } // Only generates static pages for popular items, reducing build time and bundle size.
export async function generateStaticParams() { const res = await fetch('https://api.example.com/all-items'); const data = await res.json(); return data.map(item => ({ id: item.id.toString() })); } // This fetches all items including rarely visited ones, generating thousands of pages.
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Generate all paths statically | N/A (build time cost) | N/A | N/A | [X] Bad for large datasets |
| Generate only popular paths statically | N/A | N/A | N/A | [OK] Good balance |
| No static generation, fully dynamic | N/A | N/A | N/A | [!] OK but slower initial load |
generateStaticParams in Next.js?generateStaticParams in a Next.js dynamic route file?generateStaticParams function, what static paths will Next.js generate?
export async function generateStaticParams() {
return [
{ slug: 'home' },
{ slug: 'about' },
{ slug: 'contact' }
];
}generateStaticParams function:
export async function generateStaticParams() {
return [
{ id: 1 },
{ id: 2 },
{ id: 3 }
]
}generateStaticParams implementation correctly fetches slugs and returns them for static generation?
async function fetchSlugs() {
return ['post-1', 'post-2', 'post-3'];
}
Choose the correct code: