Next.js - Dynamic Route not working in Production Mode when running `Next Build && Next Start`
Last updated: Feb 12, 2021
Can anyone here think of a reason that a dynamic route might work in dev mode but not in production mode when running
next build && next start? We have a project we are working on that has a dynamic
pathbeing created in the query to sanity, and we are using a wildcard
[path].tsxto catch those routes. The query seems to work fine, and in dev we can get to all the pages using this route. But in production we get empty pages for
/page1, and 404s for
Feb 11, 2021, 12:50 PM
[path].tsxcatches only the first level of path like
/page2. Not the sub path routes!! To catch all of them you have to use this one: https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes
Feb 11, 2021, 1:48 PM
Feb 11, 2021, 1:48 PM
Yeah, I tried that and it seems to fix the 404s. What it doesn't help with is why the pages are rendering empty when in production mode, but working fine in dev mode
Feb 11, 2021, 1:51 PM
i think i might have run into this and it was because i had a collision with a static and dynamic page. i had a hard coded page (about.tsx) and then a dynamic route ([slug].tsx) where sanity also has an about slug. not sure if that's related
Feb 11, 2021, 2:22 PM
when i had the collision, i remember the dang kind of bug where it didn't show up when deployed but was fine locally. best to just remove all collisions
Feb 11, 2021, 2:25 PM
Hmm. There shouldn't be a collision between paths I don't think. Thanks for the input though... this is an odd one
Feb 11, 2021, 2:26 PM
Are you using getStaticProps and getStaticPaths,
user M? Also, are you deploying on Vercel? If so, your Function Logs might provide some insight.
Feb 11, 2021, 3:43 PM
Deploying on Netlify at the moment, but have the same issue locally. Have managed to use
getStaticPathsto get things to render properly on first load, but now we have a different issue - when clicking a link to change to another page that uses the same route but has different components, the page is not re-rendering properly, it looks like its only updating the page title
Feb 11, 2021, 3:59 PM
When you’re navigating between pages locally, what shows up in your terminal where you’re running the dev server? Is it similar to this?
Feb 11, 2021, 4:03 PM
Sorry I kind of ghosted there... Got sucked into actually fixing the thing and then had meetings etc. It turns out that the way our guys were serializing the content wasn't refreshing the props when the page changed. So they sorted that and now the rendering updates as it should! Thanks for all the help!
Feb 12, 2021, 3:38 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.