Score:0

Varnish and custom blocks

eg flag

I want to configure varnish on my drupal 8 multilingual website and I have a block that appears on a page whose path is /my-test . So in french the url would be like this : /fr/my-test whereas in english /my-test . In this block, I simply use:

$lang = \Drupal::languageManager()->getCurrentLanguage()->getId(); to detect the language and then display news depending on this language. so when the url is /my-test > the language is english (default)> the block displays news related to the world. when the url is /fr/my-test > the language is french > the block displays news related to the France.

My question is, if varnish is used and pages like /my-test is cached. If at first place, a user enters /my-test and the block displays news about the world then after that, another user visits /my-test/fr . Will the block display news about the world (because the page is cached) despite having /fr in the url? or will it display the right content (news related to France)? I suppose that $lang detects the language from the url prefix not from the user's browser.

cn flag
Varnish is typically configured to cache by URL. Different URL, different cache entry.
Jaypan avatar
de flag
You want to look at Cache Contexts (https://www.drupal.org/docs/drupal-apis/cache-api/cache-contexts). I believe Drupal pushes these out to Varnish in the page headers, so that Varnish will vary the cache accordingly.
Mira chalak avatar
eg flag
@Jaypan So I assume that the block will display the right content event though in the link you sent LanguageInterface is mentioned instead of languageManager ..
Mira chalak avatar
eg flag
@Clive I am also using `$active_id = \Drupal::service('domain.negotiator')->getActiveId();` in this same block code and I had the same doubt regarding this expression, so thanks to your answer I know that $active_id won't be the same for both users since the URL is different :)
4uk4 avatar
cn flag
@Clive comment is correct. Varnish can't cache `/my-test` if you have browser detection enabled. So you need to add a path prefix for the default language, too, and then redirect to that. On the redirected page you can use varnish.
4uk4 avatar
cn flag
I assume you've posted this question unregistered https://drupal.stackexchange.com/questions/307459/varnish-in-multilingual-site.
Mira chalak avatar
eg flag
Thank you so much @4k4 . what do you mean by "if you have browser detection enabled" ? How can I enable that ? I still cannot figure out what a language detection method is. I don't think I have browser detection enabled. because when you enter my website, you are asked to choose a language and then we redirect you to this language prefix.
4uk4 avatar
cn flag
/admin/config/regional/language/detection
Mira chalak avatar
eg flag
@4k4 I have these enabled : Country Path Language Handler URL, URL (path prefix), selected language. Browser is disabled. so i assume it's good
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.