- πΊπ¦Ukraine Vadym.Tseiko
Hi everyone, I have a similar problem, my project is configured via detection and selection URL by domains method and I need to switch language to the correct domain accordingly to the user's browser preferences. But in my case, when using detection and selection via browser I'm constantly switched to my preferred language without the ability to switch language only once(what I'm achieved with custom event subscriber and redirect). I added a custom code that redirects user but now I'm having a problem with SEO as Google search bots is redirected too, and all machine services return 302 status code besides the basic English one as those bots I guess have English as a preference. Is there a way for redirect to not mess with bots or to detect them and not redirect if that not human-users, none of those patches fixed behavior when the user is constantly redirected and none of them set the right domain in the URL as a result I getting always URL from with browser switch to the preferred language? Has anyone solved this problem somehow?
- last update
over 1 year ago 29,721 pass, 2 fail - First commit to issue fork.
- Merge request !6512Issue #2430335 Browser language detection is not cache aware β (Open) created by matthijs
- π§πͺBelgium matthijs
Since the cache ID is actually cached, the
$vary_headers
passed to\Drupal\page_cache\StackMiddleware\PageCache::getCacheId()
actually did nothing.I created a merge request from #2430335-194: Browser language detection is not cache aware β and did some changes to cache the cache ID by
$vary_headers
. - π©πͺGermany sleitner
I fixed the visibility of the modules variable of the test.
Drupal\Tests\language\Functional\LanguageBrowserDetectionAcceptLanguageTest
still fails. - Status changed to Needs review
about 1 year ago 9:00pm 14 February 2024 - π©πͺGermany sleitner
X-Drupal-Cache
is now always present. It isMISS
for the first access andHIT
for further accesses. - Status changed to Needs work
about 1 year ago 9:19pm 17 February 2024 - πΊπΈUnited States smustgrave
Left some comments on MR
I don't know if adding a CR from D9 is correct anymore but will leave as is.
- Status changed to Needs review
about 1 year ago 11:21pm 17 February 2024 - π©πͺGermany sleitner
I don't know why this is not a local variable. But since it only reads the request, a clone is not necessary.
- πΊπΈUnited States smustgrave
Believe all feedback has been addressed around MR but don't really follow the issue summary, specifically the proposed solution section. Will leave in review if anyone else wants to pick up.
- Status changed to Needs work
about 1 year ago 4:14pm 25 February 2024 - π¨πSwitzerland znerol
This includes #3023104. As one of the authors of the cache policy mechanism, I'd be disappointed if this approach would make it into core. Citing from #3023104-4: Introduce "Vary" page cache response policy β :
Regrettably this approach clearly violates the
ResponsePolicyInterface
contract. While not stated explicitly in the documentation, thecheck()
method is certainly not expected to actually change the response. - π¨πSwitzerland znerol
I suggest to move the logic into a response event subscriber instead.
- π³π±Netherlands bbrala Netherlands
bbrala β changed the visibility of the branch 2430335-browser-language-detection to active.
- π³π±Netherlands bbrala Netherlands
I played around with this and moved the implementation to a eventsubscriber as asked for by @znerol. I see one things that is very weird though as per comment in the code:
/** * Either Drupal\Tests\page_cache\Functional\PageCacheVaryTest::testPageCacheWithVary * fails or Drupal\Tests\language\Functional\LanguageBrowserDetectionAcceptLanguageTest::testAcceptLanguageEmptyDefault * fails if you remove one of those. Not sure why */ $events[KernelEvents::RESPONSE][] = ['onRespond', -100]; $events[KernelEvents::RESPONSE][] = ['onRespond', 100]; return $events;
Not sure why, those priorities sometimes confuse me a little.
- Status changed to Needs review
8 months ago 2:44pm 22 July 2024 - π³π±Netherlands bbrala Netherlands
Well at least everything is green. So thats good. But it some weirdness with the event subscriber. Not sure how that should work. I didn't investigate the redirects and such. Mostly just refactored the current recent patch to work with an event subscriber.
- Status changed to Needs work
7 months ago 3:02pm 14 August 2024 - π³π±Netherlands bbrala Netherlands
I recently discussed thie with catch.
Using the header and vary on that is very dangerous for cache purposes. It will make very very many variations, which results in bad caching and performance. Not to mention possible memroy issues in caches or high cache trashing.
We did think about how we could possible work around that.
What if;
We normalize the prermutation of the language into the resulting language, and cache that normalization and the resulting language.
So perhaps a site with en_EN and fr_FR. A browser comes along and tells the site "Hi i'm
en-GB,en-US;q=0.9,en;q=0.8
". The site would find out what this will end up as soen-GB,en-US;q=0.9,en;q=0.8
magic =>en_EN
, we cache this and threat serve cache asifen_EN
. This would mean (at least on the drupal side) only the records for inLang => outLang exist, which are pretty small and we dont end up with loads of difference permutations.Not entirely sure how we would implement that, but feels like a way to have drupal cache be happy.
The little devil on my shoulder also has a voice though. Which does tell me this is kinda a. can of worms as this could also easily break caching on the caches in front of Drupal, be it varnisch or maybe smoething like cloudflare. That is kinda the scary part. Perhaps contrib is better to have this live, instead of trying to get this into core.
- π§πͺBelgium borisson_ Mechelen, π§πͺ
Using the header and vary on that is very dangerous for cache purposes. It will make very very many variations, which results in bad caching and performance. Not to mention possible memroy issues in caches or high cache trashing.
While this is true for a web-based environment, it is not true for JSON:API.
Quoting @GabeSullice from https://www.drupal.org/project/drupal/issues/2794431#comment-12806367 π± [META] Formalize translations support Needs workHowever, JSON API doesn't need to be very concerned about that because JSON API requests are not typically made directly via a browser. Instead, they're most frequently made via JavaScript or some other system where the Accept-Language header can be tightly controlled by a developer.
- π¨πSwitzerland znerol
Re #21: Fair.
Maybe we need a way to create two (or more) groups of routes. One group is accessed by browsers, others by special clients. Then apply different sets of rules for request processing, content/language negotiation, authentication and caching to each of them.
However, page cache is only effective if it runs before request routing. So maybe the matching needs to happen on a path prefix.