Generating static params

Intermediate · 14 min

Use generateStaticParams to prerender dynamic routes at build time, handle nested segments, and see what changes under Cache Components.

Before thisStatic and dynamic renderingDynamic routes and params

tapaScript by Tapas Adhikary · 14 min

Generating static params

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by tapaScript by Tapas AdhikaryReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A dynamic route like /blog/[slug] has no fixed URLs at build time, so generateStaticParams returns the list of param values to prerender.
  • It returns an array of objects keyed by segment name: { slug: string }[] for /blog/[slug], and { slug: string[] }[] for a catch-all.
  • For nested dynamic segments, the child's generateStaticParams runs once for each set of params the parent produced and receives them.
  • Without Cache Components, dynamicParams decides what happens to an unlisted path: render it on demand (the default) or return a 404.
  • With Cache Components, generateStaticParams must return at least one result and the dynamicParams export is a build error; unlisted params render on request, so call notFound() for bad ones.

Dynamic routes have no URLs yet

A folder like app/blog/[slug]/page.tsx describes a pattern, not pages. At build time Next.js cannot know which slugs exist. Without more information it waits until someone visits /blog/hello-world and renders it then.

generateStaticParams is how you tell it: here are the values worth building ahead.

// app/blog/[slug]/page.tsx
export async function generateStaticParams() {
  const posts = await fetch('https://api.vercel.app/blog').then((res) => res.json())
  return posts.map((post: { slug: string }) => ({ slug: post.slug }))
}

export default async function Page({ params }: PageProps<'/blog/[slug]'>) {
  const { slug } = await params
  // render the post
}

Each returned object fills in the segments of one URL. The property name is the segment name; the value is what goes in it. The docs’ table: /product/[id] returns { id: string }[], /products/[category]/[product] returns { category: string, product: string }[], and /products/[...slug] returns { slug: string[] }[]. It replaces getStaticPaths from the Pages Router.

Nested segments

With multiple dynamic segments, the child’s function receives the parent’s params and runs once for each parent value:

// app/products/[category]/layout.tsx
export async function generateStaticParams() {
  return [{ category: 'tops' }, { category: 'shorts' }]
}

// app/products/[category]/[product]/page.tsx
export async function generateStaticParams({ params }: { params: { category: string } }) {
  const products = await getProducts(params.category)
  return products.map((p) => ({ product: p.slug }))
}

You can also return every combination from the leaf segment, which is simpler when the data comes from one query.

Timing

During next build, generateStaticParams runs before the pages and layouts are generated. During next dev it runs when you visit a route. During revalidation it is not called again. Fetches inside it that match a fetch in the page are memoized, so you do not pay for the data twice.

Unlisted paths: two models

I built a route with generateStaticParams returning [{ slug: 'a' }] on 16.4.6 with Cache Components on. The route table showed /posts/[slug] as partial prerender and /posts/a as a static page with a 1h revalidate and a 1d expire.

Without Cache Components, dynamicParams controls what happens to a path you did not list:

  • true (the default): render it on demand and cache the result.
  • false: return a 404.

With Cache Components, the rules change in three ways, each of which I triggered on 16.4.6:

  1. generateStaticParams must return at least one param. An empty array fails the build: “When using Cache Components, all generateStaticParams functions must return at least one result.” The reason, from the error text, is to let Next.js validate at build time that nothing else would cause a runtime error.
  2. Exporting dynamicParams is a build error: “Route segment config “dynamicParams” is not compatible with nextConfig.cacheComponents. Please remove it.”
  3. Unlisted params render on request. If you used dynamicParams = false to reject unknown values, call notFound() in the page when the param does not match real data.

Also, removing generateStaticParams entirely opts the route out of ISR. The next lesson covers how unlisted URLs are served and upgraded.

Keep the build cheap

If your site has 50,000 posts, do not prerender all of them if most are never visited. Return the popular ones and let the rest render at request time. With Cache Components, the docs’ example returns one product per category, using slice(0, 1) to keep the build short, and says paths you do not return are still served at request time.

What the video covers

tapaScript’s January 2025 video covers, by its chapters: why generateStaticParams matters, what it is, nested dynamic routes, real-world examples, and a few tips. It was recorded before Next.js 16, so it describes the dynamicParams model. Codevolution’s two short videos are linked as runner-ups: the first is the basics and multiple segments, the second the dynamicParams option.

Where this fits

You can now say which pages exist at build time. The follow-up is what happens to them afterwards. Next: incremental static regeneration. If routes with params are new to you, see dynamic routes and params.

Check yourself

  1. 01What does generateStaticParams return for /products/[category]/[product]?

    An array of objects with both segment names as keys, such as { category: string, product: string }[], one object per URL to prerender.

  2. 02For a route with nested dynamic segments, when does the child's generateStaticParams run?

    Once for every set of params the parent generated, and it receives that set so it can return the child values for each parent.

  3. 03What did dynamicParams = false do before Cache Components, and what replaces it?

    It made unlisted paths return a 404 instead of rendering on demand, and with Cache Components you delete the export, because it fails the build, and call notFound() in the page when the param does not match real data.

  4. 04Why would generateStaticParams returning an empty array break a Cache Components build?

    Cache Components needs at least one real param to prerender and validate the route's static shell, so an empty array is an error, and returning one real param is the minimum.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark