Skip to content

Commit

Permalink
feat: Added cookieDomain option to override locale cookie's domain (#599
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tho-masn authored Feb 18, 2020
1 parent 3eb9fdb commit 7525cd7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/es/options-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Aquí están todas las opciones disponibles al configurar el módulo y sus valor
// preferred language to prevent subsequent redirections
// Set to false to redirect every time
useCookie: true,
// Set to override the default domain of the cookie. Defaults to host of the site.
cookieDomain: null
// Cookie name
cookieKey: 'i18n_redirected',
// Set to always redirect to value stored in the cookie, not just once
Expand Down
2 changes: 2 additions & 0 deletions docs/options-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Here are all the options available when configuring the module and their default
// preferred language to prevent subsequent redirections
// Set to false to redirect every time
useCookie: true,
// Set to override the default domain of the cookie. Defaults to host of the site.
cookieDomain: null
// Cookie name
cookieKey: 'i18n_redirected',
// Set to always redirect to value stored in the cookie, not just once
Expand Down
1 change: 1 addition & 0 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ exports.DEFAULT_OPTIONS = {
rootRedirect: null,
detectBrowserLanguage: {
useCookie: true,
cookieDomain: null,
cookieKey: 'i18n_redirected',
alwaysRedirect: false,
fallbackLocale: null
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default async (context) => {
}, { preserveState: !!store.state[vuex.moduleName] })
}

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

const getLocaleCookie = () => {
if (useCookie) {
Expand All @@ -109,6 +109,11 @@ export default async (context) => {
path: '/',
sameSite: 'lax'
}

if (cookieDomain) {
cookieOptions.domain = cookieDomain
}

if (process.client) {
JsCookie.set(cookieKey, locale, cookieOptions)
} else if (res) {
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 @@ -26,6 +26,7 @@ declare namespace NuxtVueI18n {

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

0 comments on commit 7525cd7

Please sign in to comment.