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

getStaticPathsgenerateStaticParams
Return{ paths: [{ params }], fallback }array of param objects
Unknown pathsfallback: false/true/"blocking"dynamicParams (previous model) or App Shell + background generation (Cache Components)
Nested segmentsone function per pageeach level can generate its own params, and children receive parent params
Dataseparate callfetch 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 needs dynamic = '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.
  • dynamicParams is 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.tsx
2export 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}
6
7// app/shop/[category]/[item]/page.tsx
8export async function generateStaticParams({ params }: { params: { category: string } }) {
9 // Called once per category generated by the parent
10 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}
13
14export default async function Item({ params }: PageProps<"/shop/[category]/[item]">) {
15 const { category, item } = await params;
16 return <h1>{category} / {item}</h1>;
17}
18
19// 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.

Open in study app