Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Redirect by middleware works incorrect for site navigation via Link (with locale change) #52316

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

Open
1 task done
oleggrishechkin opened this issue Jul 6, 2023 · 5 comments
Labels
bug Issue was opened via the bug report template. Linking and Navigating Related to Next.js linking (e.g., <Link>) and navigation. stale The issue has not seen recent activity.

Comments

@oleggrishechkin
Copy link

oleggrishechkin commented Jul 6, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 21:00:17 PST 2023; root:xnu-8796.101.5~3/RELEASE_X86_64
    Binaries:
      Node: 16.15.1
      npm: 8.11.0
      Yarn: 1.22.11
      pnpm: N/A
    Relevant Packages:
      next: 13.4.9-canary.2
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.5.5
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

Internationalization (i18n), Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

https://codesandbox.io/p/sandbox/laughing-northcutt-wmc65k?file=%2Fpages%2Findex.tsx%3A17%2C11

To Reproduce

  1. Open /fr locale
  2. Click to the link "Open to the OnlyEnPage" (you should open a Link to the page which should be redirected)
  3. Check middleware logs

Describe the Bug

When you have a Link to the page which should be redirected, you can't use next redirects but you can use middleare to perform redirect.

In next 13.3+ versions if you navigate to a page with locale redirect redirect not happened correctly. In reproduction page will be show correctly (with correct locale) but an url will be incorrect - with wrong locale. Middleware logs a bad data - pathname is "/fr/en/only-en-page" with locale "en"

I have an Error: Invariant: The detected locale does not match the locale in the query. Expected to find 'en' in '/fr/en/only-en-page' but found 'fr'} error in my case and url "/fr/en/only-en-page"

Expected Behavior

  1. Url should be shown correctly - "/en/only-en-page"
  2. Middleware should have correct data - pathname should be "/only-en-page" and locale "fr" and "/only-en-page" with locale "fr" after redirect.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@oleggrishechkin oleggrishechkin added the bug Issue was opened via the bug report template. label Jul 6, 2023
@github-actions github-actions bot added the Linking and Navigating Related to Next.js linking (e.g., <Link>) and navigation. label Jul 6, 2023
@oleggrishechkin oleggrishechkin changed the title Redirect in middleware works incorrect for site navigation via Link (with locale change) Redirect by middleware works incorrect for site navigation via Link (with locale change) Jul 6, 2023
@skworden
Copy link

In it's current state, we also found middleware doesn't work with client side routing including route interception (i.e. login modal example). I think this issue an the route interceptions could be related.

@JesseKoldewijn
Copy link
Contributor

Seems to be an issue in general and not specific to i18n related routing + interception.

@tom-bart
Copy link

I'm experiencing the same error in 13.4.8 when working with i18n.

Additionally even if I set

localeDetection: false

This still doesn't go away. Quite a critical bug which prevents us from using NextJS on a big commercial site.

Is there any update on this?

@oleggrishechkin
Copy link
Author

I avoid this behaviour by manually replace Link href to redirect destination in custom Link wrapper component. Next can't resolve redirects by client-side navigation via Link, so I made redirects by myself. It's also better for SEO to have direct links without redirects.

@nextjs-bot
Copy link
Collaborator

This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

@nextjs-bot nextjs-bot added the stale The issue has not seen recent activity. label Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Linking and Navigating Related to Next.js linking (e.g., <Link>) and navigation. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

5 participants