Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Next.js 16+ · Function

generateStaticParams

generateStaticParams supplies dynamic-segment values that Next.js uses to prerender routes during the build. It can target all known paths or only a selected subset.

How it works

Export generateStaticParams from a dynamic route to provide the parameter values Next.js should prerender. It returns one object per generated pathname, with property names matching the route's dynamic segments; the function can be async when values come from a data source.

Syntax

export async function generateStaticParams(): Promise<Array<Record<string, string | string[]>>>

Returns

An array of objects; each object contains values for the dynamic segments of one route to generate.

Key points

  • Use it to prerender dynamic routes during the build rather than waiting for their first request.
  • Each returned object describes the dynamic-segment values for one pathname.
  • Object properties correspond to dynamic segment names in the route.
  • Build-time execution happens before Next.js generates the associated layout or page output.

Common mistakes

  • The return value must be an array, including when no paths are selected.
  • Changing or revalidating cached output does not rerun this function.
  • With Cache Components enabled, an empty result is invalid and causes a build error.

Example: Prerender known blog paths

export async function generateStaticParams() {
  const posts = await fetch('https://example.com/api/posts').then((res) => res.json())
  return posts.map((post: { slug: string }) => ({ slug: post.slug }))
}

export default async function Page({ params }: PageProps<'/blog/[slug]'>) {
  const { slug } = await params
  return <h1>{slug}</h1>
}

Fetches known posts during the build and returns one parameter object per blog route.

When to use it

Prerender known content paths

A blog, documentation site, or catalog has a known collection of slugs or IDs at build time.

Prerender a subset

Only popular or editorially selected dynamic paths should be generated initially.

Generate static API responses

A dynamic route handler should expose static responses for known parameter combinations.

Interview questions

QWhat problem does generateStaticParams solve?

It supplies parameter objects for dynamic segments so Next.js can statically generate matching routes at build time rather than on demand. It is supported by pages, layouts, and route handlers.

Follow-up: How would you generate only selected routes? Return an array containing only the parameter objects for those routes.

QWhat should the function return?

Return an array in which each object represents one route's populated dynamic segments. For a [slug] segment, each object needs a slug property containing the value used in that pathname.

Follow-up: What must be true for a nested route with multiple dynamic segments? The generated parameter data must populate the dynamic segments required for each generated route.

QWhen does generateStaticParams run?

It runs before the corresponding layouts or pages are generated during next build. It is not called again during ISR, so regenerated output does not cause a fresh parameter discovery pass.

Follow-up: What happens with Cache Components and an empty array? The build fails because at least one parameter is required to validate a non-empty static shell.

  • dynamic route segments
  • generateMetadata
  • Page
  • Layout
  • Route Handlers
  • dynamicParams

Source: generateStaticParams. Checked 2026-10-15.