Skip to content

Commit

Permalink
fix: crash on no_prefix + invalid/tempered locale cookie
Browse files Browse the repository at this point in the history
We need to validate locale gotten from cookie as invalid one would
later crash when creating SEO tags. And it's not useful to have invalid
locale set.
  • Loading branch information
rchl committed Sep 13, 2019
1 parent 6427836 commit 4b56d84
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exports.DEFAULT_OPTIONS = {
detectBrowserLanguage: {
useCookie: true,
cookieKey: 'i18n_redirected',
alwaysRedirect: '',
alwaysRedirect: false,
fallbackLocale: null
},
differentDomains: false,
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ export default async (context) => {
const routeLocale = getLocaleFromRoute(route)
locale = routeLocale || locale
} else if (useCookie) {
locale = getLocaleCookie() || locale
const localeCookie = getLocaleCookie()

if (localeCodes.includes(localeCookie)) {
locale = localeCookie
}
}

await loadAndSetLocale(locale, { initialSetup: true })
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/module.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ exports[`no_prefix strategy / contains EN text & link /about 1`] = `
<body >
<div data-server-rendered=\\"true\\" id=\\"__nuxt\\"><!----><div id=\\"__layout\\"><div>
Homepage
<a href=\\"/about\\">About us</a></div></div></div>
<a href=\\"/about\\">About us</a> <div>locale: en</div></div></div></div>
</body>
</html>
"
Expand Down
1 change: 1 addition & 0 deletions test/fixture/no-lang-switcher/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<div>
{{ $t('home') }}
<nuxt-link exact :to="localePath('about')">{{ $t('about') }}</nuxt-link>
<div>locale: {{ $i18n.locale }}</div>
</div>
</template>
20 changes: 20 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ describe('basic', () => {
const newRoute = window.$nuxt.switchLocalePath()
expect(newRoute).toBe('/about-us')
})

test('fallbacks to default locale with invalid locale cookie', async () => {
const requestOptions = {
headers: {
Cookie: 'i18n_redirected=invalid'
}
}
const html = await get('/', requestOptions)
expect(cleanUpScripts(html)).toContain('locale: en')
})
})

describe('lazy loading', () => {
Expand Down Expand Up @@ -255,6 +265,16 @@ describe('no_prefix strategy', () => {

spy.mockRestore()
})

test('fallbacks to default locale with invalid locale cookie', async () => {
const requestOptions = {
headers: {
Cookie: 'i18n_redirected=invalid'
}
}
const html = await get('/', requestOptions)
expect(cleanUpScripts(html)).toContain('locale: en')
})
})

describe('no_prefix strategy + differentDomains', () => {
Expand Down

0 comments on commit 4b56d84

Please sign in to comment.