Next.jsMedium
What is generateStaticParams and how does it replace getStaticPaths?
generateStaticParams is an App Router function exported from a dynamic route's page or layout that returns the param values to prerender at build time, for example [{ slug: "a" }, { slug: "b" }]. It replaces getStaticPaths, with simpler return values, support at multiple nesting levels, and deduplicated fetch calls with the page.
Differences from getStaticPaths
getStaticPaths | generateStaticParams | |
|---|---|---|
| Return | { paths: [{ params }], fallback } | array of param objects |
| Unknown paths | fallback: false/true/"blocking" | dynamicParams (previous model) or App Shell + background generation (Cache Components) |
| Nested segments | one function per page | each level can generate its own params, and children receive parent params |
| Data | separate call | fetch memoized with the page |
Behavior
- Runs at build time, before the layouts and pages for those params are rendered.
- In dev, it runs on navigation to check params (and its time shows in dev request timings since 16.1).
- Return
[]to prerender nothing at build and generate on demand (in the previous model this needsdynamic = 'force-static'for ISR-like behavior; with Cache Components it serves the App Shell).
With Cache Components
- Must return at least one param set; an empty array fails validation, because Next.js needs to verify the route renders.
dynamicParamsis not supported; unknown params serve the App Shell and are generated in the background.- With
ensureStatic = 'navigation'every dynamic param must be covered.
Nested routes
For /shop/[category]/[item], the category layout returns categories and the item page returns items for the category it receives as params.
Code example
tsx
1// app/shop/[category]/layout.tsx2export async function generateStaticParams() {3 const categories = await fetch("https://api.example.com/categories").then((r) => r.json());4 return categories.map((c: { slug: string }) => ({ category: c.slug }));5}67// app/shop/[category]/[item]/page.tsx8export async function generateStaticParams({ params }: { params: { category: string } }) {9 // Called once per category generated by the parent10 const items = await fetch(`https://api.example.com/categories/${params.category}/items?top=20`).then((r) => r.json());11 return items.map((i: { slug: string }) => ({ item: i.slug }));12}1314export default async function Item({ params }: PageProps<"/shop/[category]/[item]">) {15 const { category, item } = await params;16 return <h1>{category} / {item}</h1>;17}1819// Pages Router equivalent:20// export const getStaticPaths = async () => ({ paths: [{ params: { category: "shoes", item: "runner" } }], fallback: "blocking" });
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.