next.js: Chunk Load Error using dynamic routes ([lang])
Link to the code that reproduces this issue
https://github.com/leof240/chunk_load_error_nextauth-nextintl
To Reproduce
- build the project
- deploy on prod server
- go to a dynamic locale page like http:site/en
- the page is showing an error on chunk load error
Current vs. Expected behavior
Current:
In development the site is working as expected.
in a production build and production environment, the site is throwing a Chunk Load Error on the page js file behind
the buttons on the page do not work
Expected: The expected behaviour is to show a page with 2 buttons for signin and sign out. when clicking the signin button, the session is updated and after a page refresh is shown from the layout and the page. it’s also showing the page title translated into different languages for the /en, /fr, / it
Provide environment information
Node 20.1
npm v9.8.1
Which area(s) are affected? (Select all that apply)
Not sure, App Router, Internationalization (i18n), Middleware / Edge (API routes, runtime)
Which stage(s) are affected? (Select all that apply)
Vercel (Deployed), Other (Deployed)
Additional context
the “next” package is updated to canary version ( 24 / 02 / 2024)
cannot reproduce the error on development environment with npm run start
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 16
Hello,
I updated the code in the branch “Fixed”.
below you can find the added snippet:
let reqUrl = req.url .replaceAll(/[/g, ‘%5B’) .replaceAll(/]/g, ‘%5D’) .replaceAll(‘%5b’, ‘%5B’) .replaceAll(‘%5d’, ‘%5D’) ;
I was able to fix the error. It’s related to the previous errors on dynamic routs. There was a problem in the encoding url in server.js
The url was all lower case having the encoded square brackets lowercase therefore not properly encoded. (%5b instead of %5B)
I added a replace statement before the url processing. I’ll post the code later for awareness
@paigekim29 Before the fix My matcher config looked like this:
This basically acts as a reverse matcher and ignores routes that match the regex
But in order to catch requests for static assets and rewrite them I had to remove the portion for “_next/static” and my matcher now looks like this:
I would have preferred to fix this whole issue via The Nginx and/or Apache config, but in my case that wasn’t an option due to our host limiting access to those.
I resolved my problem by configuring my apache web server reverse proxy like so
AllowEncodedSlashes NoDecode — allows URLs like %40sidebar (encoded @) ProxyPass / http://127.0.0.1:3000/ nocanon — prevents Apache from decoding percent-encoded URLs before proxying to Node/Next.js
@HermanDhillon How did you configure the middleware matcher? I’m experiencing this issue with the App Router on v15.3.4.
I ran into a similar issue when upgrading our company’s app from next 13.4.12 pages router to 15.3.3 pages router . But our issue was somewhat related to our NGINX reverse proxy, however I believe Next should have handled it.
Our app makes use of a catch all dynamic route like:
[...uid]When a chunk is created for this route it is saved with a file name like this: ‘[…uid]-4d908551bf6ada7d.js’ in
.next/static/chunks/pagesNow when the browser makes a request to the server to GET these assets, the URL is percent encoded:
When this request makes it way to NGINX , NGINX takes that percent encoding and turns something like
%5B...uid%5Dinto%5b...uid%5dand then proxies that to our Next app. Next then doesn’t recognize that request URL due to case sensitivity, and returns a 404.At first I believed this to be a NGINX issue but realized that percent encoding is not case sensitive, per RFC 3986 . So it was still technically correct and Next should be able to handle.
My Solution was very similar to @leof240 's. In my Next middleware I check the request URL for lowercase encoding and replace it with uppercase encoding then rewrite.
I also had this issue when trying to upgrade to the the latest patch version of 13.4, and after reading through this issue: https://github.com/vercel/next.js/issues/54008#issuecomment-1734545438 . I believe the issue was introduced in 13.3.13
Thanks, @leof240 ! Got the same problem with an @ in parallel routes, like app/@modal/… Has always been crashing on production in Google App Engine Standard Environment but has been working fine locally on dev. So I adapted your solution with the following extra line:
That made it work. Leaving it here for anyone who has a Chunk Load Error with parallel routes on Google App Engine.
@leof240 Hey Leof 👋
Any chance you got that code handy? I’m currently experiencing an almost identical issue except my uri is getting encoded twice around my slug. So, I’m getting %255B instead of %5B… incredibly frustrating.
Using Next v13.5.6.
Appreciate any help!