Fallback rewrites with catch all route causes a JSON error
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64
Binaries:
Node: 16.10.0
npm: 7.24.0
Yarn: 1.22.17
pnpm: N/A
Relevant packages:
next: 12.1.7-canary.31
react: 18.1.0
react-dom: 18.1.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
I’m using rewrites with the fallback
option to rewrite after dynamic routes have been checked. This works fine during server rendering, but when the app rehydrates, a fetch is made to /produkte.json
, causing the 404 page to be rendered and sent to the client (as the route doesn’t exist), which cannot be interpreted as JSON, therefore causing the app to crash.
Error in the console
The 404 page’s HTML returned for the produkte.json request
Expected Behavior
The application “follows” rewrites when fetching the JSON content (or doesn’t fetch it at all) and doesn’t crash.
To Reproduce
- Clone the repo: https://github.com/johnnyomair/next-rewrites-fallback-bug-demo
- Install dependencies
npm install
- Start application
npm run dev
- Click on “Produkte” link on homepage
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:9 (5 by maintainers)
Top Results From Across the Web
React-router URLs don't work when refreshing or writing ...
The historyApiFallback is what fixed this issue for me. Now routing works correctly and I can refresh the page or type in the...
Read more >Client-Side Routing In Next.js - Smashing Magazine
As it is with the catch-all route, the route /printed-books will throw a 404 error unless you provide a fallback index route.
Read more >Configure Azure Static Web Apps - Microsoft Learn
Learn to configure routes, enforce security rules, and global settings for Azure Static Web Apps.
Read more >Routing in Next.js – How to Set Up Dynamic Routing with Pre ...
fallback set to true doesn't automatically lead to a 404 error page whenever the user tries to access a path that doesn't exist...
Read more >How to Fix the Invalid JSON Response Error in WordPress
What causes the WordPress invalid JSON response error? ... If you see "Updating failed. The response is not a valid JSON response," it...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can see that as well, taking a closer look!
Sorry for the late response.
The reason we’re not using
afterFiles
is because our catch-all may specify paths that somewhat collide with the rewrites. For instance, it may return a path/produkte/neue-produkte
. This page should not be rewritten to/products/neue-produkte
. Only paths that do not match a path specified in the catch-all should be rewritten.I can see why this might not work due to the client not having a list of all paths specified in the catch-all. As we have found a workaround (see https://github.com/vercel/next.js/issues/37510#issuecomment-1165493949), I’m fine with that. Thanks for the effort! 🙂