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
Fix redirect url for prefixing the default locale #33762
Conversation
We want to prefix the default locale to the current path (`/path`), not the current href (`http://domain.tld/path`).
@@ -177,7 +177,7 @@ export function middleware(request: NextRequest) { | |||
request.nextUrl.locale === 'default' | |||
|
|||
return shouldHandleLocale | |||
? NextResponse.redirect(`/en${request.nextUrl.href}`) | |||
? NextResponse.redirect(`/en${request.nextUrl.pathname}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about when there is a query string?
I think it should be something like
const url = new URL(request.nextUrl)
url.pathname = `/en${url.pathname}`
NextResponse.redirect(url)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @styfle! I am thinking about not to create a new object, but not sure if there is any pitfall doing this, something like
request.nextUrl.pathname = `/en${request.nextUrl.pathname}`
NextResponse.redirect(`/en${request.nextUrl.href}`)
The previous fix redirect to the correct default locale url, but would drop the query string. This commit preserve the original query string by redirecting to the full href.
The recommended code for `_middleware.ts` does not work in prod, but does work locally. You need to use `request.nextUrl.pathname` to properly redirect from what I can tell. You also need to have a quick helper function to strip off the `/default` locale at the start of the pathname as we are providing `/en` as a fallback locale. FWIW - I am pretty new to NextJS. Someone with more experience should probably review this suggestion before merging it. What I can tell you however is that the code as it is in `_middleware.ts` works locally but breaks in prod. To test this out use the code and navigate to `https//www.mysite.com` - it will work as expected on the root url, as this matches `nextUrl.href`. Now try navigating to `https//www.mysite.com/about` and you will be redirected to `https://www.mysite.com/en/https://www.mysite.com/about`. ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` Closes: #33762 Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
# Conflicts: # docs/advanced-features/i18n-routing.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
The recommended code for `_middleware.ts` does not work in prod, but does work locally. You need to use `request.nextUrl.pathname` to properly redirect from what I can tell. You also need to have a quick helper function to strip off the `/default` locale at the start of the pathname as we are providing `/en` as a fallback locale. FWIW - I am pretty new to NextJS. Someone with more experience should probably review this suggestion before merging it. What I can tell you however is that the code as it is in `_middleware.ts` works locally but breaks in prod. To test this out use the code and navigate to `https//www.mysite.com` - it will work as expected on the root url, as this matches `nextUrl.href`. Now try navigating to `https//www.mysite.com/about` and you will be redirected to `https://www.mysite.com/en/https://www.mysite.com/about`. ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` Closes: vercel#33762 Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
We want to prefix the default locale to the current path (`/path`), not the current href (`http://domain.tld/path`). ## Documentation / Examples - [x] Make sure the linting passes by running `yarn lint` Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
We want to prefix the default locale to the current path (
/path
), not the current href (http://domain.tld/path
).Documentation / Examples
yarn lint