QuiiBz / next-international

Type-safe internationalization (i18n) for Next.js
https://next-international.vercel.app
MIT License
1.2k stars 54 forks source link

Locale lost when using `rewriteDefault` on a no-cookie environment #306

Open Vahor opened 7 months ago

Vahor commented 7 months ago

Is your feature request related to a problem? Please describe.

This scenario can seem a bit weird, but actually it's not. The no-cookie environment can be crawling bots, social embeds etc that are using fetch/curl to get the raw html.

The issue is that when using rewriteDefault the following happen: (Here the default locale is 'fr', the locales are fr and en)

  1. Goes to /en

Middleware check the locale in cookies -> Nothing -> Fallback to fr. The locale (en) is in the prefix, it's not the default locale (fr) and it's not the one stored in the cookies (fallback to fr) so we are redirected to /. Then we add the locale to the response (in cookies). But our environment does not support cookies

  1. Redirected to /.

Same here, the middleware check the cookies and found nothing, so if fallback to fr. We no longer have the locale in prefix, the default locale (fr) is the same as the one in cookie (fallback to fr), so it stops early.

In conclusion we were going for /en but the response body is in french.

I also think this is a bug as rewriteDefault should only redirect if it's the default language ? In our case if we go to /en there should not be any redirection.

Reproduction

You can use this reproduction: https://github.com/Vahor/next-international-rewritedefault-issue In one terminal

In another terminal

Describe the solution you'd like

In a cookie disabled environment the rewriteDefault should work or should default to a working alternative.

Describe alternatives you've considered Not using rewriteDefault or rewrite but that's not what we want.

Note: redirect is the only one working.

But as an idea, maybe default to the redirect behaviour if cookies are not available. (maybe based on the useragent as the issue is mostly for crawler and bots?)