Next.js generateStaticParams
generateStaticParams builds one static page per book at build time. The build output lists them.
The function lists the books
The function maps the five books to five ids. One id becomes one page.
export function generateStaticParams() {
return getBooks().map((book) => ({ id: book.id }));
}A missing book answers 404
The check asks for a book that does not exist. The answer is 404.
curl -s -o /dev/null -w "HTTP %{http_code}\n" http://localhost:3111/books/999Example
npm run buildKey points
- Static page: a page built once, at build time, and served as it is
- generateStaticParams: the function that lists the pages to build
- It returns one object per book. Each object holds that book id.
- The build output marks each built page with a filled circle.
- Five books mean five static pages. A missing book still answers 404.
In this example, the build output lists the book pages. Each line with a filled circle is one static page. Dune, Pride and Prejudice, and The Hobbit are named, and the last line says two more paths follow. Five books became five static pages at build time. The home page is dynamic instead, because it reads the search query on each visit.
Note
Static pages are fast to serve, because the work is done at build time.
Try it Yourself
Edit the code, then press Run.
Running is simulated · TypeScript Playground
Exercise
What does generateStaticParams return?