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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced-features/i18n-routing.md
Expand Up @@ -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}`)

: undefined
}
```
Expand Down