Unlock seamless workflows and faster delivery with our latest releases- get the details

Next.js - Dynamic Route not working in Production Mode when running `Next Build && Next Start`

18 replies
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
path
being created in the query to sanity, and we are using a wildcard
[path].tsx
to 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
/page1/childpage
Feb 11, 2021, 12:50 PM
This here
[path].tsx
catches only the first level of path like
/page1
and
/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
[…path].tsx
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
In the dev mode 404 pages a not supported
Feb 11, 2021, 1:52 PM
no, sorry... not what i mean.
Feb 11, 2021, 1:52 PM
So, I've changed to the catch-all route
Feb 11, 2021, 1:53 PM
That route, in dev mode, returns a page with content from Sanity as expected
Feb 11, 2021, 1:53 PM
in production mode it returns empty
Feb 11, 2021, 1:53 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
The hard coded page should always wins as i know 🙂
Feb 11, 2021, 2:23 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
learned that the hard way lol
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
getStaticProps
and
getStaticPaths
to 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
That’s great! I’m happy to hear it. Thanks for the update. 🙂
Feb 12, 2021, 3:40 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.

Was this answer helpful?