generateStaticParams can be used with dynamic route segments to statically generate routes at build time instead of on-demand at request time.
It can be exported from:
- Pages (
page.tsx) - Layouts (
layout.tsx) - Route Handlers (
route.ts)
app/blog/[slug]/page.tsx
Parameters
When multiple dynamic segments in a route each export
generateStaticParams, the child function receives the params object generated by the parent. This allows child segments to generate their own params based on parent values.Returns
An array of objects where each object represents the populated dynamic segments of a single route.- Each property key is the segment name.
- Each property value is the string value (or array of strings for catch-all segments) to fill in.
| Route | Return type |
|---|---|
/product/[id] | { id: string }[] |
/products/[category]/[product] | { category: string; product: string }[] |
/products/[...slug] | { slug: string[] }[] |
Good to know
- Use
dynamicParamsto control what happens when a dynamic segment not covered bygenerateStaticParamsis visited. - Return an empty array (or use
export const dynamic = 'force-static') to render all paths at runtime (ISR). - During
next dev,generateStaticParamsis called when you navigate to a route. - During
next build, it runs before the corresponding Layouts and Pages are generated. - During revalidation (ISR),
generateStaticParamsis not called again. generateStaticParamsreplacesgetStaticPathsfrom the Pages Router.fetchrequests ingenerateStaticParamsare automatically memoized across allgenerate-prefixed functions, Layouts, Pages, and Server Components.
Examples
Single dynamic segment
app/product/[id]/page.tsx
Multiple dynamic segments
app/products/[category]/[product]/page.tsx
Catch-all segment
app/product/[...slug]/page.tsx
All paths at build time
app/blog/[slug]/page.tsx
Subset of paths, rest at runtime
app/blog/[slug]/page.tsx
Disabling unspecified paths (404)
app/blog/[slug]/page.tsx
Multiple dynamic segments (top-down)
Generate parent segment params first, then use them in childgenerateStaticParams:
app/products/[category]/layout.tsx
app/products/[category]/[product]/page.tsx
Version history
| Version | Changes |
|---|---|
v13.0.0 | generateStaticParams introduced. |
