vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.51k stars 26.92k forks source link

Using rewrites with domain-specific i18n causes redirect to defaulLocale #36048

Open martinstepanek opened 2 years ago

martinstepanek commented 2 years ago

Verify canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT Wed Dec 29 18:09:17 UTC 2021
Binaries:
  Node: 16.14.2
  npm: 8.5.0
  Yarn: 1.22.10
  pnpm: N/A
Relevant packages:
  next: 12.1.5-canary.4
  react: 17.0.2
  react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

When having configured i18n with domain-specific localization for two domains and rewrites configuration, app always redirects to defaultLocale localization

Expected Behavior

Rewrites should work as described in documentation and shouldn't redirect to different locale.

To Reproduce

Clear installation with domain-specific i18n and rewrites setup like below.

next.config.js

  i18n: {
    locales: ['en', 'cs'],
    defaultLocale: 'en',
    localeDetection: false,
    domains: [
      {
        domain: 'example.com',
        defaultLocale: 'en',
      },
      {
        domain: 'example.cz',
        defaultLocale: 'cs',
      },
    ],
  },
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: '/test',
          destination: '/existingRoute',
        },
      ],
    }
  },

Visit example.cz domain and it immediately redirects you to example.com

fintand commented 2 years ago

Seeing a similar issue for this sample config:

const nextConfig = {
  i18n: {
    locales: ['en', 'cs'],
    defaultLocale: 'en',
  },
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: '/myapi/:path*',
          destination: '/api/:path*',
        },
      ],
    };
  },
}

https://localhost:3000/myapi/hello returns a 404

It looks to have been introduced in https://github.com/vercel/next.js/releases/tag/v12.1.1-canary.6 and most likely this commit: https://github.com/vercel/next.js/pull/33966

Babbili commented 2 years ago

@martinstepanek in next.config.js i18n configuration, try to remove localeDetection: false,

36048