question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fallback rewrites with catch all route causes a JSON error

See original GitHub issue

Verify 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

Screen Shot 2022-06-07 at 13 53 27

The 404 page’s HTML returned for the produkte.json request

image

Expected Behavior

The application “follows” rewrites when fetching the JSON content (or doesn’t fetch it at all) and doesn’t crash.

To Reproduce

  1. Clone the repo: https://github.com/johnnyomair/next-rewrites-fallback-bug-demo
  2. Install dependencies npm install
  3. Start application npm run dev
  4. Click on “Produkte” link on homepage

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
wyattjohcommented, Jun 29, 2022

I can see that as well, taking a closer look!

0reactions
johnnyomaircommented, Aug 1, 2022

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! 🙂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found