Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add setting to use cross-origin cookie for "detectBrowserLocale" #853

Merged
merged 9 commits into from
Aug 23, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/browser-language-detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,17 @@ To redirect the user every time they visit the app and keep their selected choic
}
}]
```

To use the cookie within a cross-origin environment (e.g. in an iFrame), you can set `crossOriginCookie: true`. This will change the cookie settings from `SameSite=Lax` to `SameSite=None; Secure`.

```js
// nuxt.config.js

['nuxt-i18n', {
// ...
detectBrowserLanguage: {
useCookie: true,
crossOriginCookie: true
}
}]
```
1 change: 1 addition & 0 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exports.DEFAULT_OPTIONS = {
rootRedirect: null,
detectBrowserLanguage: {
useCookie: true,
crossOriginCookie: false,
cookieDomain: null,
cookieKey: 'i18n_redirected',
alwaysRedirect: false,
Expand Down
4 changes: 2 additions & 2 deletions src/templates/plugin.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default async (context) => {
})
}

const { useCookie, cookieKey, cookieDomain } = detectBrowserLanguage
const { useCookie, cookieKey, cookieDomain, crossOriginCookie } = detectBrowserLanguage

const loadAndSetLocale = async (newLocale, { initialSetup = false } = {}) => {
// Abort if different domains option enabled
Expand Down Expand Up @@ -213,7 +213,7 @@ export default async (context) => {
i18n.differentDomains = differentDomains
i18n.beforeLanguageSwitch = beforeLanguageSwitch
i18n.onLanguageSwitched = onLanguageSwitched
i18n.setLocaleCookie = locale => setLocaleCookie(locale, res, { useCookie, cookieDomain, cookieKey })
i18n.setLocaleCookie = locale => setLocaleCookie(locale, res, { useCookie, cookieDomain, cookieKey, crossOriginCookie })
i18n.getLocaleCookie = () => getLocaleCookie(req, { useCookie, cookieKey, localeCodes })
i18n.setLocale = (locale) => loadAndSetLocale(locale)
i18n.__baseUrl = app.i18n.__baseUrl
Expand Down
7 changes: 4 additions & 3 deletions src/templates/utils-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,18 @@ export const getLocaleCookie = (req, { useCookie, cookieKey, localeCodes }) => {
/**
* @param {string} locale
* @param {object} [res]
* @param {{ useCookie: boolean, cookieDomain: string, cookieKey: string}} options
* @param {{ useCookie: boolean, cookieDomain: string, cookieKey: string, crossOriginCookie: boolean}} options
*/
export const setLocaleCookie = (locale, res, { useCookie, cookieDomain, cookieKey }) => {
export const setLocaleCookie = (locale, res, { useCookie, cookieDomain, cookieKey, crossOriginCookie }) => {
if (!useCookie) {
return
}
const date = new Date()
const cookieOptions = {
expires: new Date(date.setDate(date.getDate() + 365)),
path: '/',
sameSite: 'lax'
sameSite: crossOriginCookie ? 'none' : 'lax',
secure: !!crossOriginCookie
lucianholt97 marked this conversation as resolved.
Show resolved Hide resolved
}

if (cookieDomain) {
Expand Down
1 change: 1 addition & 0 deletions types/nuxt-i18n.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ declare namespace NuxtVueI18n {

interface DetectBrowserLanguageInterface {
useCookie?: boolean
crossOriginCookie?: boolean
cookieDomain?: string | null
cookieKey?: string
alwaysRedirect?: boolean
Expand Down