If a page has Dynamic Routes and uses getStaticProps, it needs to define a list of paths to be statically generated.
When you export a function called getStaticPaths (Static Site Generation) from a page that uses dynamic routes, Next.js will statically pre-render all the paths specified by getStaticPaths.
// pages/posts/[id].js // Generates `/posts/1` and `/posts/2` export async function getStaticPaths() { return { paths: [{ params: { id: '1' } }, { params: { id: '2' } }], fallback: false, // can also be true or 'blocking' } } // `getStaticPaths` requires using `getStaticProps` export async function getStaticProps(context) { return { // Passed to the page component as props props: { post: {} }, } } export default function Post({ post }) { // Render post... }
The getStaticPaths API reference covers all parameters and props that can be used with getStaticPaths.
You should use getStaticPaths if you’re statically pre-rendering pages that use dynamic routes and:
getStaticProps generates HTML and JSON files, both of which can be cached by a CDN for performancegetStaticPaths will only run during build in production, it will not be called during runtime. You can validate code written inside getStaticPaths is removed from the client-side bundle with this tool.
getStaticProps runs during next build for any paths returned during buildgetStaticProps runs in the background when using fallback: truegetStaticProps is called before initial render when using fallback: blockinggetStaticPaths must be used with getStaticPropsgetStaticPaths with getServerSidePropsgetStaticPaths from a Dynamic Route that also uses getStaticPropsgetStaticPaths from non-page file (e.g. your components folder)getStaticPaths as a standalone function, and not a property of the page componentIn development (next dev), getStaticPaths will be called on every request.
getStaticPaths allows you to control which pages are generated during the build instead of on-demand with fallback. Generating more pages during a build will cause slower builds.
You can defer generating all pages on-demand by returning an empty array for paths. This can be especially helpful when deploying your Next.js application to multiple environments. For example, you can have faster builds by generating all pages on-demand for previews (but not production builds). This is helpful for sites with hundreds/thousands of static pages.
// pages/posts/[id].js export async function getStaticPaths() { // When this is true (in preview environments) don't // prerender any static pages // (faster builds, but slower initial page load) if (process.env.SKIP_BUILD_STATIC_GENERATION) { return { paths: [], fallback: 'blocking', } } // Call an external API endpoint to get posts const res = await fetch('https://.../posts') const posts = await res.json() // Get the paths we want to prerender based on posts // In production environments, prerender all pages // (slower builds, but faster initial page load) const paths = posts.map((post) => ({ params: { id: post.id }, })) // { fallback: false } means other routes should 404 return { paths, fallback: false } }
For more information on what to do next, we recommend the following sections: