-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
[Internationalization] Redirect with { locale: false }
and dynamic isn't working properly.
#19690
Comments
Any help in this case? |
{ locale: false }
and dynamic isn't working properly.{ locale: false }
and dynamic isn't working properly.
UpdateI upgrade the demo project to |
@raulfdm I was having a similar issue. Just out of interest, do you have a Note - I had to refactor some of my pages to use Next's dynamic routing instead of doing rewrites using legacy routes approach. See https://vercel.com/docs/configuration#project/routes for more info. |
Hey @ramiyahya , thanks for answering. No man, I don't have routes configured. Where do you put your redirects? In you |
I'm also running into this - exact same behavior as described. Going to spend some time poking around the codebase tomorrow, hoping the fix isn't too hard 🤞 |
Hey @IanMitchell ... thanks for sharing. I've been checking every canary release but it seems nobody fix this problem yet. I'm not sure if it's that complex or it has low prio or even if my PR is clear enough so if you can review that, the examples it would be great. |
I think this might also affect rewrites in addition to redirects since I'm having a similar issue: #21096 |
Despite the traction it does not seem that the issue will be addressed any time soon. Is there a recommended workaround to be able to "cleanly" deal with this? Thank you in advance for your help :) |
Any updates? |
For those who want to redirect to a different route: It seems that even if "locale": "false" is set, the Unfortunately this doesn't help OP with redirecting to the exact same route with the prefix added, because after the first redirect, it matches the same rule again (so it matches both with and without the prefix), and it goes into a redirect loop. |
It's still a bug. If you're experiencing this issue, here's a temporary workaround to fix it: type RequestInfo = {
pathname: string,
nextPathName: string,
basePath: string,
locale: string,
};
function getRequestInfo(request: NextRequest): RequestInfo {
const {locale} = request.nextUrl;
let urlPathName = new URL(request.url).pathname;
let {pathname: nextPathName, basePath} = request.nextUrl;
if (locale !== '' && basePath === '' && urlPathName === `/${locale}${nextPathName}`) {
/*
There is a known bug in Next.js when using localized routes with a basePath.
For example, if the locale is 'pt' and the basePath is '/app', it leads to:
- Actual URL: /app/pt
- request.url = /pt/app/pt
- request.nextUrl.pathname = /app/pt
- request.nextUrl.basePath = (empty)
However, it should be:
- request.url = /app/pt
- request.nextUrl.pathname = /
- request.nextUrl.basePath = /app
Interestingly, if the basePath starts with the locale (e.g., '/pt/app'), it works as expected.
Bug report:
https://github.com/vercel/next.js/issues/19690
*/
const escapedLocale = escapeRegExp(locale);
const match = new RegExp(`^/${escapedLocale}((/.+?)/${escapedLocale}(.*))`).exec(urlPathName);
if (match !== null) {
basePath = match[2] ?? basePath;
urlPathName = match[1] ?? urlPathName;
nextPathName = (match[3] === '' ? '/' : match[3]) ?? nextPathName;
}
}
return {
pathname: urlPathName,
locale: locale,
basePath: basePath,
nextPathName: nextPathName,
};
} |
Bug report
Describe the bug
Redirect with
{ locale: false }
isn't working properly.Context
Before describe the bug, I need to give you some context about what problem I'm trying to solve.
I recently switch my website default locale from
pt
toen
. It means that some old URLs I had localized, now would be inverted, for example:The problem
What's is happening now is I can't just setup this redirect via
next.config.js
or viavercel.json
(but I'll focus on next config).The way I would try to solve that is setting up something like this:
This won't work and that's comprehensible. I assume when I try to access
/blog/e-o-coronavirus-hein
, since my browser is in English, it'll try to resolve the request locale to/en
. But the stranger thing is that the redirect works but it concatenates everything and I'm being redirected to:/en/pt/blog/e-o-coronavirus-hein
.That's weird but makes sense because I'm embedding the locale in the path.
In the docs it says we can turn off the locale process, that should do the job, right?
But now if I try to access
/blog/e-o-coronavirus-hein
, instead being redirected to/pt/blog/e-o-coronavirus-hein
, I just reach 404.I assume that somehow it's thinking that's is a valid route (because I use
pages/blog/[slug].js
) and it does not consider the redirect setup.Another problem (which I can open another issue if you want to) is when I try to redirect from a url which even does not exist from an existing one:
In this case I also get 404.
To Reproduce
I prepare a demo site with some deeper explanation how to test this problem but:
Defective redirects
sectionExpected behavior
As a dev user, I want to be able to setup a redirect from a
/blog/some-post
to/<locale>/blog/some-post
.Screenshots
Not applicable
System information
Pop!_OS
any
10.0.3-canary.3
12.19.1
Vercel
Additional context
Update
rewrites
The text was updated successfully, but these errors were encountered: