Course outline · 0% complete

0/29 lessons0%

Course overview →

generateStaticParams: prebuild the pages

lesson 5-2 · ~11 min · 16/29

Rendering ahead of time

By default a dynamic route renders on demand: the first visitor to /blog/hello-world waits while the server fetches and renders. But your blog knows its post list in advance. generateStaticParams lets you hand Next.js that list at build time:

// app/blog/[slug]/page.js
export async function generateStaticParams() {
  const posts = await fetch("https://api.example.com/posts").then((r) => r.json());
  return posts.map((post) => ({ slug: post.slug }));
}

Decode it:

  • It runs during next build, not per request.
  • It returns an array of params objects, one per page to prerender: [{ slug: "hello-world" }, { slug: "why-frameworks-win" }, …].
  • Next.js then renders each of those pages once, at build time, and serves the finished HTML to everyone. Fast for users, cheap for your server.

Quiz

generateStaticParams returns 50 slug objects at build time. What did Next.js produce by the end of next build?

Worked example: prebuild the popular, render the rest

generateStaticParams does not have to return every possible page — and at scale it should not. An e-commerce site with 200,000 products cannot rebuild them all on every deploy (builds would take hours). The production pattern is a partial list:

// app/products/[id]/page.js
export async function generateStaticParams() {
  const top = await getTop100ProductIds(); // best sellers only
  return top.map((id) => ({ id }));
}

What this buys you:

  • The 100 pages that receive most of the traffic are prebuilt and instant.
  • The long tail renders on demand on first request, then is kept, so the second visitor to any product is fast too.
  • Builds stay minutes long no matter how big the catalog grows.

The list is a performance dial, not a registry: turn it up for hot pages, let on-demand rendering cover the rest.

Quiz

A store has 200,000 products but generateStaticParams returns only the 100 best sellers. A shopper visits product #150,000 (not in the list). What happens by default?

What about slugs not in the list?

A visitor requests /blog/brand-new-post, published after the build. By default Next.js does not give up: it renders that page on demand on first request, then keeps the result. So generateStaticParams is an optimization list, not a security wall. Pages you listed are ready instantly, pages you did not are generated when first asked for.

(If you want unlisted slugs to 404 instead, an exported config option called dynamicParams can turn on-demand rendering off. Just know it exists.)

Problem

A docs site has exactly three pages: intro, setup, and faq, served by app/docs/[page]/page.js. How many objects should generateStaticParams return to prerender them all?