Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
GenerateStaticParams for Static Paths in Next.js
📖 Scenario: You are building a simple blog website using Next.js. You want to create static pages for each blog post so that the pages load fast and work offline.To do this, you will use the generateStaticParams function to tell Next.js which blog post pages to create ahead of time.
🎯 Goal: Build a Next.js component that uses generateStaticParams to generate static paths for blog posts with IDs 1, 2, and 3.This will create static pages for each blog post at build time.
📋 What You'll Learn
Create a list of blog post IDs as strings
Create a generateStaticParams function that returns an array of objects with id properties
Create a React component that receives params and displays the blog post ID
Export the component as default and export generateStaticParams
💡 Why This Matters
🌍 Real World
Static site generation improves performance and SEO by pre-building pages for known paths.
💼 Career
Understanding generateStaticParams is essential for Next.js developers building fast, scalable websites.
Progress0 / 4 steps
1
Create the blog post IDs array
Create a constant array called postIds with the string values '1', '2', and '3'.
NextJS
Hint
Use const postIds = ['1', '2', '3']; to create the array.
2
Create the generateStaticParams function
Create an async function called generateStaticParams that returns an array of objects. Each object should have an id property from the postIds array.
NextJS
Hint
Use postIds.map(id => ({ id })) to create the array of objects.
3
Create the blog post page component
Create a React functional component called PostPage that takes { params } as a prop and returns a <div> showing the text Post ID: followed by params.id.
NextJS
Hint
Use function PostPage({ params }) and return a div with the post ID.
4
Export the component and generateStaticParams
Ensure the PostPage component is exported as default and the generateStaticParams function is exported (already done). Confirm the full code includes the postIds array, the generateStaticParams function, and the PostPage component.
NextJS
Hint
Check that all parts are exported and included in the code.
Practice
(1/5)
1. What is the main purpose of generateStaticParams in Next.js?
easy
A. To tell Next.js which dynamic routes to pre-render at build time
B. To fetch data on every user request
C. To handle client-side navigation between pages
D. To define API routes in Next.js
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 A
B. The function must return an object, not an array
C. The function must be named getStaticPaths instead
D. The id values should be strings, not numbers
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 D
Quick 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
A. export async function generateStaticParams() {
const slugs = await fetchSlugs();
return slugs;
}
B. export async function generateStaticParams() {
const slugs = await fetchSlugs();
return { paths: slugs };
}
C. export async function generateStaticParams() {
const slugs = await fetchSlugs();
return slugs.map(slug => ({ slug }));
}