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

Middleware rewrite doesn't work with i18n #30897

Closed
leethree opened this issue Nov 3, 2021 · 2 comments · Fixed by #31174
Closed

Middleware rewrite doesn't work with i18n #30897

leethree opened this issue Nov 3, 2021 · 2 comments · Fixed by #31174
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware.

Comments

@leethree
Copy link

leethree commented Nov 3, 2021

What version of Next.js are you using?

12.0.2

What version of Node.js are you using?

14.17.6

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

next start

Describe the Bug

Next 12 Middleware rewrite doesn't handle locale correctly with i18n routing enabled. For example, if we rewrite /test to /fr/test using middleware, it will still show /test with the default locale because the locale info is lost during rewrite.

So you can't do this:

// in middleware
req.nextUrl.locale = detectedLocale;
return NextResponse.rewrite(req.nextUrl);

or equivalently this:

// in middleware
req.nextUrl.pathname = `/${detectedLocale}${req.nextUrl.pathname}`
return NextResponse.rewrite(req.nextUrl);

Expected Behavior

The locale info should be parsed when rewriting from middleware.

To Reproduce

I modified the vercel i18n example to demonstrate the issue.

See: https://github.com/leethree/next-i18n-rewrite

Supposedly you have fr as your browser locale. When visiting the home page (http://localhost:3000/), it should show your current locale as detected:

image

(ignore the flag, note the "locale: fr" below that.)

But actual behaviour:

image

@leethree leethree added the bug Issue was opened via the bug report template. label Nov 3, 2021
@leethree
Copy link
Author

leethree commented Nov 3, 2021

From my debugging, it seems like the locale is stripped fromresolvedUrlPathname in the server code while handling rewrite:

let resolvedUrlPathname = (req as any)._nextRewroteUrl
? (req as any)._nextRewroteUrl
: urlPathname
urlPathname = removePathTrailingSlash(urlPathname)
resolvedUrlPathname = normalizeLocalePath(
removePathTrailingSlash(resolvedUrlPathname),
this.nextConfig.i18n?.locales
).pathname

@timneutkens timneutkens added the Middleware Related to Next.js Middleware. label Nov 4, 2021
@kodiakhq kodiakhq bot closed this as completed in #31174 Nov 9, 2021
kodiakhq bot pushed a commit that referenced this issue Nov 9, 2021
Fixes #30897

This PR fixes the linked issue where rewrites are not being applied for locale. It adds the corresponding test but also, as it was added in debugging process, it introduces a helper to read/write into the `request` object.

We are currently writing directly into the request by casting to `any` and then using flags like `_nextRewrote`. Instead, this PR puts all of this metadata under a symbol so it is not directly accessible. This also allows to have a single place where all of this metadata is listed so we can add comments describing the purpose of each flag.

In the same way, there is metadata written in the querystring. This is adding some types for it in order to throw some visibility on where is this metadata accessed. In an upcoming PR we can move all of it to the `request` object if possible to simplify the system.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
Fixes vercel#30897

This PR fixes the linked issue where rewrites are not being applied for locale. It adds the corresponding test but also, as it was added in debugging process, it introduces a helper to read/write into the `request` object.

We are currently writing directly into the request by casting to `any` and then using flags like `_nextRewrote`. Instead, this PR puts all of this metadata under a symbol so it is not directly accessible. This also allows to have a single place where all of this metadata is listed so we can add comments describing the purpose of each flag.

In the same way, there is metadata written in the querystring. This is adding some types for it in order to throw some visibility on where is this metadata accessed. In an upcoming PR we can move all of it to the `request` object if possible to simplify the system.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants