next.js: i18n: Wrong locale on getServerSideProps when using next.config.js rewrite

Verify canary release

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

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
    Binaries:
      Node: 18.14.2
      npm: 9.5.0
      Yarn: 1.22.19
      pnpm: N/A
    Relevant Packages:
      next: 13.4.14-canary.5
      eslint-config-next: 13.4.12
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4
    Next.js Config:
      output: N/A

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

Data fetching (gS(S)P, getInitialProps), Internationalization (i18n), 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/routing-i18n-bug-gfc94t?file=%2Fnext.config.js%3A1%2C1

To Reproduce

If you click on “Link to Page1 PT” you will see that both props and router will output the correct locale, pt.

However, if you click on “Link to Pagina1 (Redirect to Page1) PT”, props will be the defaultLocale (which is en), whereas router be the expected locale, pt.

The only difference between both links is that the second one works via a next.config.js rewrite property.

Describe the Bug

getServerSideProps is not getting the correct locale if the route was using a next.config.js rewrite.

This was working as expected up until and including version 13.2.5-canary.16. On version 13.2.5-canary.17 it has stopped working and has not being fixed yet (example on CodeSandbox is using the latest version 13.4.14-canary.5).

Expected Behavior

getServerSideProps should have the correct locale value regardless of the routing strategy.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 7
  • Comments: 19

Most upvoted comments

Same as:

https://github.com/vercel/next.js/issues/51923 https://github.com/vercel/next.js/issues/49883 https://github.com/vercel/next.js/issues/51695

This has been a bug for 4+ months, and everyone that uses i18n and rewrites/redirects is at a standstill… 13.2.4 is the last working version.

I think I’m running into this on 14.2.4. Found this by searching.

I have a link like /fr/path/to/stuff or /de/path/to/stuff. When I visit it directly, the locale in the getServerSideProps context is fr or de, respectively.

But clicking a link on the page, whose destination is e.g., href="/fr/path/to/otherstuff", the locale provided to getServerSideProps is the default locale (en).

This seems to be the most relevant issue. So just pitching a vote here 😅

I just discovered this, so haven’t yet had time to see if there are open PRs related to this.

UPDATE: As mentioned on related tickets, if I delete the middleware.ts file, the behavior seems to be resolved.

Wut.

Yeah you’re right @raRaRa, leaving out the matcher is not an optimal solution.

@raRaRa I had to remove matcher from Middleware and use Conditional Statement instead.