-
Notifications
You must be signed in to change notification settings - Fork 26.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Server side locale always default in dynamic routes when a middleware is present #54217
Comments
We are facing the same issue using a global middleware file on the project. The locale passed to the We are using the locale for a subsequent request inside the
next.config.js
|
Having the same issue when middleware file present. Problem is since v13.4.13. |
We are facing similar issue - aralroca/next-translate#1131 |
(edit: removed suggested workaround which did not avail the issue) |
looks like I have the same issue. I use middleware for default locale prefix and my locale inside getStaticProps is 'default' on client-side navigation. If I open a page by direct link locale is correct. I found that this issue starts from 13.4.13 version. |
Same issue here. We'll revert to 13.4.12 for the time being, let us know if we can provide any more info to help troubleshooting. |
We also ran into a similar problem when updating Next.js which broke our internationalization strategy. We´ll also keep using 13.4.12 for the moment. |
Yup, after updating from 13.4.12 to 13.4.13+, the internationalization strategy breaks. getServerSideProps gets "default" as the passed locale. Hopefully this gets looked into soon. |
Wow, this bug was annoying me today. Downgrading to 13.14.12 fixed it. I use two languages, 'is' and 'en', where 'en' is the default one. If I open /is/ then the page opens in Icelandic. If I open it dynamically with Link, then it uses the default locale. Refreshing the page makes it work. I wonder if this is also a bug on prod? |
In my case I have a middleware with a matcher of specific URLs. The middleware is not even run for the pages where this issue happens. But removing the middleware fixes it. |
I wonder if this PR fixes it: #54357 |
Any updates on this issue? According to nextjs docs, "default" should be added for prefixing locale. This resolved it for us: |
Still broken in 13.5.1 |
Upgrading to 13.5.1 breaks the internationalization in a new way. The app gets stuck in a redirect loop when trying to navigate to the app's root ("/" or "/en" or "/et"). Seems to me that the automatic locale prefixing is still broken and 13.4.12 is still the last working version for me. Either some logic was changed and the Docs is not updated or it's just bugging out still.
|
Facing the same issue. Upgraded to 13.5.2 from 13.3.1. Server always has the default locale when routing via a NextLink. Reloading the page does give the correct locale. |
It's really worrying that this has been an issue for such a long time without Vercel taking any action. This breaks so many websites that support multiple locales + have middleware file present (very common as well). |
We have the same issue as well. Downgrading to 13.3.1 for now. |
This comment has been minimized.
This comment has been minimized.
Same issue here, upgrading > 13.4.19 breaks the app with the 307 redirect loop. |
Same issue. I couldn't fathom this to be a bug, since it seems to basic and so easily (regression-)testable at Vercel's end. So I started this discussion. No responses yet, at the time of writing, unfortunately. I spent another 2 hours stepping through internal Next.js code, and I couldn't find any place where the locale definitely gets reset to default. Even knowing the middleware mechanism is the culprit, it still didn't help to figure out how, where, and why this is happening. I'm also very interested to how this could've slipped through QA at Vercel. Since i18n seems like a pretty important feature, I would assume it goes through some sort of testing. Maybe not good enough then. |
@thany I would also assume such a feature has the proper tests in place. It might be because the app router doesn't provide out of the box i18n routing that this slipped through. I believe the app router is the main focus, which is quite clear as no one from vercel or the next team has replied about this issue yet. |
Still an issue in 13.5.4 - reverting to 13.4.12 works. |
We updated project to use next v13.5.4, and now both in middleware and page |
I would just do all the locale functionality manually, which will also help you migrate to app router in the future. |
@leerob it has been more than 4 months since this issue was reported and there has been no communication from the team whatsoever. |
I guess someone using a payed vercel product needs to open a support ticket or something to increase priority |
Would love to see an update on this so we can upgrade to v14 for now we are pinned to v13. |
I don't experience the "Server side locale always default in dynamic routes when a middleware is present" error when using next 14.1.0 in the minimal repo that reproduces the error: https://github.com/XavierLasierra/next-dynamic-routes-bug But I do experience the redirect issue mentioned above unless I set I've performed this test:
And all pages seem to show the correct locale. Maybe someone who still experiences this error, even with next 14.1.0, can update the minimal repo to reproduce the error? |
I just tried 14.1.0. Client side navigation is working now without reload. I use localeDetection and I don't experience the redirect issue. However, the locale in getStaticProps is always the default locale. |
We are facing the same issue with |
@leerob This issue has a reproduction, what needs to happen to get a response from Vercel? |
Can't update our project higher than 13.4.12 cause of this localeDetection/middleware problems((( |
The problem is still relevant, and it seems that the development team has no plans to fix it. |
Just want to chime in and add that we also face this issue in a similar setup. Also blocking us from updating to > 13.4.12. |
Might be fixed by #62606 |
I think this issue should be closed. The bug as stated in the original issue: "Server side locale always default in dynamic routes when a middleware is present" using the original reproduction repo but updated to the latest next (14.1.4) does not show the bug. With next 14.1.4 the endless redirect mentioned in the thread is also gone. I tried adding a Static Site Generation (SSG) page using There are other bugs related to i18n such as:
But the particular bug discussed in this thread seem to be solved. I suggest that anyone that still sees this issue update the original reproduction repository https://github.com/XavierLasierra/next-dynamic-routes-bug or create a new, minimal one, that demonstrates the issue and submits a new issue or updates this one if you think its the same issue. Please remember that router (app/pages), middleware (present or matching/not present or matching), runtime (node/edge), i18n (on/off), server (npm run dev/npm run build + npm start/vercel/other) can all affect the behavior. If you see this problem in your app then there may be something in it that differs from the minimal reproduction. |
Problem actually still occurs when middleware has a config object with a matcher array. I updated the reproduction repo with such a middleware file and last Next version (14.1.4), the issue still exists. export const config = { matcher: ["/foo"] };
export async function middleware(req) {} Having found this, I am going to try replacing the config matcher with conditions inside the middleware (https://nextjs.org/docs/app/building-your-application/routing/middleware#conditional-statements), it should do the trick. |
This comment has been minimized.
This comment has been minimized.
@Mattgic I tested your updated repo and can confirm that with the additional middleware config the bug is still present. Here is an updated bug report using the new template: Link to the code that reproduces this issuehttps://github.com/Mattgic/next-dynamic-routes-bug To Reproduce
Current vs. Expected behaviorAs I click the link I expect the locale to be Reloading the page correctly shows the locale as Provide environment informationOperating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
Available memory (MB): 16384
Available CPU cores: 12
Binaries:
Node: 18.18.2
npm: 9.8.1
Yarn: 1.22.22
pnpm: 8.15.5
Relevant Packages:
next: 14.3.0-canary.9 // Latest available version is detected (14.3.0-canary.9).
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: N/A
Next.js Config:
output: N/A Which area(s) are affected? (Select all that apply)Internationalization (i18n), Middleware / Edge (runtime), Routing (next/router, next/link) Which stage(s) are affected? (Select all that apply)next dev (local), next build (local), next start (local) Additional contextThe bug seems to be triggered by pages router + i18n + middlware present with matcher config + dynamic route. The example repo demonstrates the bug using a Server Side Rendered page but the bug is also present for a Static Site Generated page using |
We have the same issue on our project (Next.js v14.2.4). Almost a year since the bug appeared, why is nobody fixing it? P.S. I have reported the bug to "Pro Support" using my corporate Vercel account, but they said they can't help me with my Next.js problems. |
I have tried the same, but enabling So you'll have to implement rewrites inside of your middleware. Here is how I did that (hope it'll help): import { compile, match } from 'path-to-regexp'
// Copy of the array from `next.config.js`
const REWRITES = [
{
source: '/:type(resume|cover-letter|cv)-templates',
destination: '/templates/:type',
},
{
source: '/@:path',
destination: '/my/:path',
}
]
export default async function middleware(req: NextRequest) {
const url = req.nextUrl
// Return static files right away
if (url.pathname.startsWith('/_next/static') || url.pathname.startsWith('/_next/image')) {
return NextResponse.next()
}
// Perform rewrites
for (const rewrite of REWRITES) {
const fromPath = match(rewrite.source)
const result = fromPath(url.pathname)
if (result) {
const toPath = compile(rewrite.destination)
url.pathname = toPath(result.params)
return NextResponse.rewrite(url)
}
}
// ... Put the rest of your middleware here ...
return NextResponse.next()
} |
I thought only Next.js is abandoned but it seems the company is abandoning its customers too. |
Hi everyone, We apologize for the length it has taken to respond to this issue. I was not able to reproduce the issue when testing the CleanShot.2024-06-23.at.11.07.41.mp4Let me know if you are able to confirm this or are seeing anything differently |
I updated my repro repo to latest Next canary, bug still exists as previously said when middleware has a matcher. |
This is the matcher I used in particular, and I can't replicate.
|
@samcx you should be able to replicate it with the middleware CORS example, or with @Mattgic's repro. export const config = {
matcher: '/api/:path*',
} |
@tjcampbell Using @Mattgic's CleanShot.2024-07-01.at.13.49.20.mp4 |
You are reproducing it. You pick English, you go to the dynamic page and it shows up as |
@thansk Oh my bad, I see your point, I was just looking at the URL Interesting, it must be related to the matcher |
@tjcampbell @thansk What's interesting, is I'm only seeing this locally (with
|
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000 Binaries: Node: 18.8.0 npm: 8.18.0 Yarn: 1.22.19 pnpm: N/A Relevant Packages: next: 13.4.18-canary.0 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: N/A Next.js Config: output: N/A
Which area(s) of Next.js are affected? (leave empty if unsure)
Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)
Link to the code that reproduces this issue or a replay of the bug
https://github.com/XavierLasierra/next-dynamic-routes-bug
To Reproduce
/en
)next/link
Describe the Bug
When doing client side navigation to a dynamic route having a
middleware.js
file, the locale will always be the default one.middleware.js
file.Expected Behavior
The locale will be consistent in dynamic routes.
Which browser are you using? (if relevant)
Chrome 115.0.5790.170
How are you deploying your application? (if relevant)
No response
The text was updated successfully, but these errors were encountered: