Skip to content
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

Merged
merged 4 commits into from Feb 6, 2022

Conversation

lancechentw
Copy link
Contributor

@lancechentw lancechentw commented Jan 28, 2022

We want to prefix the default locale to the current path (/path), not the current href (http://domain.tld/path).

Documentation / Examples

  • Make sure the linting passes by running yarn lint

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}`)
Copy link
Member

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)

Copy link
Contributor Author

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.
@ijjk ijjk mentioned this pull request Feb 5, 2022
1 task
@kodiakhq kodiakhq bot closed this in #33123 Feb 6, 2022
kodiakhq bot pushed a commit that referenced this pull request Feb 6, 2022
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>
@ijjk ijjk reopened this Feb 6, 2022
# Conflicts:
#	docs/advanced-features/i18n-routing.md
Copy link
Member

@ijjk ijjk left a 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!

@kodiakhq kodiakhq bot merged commit 93fa129 into vercel:canary Feb 6, 2022
natew pushed a commit to natew/next.js that referenced this pull request Feb 16, 2022
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>
natew pushed a commit to natew/next.js that referenced this pull request Feb 16, 2022
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>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants