Watch a live product demo 👀 See how Sanity powers richer commerce experiences

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

The Sanity Composable Content Cloud is the 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.

Categorized in

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing