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.


Amira BedhiafiRank #601 · 4K reputationPosted Jan 21, 2025, 8:06 AM EST
export async function getStaticPaths() { const slugs = await fetchSlugs(); return { paths: slugs.map((slug) => ({ params: { slug } })), fallback: false, }; }Your getStaticPathsshould look like above and thegetStaticPropscorrectly fetches data for each slug :export async function getStaticProps({ params }) { const slug = params.slug; const data = await fetchDataForSlug(slug); if (!data) { return { notFound: true }; } return { props: { data }, }; }Tuhin PaulRank #7 · 71.9K reputationPosted Jan 22, 2025, 12:12 AM EST
Structure of
getStaticPaths:getStaticPathsis returning the paths correctly.paramsobject matches the dynamic route's file name ([slug].js).export async function getStaticPaths() { const slugs = await fetchSlugs(); // Replace with your CMS/API logic const paths = slugs.map((slug) => ({ params: { slug } })); return { paths, fallback: false }; // Or 'blocking' for fallback rendering }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:
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 22, 2025, 1:01 AM EST
Another approach is :
Validate
getStaticProps:getStaticPropsfetches the correct data for each slug:export async function getStaticProps({ params }) { const data = await fetchDataForSlug(params.slug); // Replace with your data fetching logic if (!data) { return { notFound: true }; // Return a 404 if the slug data doesn't exist } return { props: { data } }; }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 PaulRank #7 · 71.9K reputationPosted Jan 22, 2025, 1:02 AM EST
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?