0

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.

9
  • 2
    Varnish is typically configured to cache by URL. Different URL, different cache entry.
    – Clive
    Oct 14 at 13:01
  • 1
    You want to look at Cache Contexts (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.
    – Jaypan
    Oct 14 at 23:57
  • @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 .. Oct 15 at 7:35
  • @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 :) Oct 15 at 7:36
  • @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.
    – 4k4
    Oct 15 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.