Course contentsShow
← Web Development
Lesson 1773 of 2,98233. Static Site Generation and JamstackFree lesson

getStaticPaths for Dynamic Routes

Defining which dynamic route paths to pre-render at build time when you have parameterized URLs.

getStaticPaths for Dynamic Routes

You'll learn how to tell your static site generator exactly which dynamic URL paths to pre-render at build time.

The Problem with Dynamic Routes

You've learned that Static Site Generation can create pages at build time using getStaticProps. But what happens when you have dynamic routes like /blog/[slug] or /products/[id]? Your build tool doesn't automatically know which specific slugs or IDs exist—it needs a list.

The getStaticPaths Solution

getStaticPaths is the companion to getStaticProps for parameterized URLs. It returns an array of all possible parameter values that should be pre-rendered as static HTML files.

Think of it like a restaurant printing menus before opening. getStaticProps is the recipe for how to format each menu item, but getStaticPaths is the list telling the printer exactly how many menus to make and what each one should feature.

How It Works

At build time, your static site generator calls getStaticPaths to get the list of paths. For each path returned, it then calls getStaticProps with those parameters to fetch the data and generate the HTML.

For example, if you have blog posts with slugs "hello-world" and "static-generation", getStaticPaths returns:

paths: [
  { params: { slug: "hello-world" } },
  { params: { slug: "static-generation" } }
]

The build process generates /blog/hello-world.html and /blog/static-generation.html, each with their specific content from getStaticProps.

The Fallback Option

getStaticPaths also returns a fallback setting that controls what happens when someone requests a path not in your list—though you've only learned about the pre-rendering part so far.

Key Takeaway: getStaticPaths defines which dynamic route variations to build as static pages by returning an array of parameter values, enabling static generation for parameterized URLs.