Hey there,
I’m working with a Next.js app and trying to implement dynamic routing with Static Site Generation (SSG). I’m using a dynamic route such as [slug].js to generate pages based on dynamic data, but I’m encountering a "Page not found" error when trying to access the generated pages.
I understand that Next.js uses the getStaticPaths function to determine which dynamic pages should be pre-rendered at build time, and getStaticProps for data fetching for each of those pages. However, despite following this process, I keep running into the "Page not found" error for some routes.
Here’s what I’ve already checked:
- I have a
[slug].jsfile in thepagesfolder. - I am using
getStaticPathsto fetch a list of possibleslugvalues (from a CMS or an API) to generate the pages. - I am using
getStaticPropsto fetch the data for each individual page based on theslug.
Even though I’ve set this up, some pages are not getting generated, and I keep seeing the "Page not found" error when trying to access them in the browser.
How can I ensure that all the dynamic pages are correctly generated to avoid this error? Are there any common issues with getStaticPaths or getStaticProps that I might be overlooking?
Things I’m unsure about:
- How should
getStaticPathsbe structured to fetch all possible paths properly? - What could be causing Next.js to not pre-render certain dynamic pages even though the correct slugs are being returned from
getStaticPaths?
I would appreciate any guidance on troubleshooting this issue and ensuring that all dynamic pages are pre-rendered successfully in Next.js.

Tuhin PaulPosted Jan 22, 2025, 6:02 AM
I am sharing a tentative flowchart for dynamic routing with Static Site Generation (SSG), please check and let me know if this works for you?
Tuhin PaulPosted Jan 22, 2025, 6:01 AM
Another approach is :
Validate
getStaticProps:getStaticPropsfetches the correct data for each slug:404 Pages for Missing Data:
fetchDataForSlugdoesn't return data for certain slugs, ensurenotFound: trueis handled ingetStaticProps.Verify CMS/API Integration:
Local Testing:
npm run build && npm startto verify that the static pages are being generated.Tuhin PaulPosted Jan 22, 2025, 5:12 AM
Structure of
getStaticPaths:getStaticPathsis returning the paths correctly.paramsobject matches the dynamic route's file name ([slug].js).Verify Slugs:
slugsandpathsingetStaticPathsto ensure all required slugs are fetched and mapped correctly.Check Fallback Setting:
fallbackisfalse, any slug not listed inpathswill show a 404 error. Usefallback: trueorblockingto handle pages dynamically.Build Logs:
Amira BedhiafiPosted Jan 21, 2025, 1:06 PM
Your getStaticPathsshould look like above and thegetStaticPropscorrectly fetches data for each slug :