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?
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:
Indicate which paths should be created on build time (returning a paths
array)
Indicate what to do when a certain page eg: "product/myProduct123" doesn't exist in the NextJS Cache (returning a fallback
type)