Skip to content
tezvyn:

generateStaticParams: Pre-building Dynamic Pages in Next.js

Source: nextjs.orgMediumHow cards are made

Think of generateStaticParams as a build-time guest list for your dynamic routes. It tells Next.js all possible parameters (like post slugs) upfront to pre-render static pages. The footgun: it only provides params, not the page data itself.

Why it exists

When you build a site for production, Next.js can pre-render pages as static HTML. For a dynamic route like /blog/[slug], Next.js doesn't know what all the possible slugs are. generateStaticParams was created to solve this by explicitly telling Next.js which pages to generate at build time.

The mental model

Imagine you're a printer tasked with creating personalized invitations. generateStaticParams is the spreadsheet you're given with everyone's name on it. Instead of waiting for someone to request an invitation for 'Alice' and then 'Bob', you get the entire guest list upfront and can print all the invitations in one efficient batch. The function provides the list of pages to build; it doesn't write the content of the invitation itself.

How it works

In a dynamic route segment's page.js or layout.js file (e.g., app/products/[id]/page.js), you export an async function called generateStaticParams. This function fetches data and must return an array of objects, where each object represents one page to be generated. For the product example, it would return [{ id: '1' }, { id: '2' }, { id: '3' }]. Next.js then iterates through this array, building a static page for /products/1, /products/2, and /products/3.

When to use it

Use generateStaticParams in the App Router for any dynamic route you want to statically generate (SSG). This is ideal for content that doesn't change frequently, like blog posts, e-commerce product pages, or documentation sites. It results in extremely fast page loads because the HTML is pre-built and served from a CDN.

When not to use it

Do not use this for pages that require fresh data on every request, like a user's account dashboard; use dynamic rendering (SSR) instead. Also, avoid it if the number of possible parameters is enormous (e.g., millions of user profiles), as this would make the build process impossibly long. In such cases, you can let pages be generated on-demand.

One canonical example

For a blog at /posts/[slug], you would define generateStaticParams in app/posts/[slug]/page.js. The function would fetch all post slugs from your database or CMS and return [{ slug: 'intro-to-react' }, { slug: 'advanced-css-tricks' }]. Then, the Page component in that same file would receive params.slug and use it to fetch the full content for that specific post. The key is that generateStaticParams provides the 'what' (which pages), while the Page component handles the 'how' (fetching and rendering the content).

Interview question

What is the primary role of generateStaticParams in Next.js?

  • a.To define the dynamic segment names and their corresponding data types for a route.
  • b.To fetch the full content for all dynamic pages at build time.
  • c.To specify which dynamic routes should be pre-rendered as static HTML during the build.Correct
  • d.To enable server-side rendering for dynamic routes that need fresh data on every request.
Why?

generateStaticParams acts as a 'guest list' that explicitly tells Next.js which specific dynamic pages (identified by their parameters) to pre-render into static HTML during the build process. Option B is incorrect because it only provides the parameters, not the actual page content, which is fetched by the page component itself.

Just read this? Test yourself on what you have been reading.

Read the original → nextjs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on next.js — each one lists the topics its interview covers.

See open roles