From aa23d555a36fd4ca9a252f7b85aeb941b6d2e294 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Fri, 31 Aug 2018 08:03:05 -0700 Subject: [PATCH] Vary caching on Accept-Language header if it is used If we use the Accept-Language header to determine the interface language, we need to vary caching on it so that a user with a different Accept-Language header won't get the wrong language out of the cache. Note that for optimal cache hitrates, you might want to implement parsing of the header in your cache itself. Bug: T203179 Change-Id: I976696cf20a5da5b2767c87e0d754a901b711a46 --- UniversalLanguageSelector.hooks.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/UniversalLanguageSelector.hooks.php b/UniversalLanguageSelector.hooks.php index 4884f98b..dbaf6def 100644 --- a/UniversalLanguageSelector.hooks.php +++ b/UniversalLanguageSelector.hooks.php @@ -223,6 +223,14 @@ class UniversalLanguageSelectorHooks { public static function getLanguage( User $user, &$code, IContextSource $context ) { global $wgULSAnonCanChangeLanguage, $wgULSLanguageDetection; + if ( $wgULSLanguageDetection ) { + // Vary any caching based on the header value. Note that + // we need to vary regardless of whether we end up using + // the header or not, so that requests without the header + // don't show up for people with it. + $context->getOutput()->addVaryHeader( 'Accept-Language' ); + } + if ( !self::isToolbarEnabled( $user ) ) { return; } @@ -276,6 +284,8 @@ class UniversalLanguageSelectorHooks { // As last resort, try Accept-Language headers if allowed if ( $wgULSLanguageDetection ) { + // We added a Vary header at the top of this function, + // since we're depending upon the Accept-Language header $preferred = $request->getAcceptLang(); $default = self::getDefaultLanguage( $preferred ); if ( $default !== '' ) {