GenerateStaticParams helps Next.js know which pages to build ahead of time. It makes your site faster by creating pages before users visit.
GenerateStaticParams for static paths in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NextJS
export async function generateStaticParams() { return [ { id: '1' }, { id: '2' }, { id: '3' } ]; }
This function returns an array of objects. Each object represents a path parameter.
Next.js uses these to build static pages at build time.
Examples
NextJS
export async function generateStaticParams() { return [ { slug: 'welcome' }, { slug: 'about' } ]; }
NextJS
export async function generateStaticParams() { const ids = ['a', 'b', 'c']; return ids.map(id => ({ id })); }
Sample Program
This Next.js page uses generateStaticParams to pre-build pages for post IDs 1 and 2. When you visit /post/1 or /post/2, the page shows the post ID.
NextJS
import React from 'react'; export async function generateStaticParams() { return [ { id: '1' }, { id: '2' } ]; } export default function Page({ params }) { return <h1>Post ID: {params.id}</h1>; }
Important Notes
Make sure the keys in the returned objects match your dynamic route segment names.
generateStaticParams runs only at build time, so it cannot use request-specific data.
Summary
generateStaticParams tells Next.js which pages to build before users visit.
It returns an array of objects with route parameters.
This improves page speed and SEO for static content.
Practice
1. What is the main purpose of
generateStaticParams in Next.js?easy
Solution
Step 1: Understand the role of generateStaticParams
This function is used to specify dynamic route parameters for static generation.Step 2: Compare with other Next.js features
Unlike client-side navigation or API routes, generateStaticParams runs at build time to pre-build pages.Final Answer:
To tell Next.js which dynamic routes to pre-render at build time -> Option AQuick Check:
generateStaticParams = pre-render dynamic routes [OK]
Hint: Remember: generateStaticParams runs at build time for static pages [OK]
Common Mistakes:
- Confusing generateStaticParams with client-side data fetching
- Thinking it runs on every request
- Mixing it up with API route definitions
2. Which of the following is the correct syntax for
generateStaticParams in a Next.js dynamic route file?easy
Solution
Step 1: Recall the correct return format
generateStaticParams returns an array of objects with route parameters as keys.Step 2: Check syntax correctness
export async function generateStaticParams() { return [{ id: '1' }, { id: '2' }]; } correctly exports an async function returning [{ id: '1' }, { id: '2' }]. Others have wrong return types or use getStaticPaths.Final Answer:
export async function generateStaticParams() { return [{ id: '1' }, { id: '2' }]; } -> Option BQuick Check:
Return array of param objects = export async function generateStaticParams() { return [{ id: '1' }, { id: '2' }]; } [OK]
Hint: generateStaticParams returns array of objects with params keys [OK]
Common Mistakes:
- Using getStaticPaths instead of generateStaticParams
- Returning array of strings instead of objects
- Not exporting the function properly
3. Given this
generateStaticParams function, what static paths will Next.js generate?
export async function generateStaticParams() {
return [
{ slug: 'home' },
{ slug: 'about' },
{ slug: 'contact' }
];
}medium
Solution
Step 1: Understand the returned params
The function returns an array with slug keys: 'home', 'about', 'contact'.Step 2: Map params to URLs
Next.js uses these slugs as dynamic route parts, so paths are /home, /about, /contact.Final Answer:
/home, /about, /contact -> Option AQuick Check:
Params slug values = generated paths [OK]
Hint: Params keys map directly to URL segments in static paths [OK]
Common Mistakes:
- Adding extra path segments like /slug/
- Assuming root path / is included automatically
- Including paths not returned by generateStaticParams
4. Identify the error in this
generateStaticParams function:
export async function generateStaticParams() {
return [
{ id: 1 },
{ id: 2 },
{ id: 3 }
]
}medium
Solution
Step 1: Check parameter types
Route parameters in Next.js must be strings because URLs are strings.Step 2: Identify type mismatch
Here, id values are numbers (1, 2, 3), which can cause build errors or unexpected behavior.Final Answer:
The id values should be strings, not numbers -> Option DQuick Check:
Route params must be strings [OK]
Hint: Always use strings for route parameters in generateStaticParams [OK]
Common Mistakes:
- Returning numbers instead of strings for params
- Confusing generateStaticParams with getStaticPaths
- Returning object instead of array
5. You want to statically generate blog post pages with slugs from an API. Which
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:hard
Solution
Step 1: Understand expected return format
generateStaticParams expects an array of objects with route params keys directly, e.g. [{ slug: 'post-1' }].Step 2: Analyze each option
export async function generateStaticParams() { const slugs = await fetchSlugs(); return slugs.map(slug => ({ slug })); } returns slugs mapped to objects with slug keys correctly. export async function generateStaticParams() { const slugs = await fetchSlugs(); return { paths: slugs }; } returns an object, not array. export async function generateStaticParams() { const slugs = await fetchSlugs(); return slugs; } returns array of strings, not objects. export async function generateStaticParams() { const slugs = await fetchSlugs(); return slugs.map(slug => ({ params: { slug } })); } adds extra params key, which is incorrect for generateStaticParams.Final Answer:
export async function generateStaticParams() { const slugs = await fetchSlugs(); return slugs.map(slug => ({ slug })); } -> Option CQuick Check:
Return array of param objects without extra nesting [OK]
Hint: Map slugs to objects with keys matching route params [OK]
Common Mistakes:
- Returning object with paths key instead of array
- Returning array of strings instead of objects
- Adding extra nesting like { params: { slug } }
