Next.js Discord

Discord Forum

Dynamic Routes returning 404 in Production environments

Answered
dripszn posted this in #help-forum
Open in Discord
Avatar
1. Hello I am updating my website, and I'm having issues with my newly added dynamic route returning 404. It works totally fine 100% of the time locally which is part of the frustration here. However, once expo runs the GHA checks on PR commit and creates the preview. I get the following error shown in the screenshotted image. I want to highlight that the actual http response is 200 from the route, however I'm sure this is a false positive in that it likely is returning 200 along with a 404 html page in the body, since I was able to recreate the same JSON error locally if I specified an unknown URL.
2.
I have tried creating a URL helper to construct the URL with proper domain depending on the environment. Vercel exposes a VERCEL_URL variable that contains the current dev environment. My helper works, but I still get a 404 in any raised environment. Any help will be really appreciated. Considering trying a different web host if this persists beyond today.

currently on next version: 14.0.0

Also want to note that I only have NEXT_PUBLIC_SITE_URL set for production and not the other environments.
Image
Image
Image
Answered by dripszn
Ah, so I solved the issue here. It's largely based in common sense, and really just took some thought as to what I was really doing here.

For anyone who may be interested:

I was attempting to make a fetch call to my server, using a server component. NextJS does not resolve routes until build time, so the route does not exist when the code is being transpiled. I was making a call to the route in an attempt to retrieve my list of BlogPosts , and again the route does not exist yet. So the solution is to just call any functions directly rather than making an api request to the route. This all makes sense when you really think about it ha 😅 . Nonetheless my issue is solved, and maybe someone with more knowledge of NextJS can correct any gaps in my knowledge here, and provide a more thorough explanation.

Issue Resolved ✅ ck
View full answer

1 Reply

Avatar
Ah, so I solved the issue here. It's largely based in common sense, and really just took some thought as to what I was really doing here.

For anyone who may be interested:

I was attempting to make a fetch call to my server, using a server component. NextJS does not resolve routes until build time, so the route does not exist when the code is being transpiled. I was making a call to the route in an attempt to retrieve my list of BlogPosts , and again the route does not exist yet. So the solution is to just call any functions directly rather than making an api request to the route. This all makes sense when you really think about it ha 😅 . Nonetheless my issue is solved, and maybe someone with more knowledge of NextJS can correct any gaps in my knowledge here, and provide a more thorough explanation.

Issue Resolved ✅ ck
Answer