Skip to content

Commit

Permalink
[docs] Guard against unknown value in userLanguage cookie (mui#23336)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrookes committed Oct 31, 2020
1 parent 407d2bb commit 88fecb6
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions docs/pages/_app.js
Expand Up @@ -23,7 +23,7 @@ import loadScript from 'docs/src/modules/utils/loadScript';
import RtlContext from 'docs/src/modules/utils/RtlContext';
import { ThemeProvider } from 'docs/src/modules/components/ThemeContext';
import { pathnameToLanguage, getCookie } from 'docs/src/modules/utils/helpers';
import { ACTION_TYPES, CODE_VARIANTS } from 'docs/src/modules/constants';
import { ACTION_TYPES, CODE_VARIANTS, LANGUAGES } from 'docs/src/modules/constants';
import { useUserLanguage } from 'docs/src/modules/utils/i18n';

// Configure JSS
Expand Down Expand Up @@ -51,7 +51,9 @@ function LanguageNegotiation() {
React.useEffect(() => {
const { userLanguage: userLanguageUrl, canonical } = pathnameToLanguage(router.asPath);
const preferedLanguage =
getCookie('userLanguage') || acceptLanguage.get(navigator.language) || userLanguage;
LANGUAGES.find((lang) => lang === getCookie('userLanguage')) ||
acceptLanguage.get(navigator.language) ||
userLanguage;

if (userLanguageUrl === 'en' && userLanguage !== preferedLanguage) {
window.location = preferedLanguage === 'en' ? canonical : `/${preferedLanguage}${canonical}`;
Expand Down

0 comments on commit 88fecb6

Please sign in to comment.