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 Use Breaks Locale Routing on / Path on Vercel if autodetected locale is not default locale #34274

Closed
neverether opened this issue Feb 12, 2022 · 4 comments
Labels
kind: bug Confirmed bug that is on the backlog Middleware Related to Next.js Middleware

Comments

@neverether
Copy link

neverether commented Feb 12, 2022

Run next info (available from version 12.0.8 and up)

/bin/sh: pnpm: command not found

    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 20.5.0: Sat May  8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64
    Binaries:
      Node: 14.17.5
      npm: 6.14.14
      Yarn: 1.22.5
      pnpm: N/A
    Relevant packages:
      next: 12.0.10
      react: 17.0.2
      react-dom: 17.0.2

What version of Next.js are you using?

12.0.10

What version of Node.js are you using?

14.17.5

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

When using automatic locale detection, and the detected locale is NOT the default locale, trying to route to another locale on / results in a failed prefetch error and doesn't complete routing.

In my case my locales are ["ja","en"] and my defaultLocale is ja. My browser language is set to english, and with the automatic locale detection, I'm taken to the /en route. When I try to route to the ja locale in yarn dev or yarn build && yarn start everything works fine. However when I deploy to Vercel I get:

Uncaught (in promise) Error: Failed to preflight request

And routing is cancelled.

My Japanese users with a japanese langauge setting have no issue moving between the two locales. and if I disable the automatic locale detection I will be taken to / with a ja locale as expected, and I can freely route between the two locales.

Expected Behavior

I'm expecting to not get a preflight failure from Vercel, and routing to complete successfully.

This might be related to #34199

To Reproduce

I've created a minimum reproducable repo here using create-next-app and the official example at: https://github.com/vercel/examples/tree/main/edge-functions/basic-auth-password

Repo/Commits on Github: https://github.com/sollective/next-locale-bug-repro/commits/main

I also have it hosted on Vercel here:
https://next-locale-bug-repro.vercel.app/en (Credentials are admin/password)
(Please set your primary language to English in browser)

@neverether neverether added the bug Issue was opened via the bug report template. label Feb 12, 2022
@balazsorban44 balazsorban44 added the Middleware Related to Next.js Middleware label Feb 13, 2022
@neverether neverether changed the title Middleware Use Breaks Locale Routing on / Path on Vercel if not on default locale Middleware Use Breaks Locale Routing on / Path on Vercel if autodetected locale is not default locale Feb 13, 2022
@balazsorban44 balazsorban44 added kind: bug Confirmed bug that is on the backlog and removed bug Issue was opened via the bug report template. labels Feb 17, 2022
@maggo
Copy link

maggo commented Feb 17, 2022

This happened to me as well, in particular the HEAD request to / is failing.
Removing the middleware file fixes the issue.

@javivelasco javivelasco added this to the Next.js Middleware GA milestone Feb 18, 2022
@youminkim
Copy link

Same issue here. I had to rewrite including locale. Otherwise, 404 error

   const localePrefix = locale === 'default' ? 'en' : locale;
   url.pathname = `/${localePrefix}/_sites/${hostname}`;
   NextResponse.rewrite(url)

kodiakhq bot pushed a commit that referenced this issue Apr 7, 2022
Fixes part of #34274

Navigating to `/` causes to redirect preflight request to a url of browser language like `/en`. 
This PR fixes to add the locale prefix always so that the redirect does not happen anymore and middleware can get a correct locale.

## Bug

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

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@nkzawa
Copy link
Contributor

nkzawa commented Apr 13, 2022

It should already be fixed by #35911

colinhacks pushed a commit to colinhacks/next.js that referenced this issue Apr 14, 2022
Fixes part of vercel#34274

Navigating to `/` causes to redirect preflight request to a url of browser language like `/en`. 
This PR fixes to add the locale prefix always so that the redirect does not happen anymore and middleware can get a correct locale.

## Bug

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

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: bug Confirmed bug that is on the backlog Middleware Related to Next.js Middleware
Projects
None yet
Development

No branches or pull requests

6 participants