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
This commit is contained in:
Kunal Mehta
2018-08-31 08:03:05 -07:00
committed by jenkins-bot
parent 32416b09c3
commit aa23d555a3

View File

@@ -223,6 +223,14 @@ class UniversalLanguageSelectorHooks {
public static function getLanguage( User $user, &$code, IContextSource $context ) { public static function getLanguage( User $user, &$code, IContextSource $context ) {
global $wgULSAnonCanChangeLanguage, $wgULSLanguageDetection; 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 ) ) { if ( !self::isToolbarEnabled( $user ) ) {
return; return;
} }
@@ -276,6 +284,8 @@ class UniversalLanguageSelectorHooks {
// As last resort, try Accept-Language headers if allowed // As last resort, try Accept-Language headers if allowed
if ( $wgULSLanguageDetection ) { 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(); $preferred = $request->getAcceptLang();
$default = self::getDefaultLanguage( $preferred ); $default = self::getDefaultLanguage( $preferred );
if ( $default !== '' ) { if ( $default !== '' ) {