next-international
next-international copied to clipboard
[ RFC ] : Redirect to locale on re-visit when saved local is not the default
Problem
When the option urlMappingStrategy
is set to rewrite
instead of redirect, it works perfectly, and it does the necessary behaviour but there is one edge case.
So lets put the case where you have en
and fr
as locales, being en
de default one, once you set /fr, the cookies will be saved with the current locale to fr
, but once you open the page again without any locale on URI, it does stay the same. I believe in this case the "correct" behaviour would be redirect the user to correct localised URL, this could bring problems for Google & SEO, where a page at same URL should not be displayed different contents at any given time.
Solution
While we are able to provide our own resolveLocaleFromRequest
, i believe a redirect should take place if the previous remembered locale is not the default one on the the default middleware, so in this case we will end up with a few nice scenarios :
- No Locale ( First hit from the user / crawler ) -> Use the default Locale, not appended to the URL
- Change locale to other than the default one -> Redirects to the /fr ( example ) and sets the cookies
- Sub-sequent visits with remembered locale -> If its the default, leave as it, if remembered, redirect to the correct URL
Let me know what you think about this! If you allow me i can take a stab trying to PR the change :)
Interesting point, especially
page at same URL should not be displayed different contents at any given time
I'm not sure if bots actually save and reuse cookies across pages when crawling a domain? As you said, users expect to be redirected to their previous saved locale, so I don't think that would be a good idea to change this (at least without enabling an option on createI18nMiddleware
). Let me know what you think.
I don't know Google Crawler internals, but i know there is 2 runs done by them, one dry run ( no javascript ) and another that does execute Javascript ( Lazy ) , in terms of cookies i'm not really sure about it since i don't know the internals.
But from my previous experience i know that for the default local you can either suffix or not, this is up to each application to decide, since some stores even opt in to have /en/FR, where the EN belongs to the local and the FR defines the location of a store ( say IKEA , you can read in French, but browser the UK store ), but i know for simple example that anything that is localised prior to the default language, should be then suffixed and set as hreflang.
That being said, i expect if im comming back to a site and my preference was Portuguese, to have PT on the URL, in case i want to go back to english version, either i delete the suffix or just force /en/.
Here is the topic around the best practices : https://developers.google.com/search/docs/specialty/international/localized-versions.
Keep in mind this has mostly no effect for people using Dashboards or private areas where the SEO and URL is not really relevant, but for front-facing apps its crucial for a good SEO following the best practices and UX.
Edit: Also to note, that a redirect on the root domain is kindly considered a bad pratice, since the root of your domain will always results in a 307, so this is a thing that should be avoided, since theres should be a default on the root domain.
Thanks for the detailed explanation, and this Google best practices guide is really interesting.
i expect if im comming back to a site and my preference was Portuguese, to have PT on the URL
This is already the case when using the default urlMappingStrategy
(redirect
). The only redirect that occurs is when the strategy is redirect
, and you don't have a locale set in the URL.
Feel free to open a PR with the changes you have in mind!