generateStaticParams
The generateStaticParams function can be used in combination with dynamic route segments to statically generate routes at build time instead of on-demand at request time.
Good to know
- You can use the
dynamicParamssegment config option to control what happens when a dynamic segment is visited that was not generated withgenerateStaticParams. - During
next dev,generateStaticParamswill be called when you navigate to a route. - During
next build,generateStaticParamsruns before the corresponding Layouts or Pages are generated. - During revalidation (ISR),
generateStaticParamswill not be called again. generateStaticParamsreplaces thegetStaticPathsfunction in the Pages Router.
Parameters
options.params (optional)
If multiple dynamic segments in a route use generateStaticParams, the child generateStaticParams function is executed once for each set of params the parent generates.
The params object contains the populated params from the parent generateStaticParams, which can be used to generate the params in a child segment.
Returns
generateStaticParams should return an array of objects where each object represents the populated dynamic segments of a single route.
- Each property in the object is a dynamic segment to be filled in for the route.
- The properties name is the segment's name, and the properties value is what that segment should be filled in with.
| Example Route | generateStaticParams Return Type |
|---|---|
/product/[id] | { id: string }[] |
/products/[category]/[product] | { category: string, product: string }[] |
/products/[...slug] | { slug: string[] }[] |
Single Dynamic Segment
Multiple Dynamic Segments
Catch-all Dynamic Segment
Examples
Multiple Dynamic Segments in a Route
You can generate params for dynamic segments above the current layout or page, but not below. For example, given the app/products/[category]/[product] route:
app/products/[category]/[product]/page.jscan generate params for both[category]and[product].app/products/[category]/layout.jscan only generate params for[category].
There are two approaches to generating params for a route with multiple dynamic segments:
Generate params from the bottom up
Generate multiple dynamic segments from the child route segment.
Generate params from the top down
Generate the parent segments first and use the result to generate the child segments.
A child route segment's generateStaticParams function is executed once for each segment a parent generateStaticParams generates.
The child generateStaticParams function can use the params returned from the parent generateStaticParams function to dynamically generate its own segments.
Good to know
fetch requests are automatically memoized for the same data across all generate-prefixed functions, Layouts, Pages, and Server Components. React cache can be used if fetch is unavailable.
Generate only a subset of params
You can generate a subset of params for a route by returning an array of objects with only the dynamic segments you want to generate. Then, by using the dynamicParams segment config option, you can control what happens when a dynamic segment is visited that was not generated with generateStaticParams.
Version History
| Version | Changes |
|---|---|
v13.0.0 | generateStaticParams introduced. |
Last updated