vercel / next.js

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

[i18n] Domain Routing: Only default locale domain should trigger automatic locale detection #25896

Open jstcki opened 3 years ago

jstcki commented 3 years ago

What version of Next.js are you using?

10.2.3

What version of Node.js are you using?

14

What browser are you using?

All of them

What operating system are you using?

macOS

How are you deploying your application?

Other platform

Describe the Bug

When I link from the index page to another locale with <Link href="/" locale="fr"> and I'm using domain-based i18n routing, I always get redirected back to the default locale's domain.

E.g. linking from foo.com to fr.foo.com does not work and I end up on foo.com again.

Or when I access fr.foo.com directly, I get redirected to foo.com.

This does not happen with sub-path routing.

I assume this is because of automatic locale detection and that all domains without sub-path are always (incorrectly) treated as "/" thus triggering it.

Expected Behavior

To be consistent with how sub-path routing works, locale redirects should only be triggered on the domain that matches the default locale.

E.g. if my Accept-Language is fr

Sub-path routing:

Domain routing:

To Reproduce

Sorry, I can't add a repro at this point because it's tedious to set up with different domains.

nickrttn commented 2 years ago

An example of the ~bug~ behavior can be found here: https://changingchildhood.unicef.org/

In the top right is a language switcher. The French and Spanish locales are configured with their own domain (so we are mixing subpath and domain routing, further complicating things). When I attempt to navigate to the French version of the site with a browser set to English, I get immediately redirected back to the English URL.

This has been reported as a bug by our users as well.

jstcki commented 2 years ago

@nickrttn FYI, you can "fix" or work around this by disabling the automatic locale detection in next.config.js. The underlying issue that automatic locale detection + domain based routing doesn't work properly still persists of course.

nickrttn commented 2 years ago

Sadly, we're currently relying on automatic locale detection to redirect our users to the right domain or subpath. However, if they do choose to navigate to another locale's domain or subpath, the automatic locale detection shouldn't be kicking in.

As an additional confounding factor, if you navigate deeper into the site and then change locales, you will not get redirected back to the domain that matches your browsers' language. Eg. try changing to French or Spanish here: https://changingchildhood.unicef.org/stories

adroste commented 2 years ago

What about setting the NEXT_LOCALE cookie (see: https://nextjs.org/docs/advanced-features/i18n-routing#leveraging-the-next_locale-cookie) when a user hit's a language specific subpath/domain ?

jsinclair-indeed commented 2 years ago

@adroste unfortunately NEXT_LOCALE isn't a super override.

The rules of NEXT_LOCALE:

  1. Does not take affect if localeDetection is false
  2. The default domain locale takes precedence

As per the source code in get-locale-redirect.ts.

patrick-mcdougle commented 1 year ago

The NEXT_LOCALE cookie can't be a super override because you can't set it on the destination domain until after you've been redirected away from it. There needs to be a force_locale query string parameter that sets the cookie upon landing BEFORE doing the detection check.