reactjsnext.jsserver-side-renderingstatic-sitessg

Error: getStaticPaths is required for dynamic SSG pages and is missing for "xxx". NextJS


I am getting this error "Error: getStaticPaths is required for dynamic SSG pages and is missing for 'xxx'" when I try to create my page in NextJS.

I don't want to generate any static page on build time. So why do I need to create a 'getStaticPaths' function?


Solution

  • If you are creating a dynamic page eg: product/[slug].tsx then even if you don't want to create any page on build time you need to create a getStaticPaths method to set the fallback property and let NextJS know what to do when the page you are trying to get doesn't exist.

    export const getStaticPaths: GetStaticPaths<{ slug: string }> = async () => {
    
        return {
            paths: [], //indicates that no page needs be created at build time
            fallback: 'blocking' //indicates the type of fallback
        }
    }
    

    getStaticPaths does mainly two things: