# generateStaticParams

> Pre-rendering dynamic routes at build time.

Subject: Next.js | Lesson 55 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/generate-static-params
Previous: [Dynamic Metadata](https://codewithanimation.in/nextjs/dynamic-metadata.md) | Next: [Dynamic SEO Files](https://codewithanimation.in/nextjs/dynamic-seo-files.md)

## Build-Time Generation

`generateStaticParams` is an async function you can export from a dynamic route segment (like `app/blog/[slug]/page.tsx`).

Instead of waiting for a user to visit the page to render it, Next.js calls this function *during the build process*. It returns an array of parameters, and Next.js uses those parameters to instantly stamp out static HTML files for every route.

## The Speed of Static

Static pages are significantly faster and cheaper to host than dynamic pages.

If you have an e-commerce store with 10,000 products, you don't want the server to do heavy database queries and React rendering every time a user clicks a product.

By using `generateStaticParams`, you pre-bake those 10,000 pages into static HTML and push them to a global CDN. When a user requests one, the CDN responds in milliseconds without ever waking up your origin server.

## Implementation

```tsx
// app/blog/[slug]/page.tsx
export async function generateStaticParams() {
  const posts = await fetchPosts();
  
  // The key 'slug' must match the folder name [slug]
  return posts.map((post) => ({
    slug: post.slug,
  }));
}
```

## Common mistake: The Key Mismatch Trap

The most common mistake is returning an object with the wrong key name.

If your folder is named `app/product/[id]/page.tsx`, your function MUST return `[{ id: '123' }]`.

If you accidentally return `[{ slug: '123' }]`, Next.js will silently ignore it, and your pages will fall back to slow, on-demand dynamic rendering.

## Try it: The Build Matrix

Watch the Next.js compiler execute `generateStaticParams` at build time, converting a database array into physical HTML files.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/generate-static-params

## Check yourself

### Question 1

If your route folder is named `app/store/[category]/[item]/page.tsx`, what must `generateStaticParams` return?

- A. An array of URL strings: ['/store/shoes/nike']
- B. An array of objects matching the segment names: [{ category: 'shoes', item: 'nike' }]
- C. A single object mapping all paths.
- D. A boolean true to enable static generation.

Correct answer: B

Next.js iterates over the returned array. For every object, it looks at the keys. The keys must exactly match the dynamic bracket folders in the file path.

- Why not A: It must return an array of objects, not strings.
- Why not C: It must be an array of objects.
- Why not D: Next.js needs the actual parameter data to build the routes.

## Remember this

- `generateStaticParams` only runs at build time, never at request time.
- It converts dynamic SSR routes into blazing fast Static (SSG) routes.
- The keys in the returned objects MUST perfectly match the bracketed folder names.
- You can combine it with `export const dynamicParams = false` to return a 404 for any path not generated at build time.
