Astro

getStaticPaths

getStaticPaths is the function a dynamic Astro route exports to list which pages to create at build. Each item gives the URL parameters and optional props.

How it is measured

Export it from a file such as [slug].astro. Return an array of objects with params and props. Astro builds one page for each item. A route with no getStaticPaths in a static build fails because Astro cannot guess the values.

Count the output. Build and count the files under dist/. The number should equal the array length. Log the array while developing to catch duplicates.

Worked example

A gardening blog has 73 posts in a collection. getStaticPaths maps each entry to a slug and passes the entry as a prop. The build emits 73 HTML files under /blog/.

The editor publishes number 74 on Thursday. Without a rebuild the URL returns 404, because the page does not exist until the next build writes it.

How it differs

getStaticPaths lists pages before the build. Static site generation is the whole process of rendering those pages to files. The function supplies the inputs; the process produces the output.

Common errors

Using it on an on-demand route where it is ignored. Returning duplicate params. Calling it for routes that need request data. Forgetting that props are serialized at build. Mismatching param names and the file name.

In practice

Keep the function small, usually one collection query and one map. Log the count during a build. For content that changes between builds, trigger a rebuild from your CMS.

See also

Static site generation, Astro

Sources

Count this on a real site.

Watch my website