Skip to content

Commit

Permalink
fix(differentDomains): handling of runtime domains from store (#1183)
Browse files Browse the repository at this point in the history
Co-authored-by: Rafal Chlodnicki <[email protected]>
  • Loading branch information
enwin and rchl authored Aug 12, 2021
1 parent d8a31b0 commit 4d77019
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
34 changes: 20 additions & 14 deletions src/templates/plugin.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ export default async (context) => {

const finalLocale =
(options.detectBrowserLanguage && doDetectBrowserLanguage(route)) ||
getLocaleFromRoute(route) || app.i18n.locale || app.i18n.defaultLocale || ''
(!options.differentDomains && getLocaleFromRoute(route)) ||
app.i18n.locale || app.i18n.defaultLocale || ''

if (options.skipSettingLocaleOnNavigate) {
app.i18n.__pendingLocale = finalLocale
Expand Down Expand Up @@ -347,30 +348,35 @@ export default async (context) => {
// Initialize locale and fallbackLocale as vue-i18n defaults those to 'en-US' if falsey
app.i18n.locale = ''
app.i18n.fallbackLocale = vueI18nOptions.fallbackLocale || ''
extendVueI18nInstance(app.i18n)
const resolveBaseUrlOptions = {
differentDomains: options.differentDomains,
normalizedLocales: options.normalizedLocales
}
app.i18n.__baseUrl = resolveBaseUrl(options.baseUrl, context, '', resolveBaseUrlOptions)
app.i18n.__onNavigate = onNavigate

Vue.prototype.$nuxtI18nHead = nuxtI18nHead

if (store) {
// Inject in store.
store.$i18n = app.i18n

if (store.state.localeDomains) {
for (const locale of app.i18n.locales) {
if (typeof (locale) === 'string') {
continue
for (const [index, locale] of options.normalizedLocales.entries()) {
const domain = store.state.localeDomains[locale.code]
if (domain) {
locale.domain = domain
const optionsLocale = options.locales[index]
if (typeof (optionsLocale) !== 'string') {
optionsLocale.domain = domain
}
}
locale.domain = store.state.localeDomains[locale.code]
}
}
}

extendVueI18nInstance(app.i18n)
const resolveBaseUrlOptions = {
differentDomains: options.differentDomains,
normalizedLocales: options.normalizedLocales
}
app.i18n.__baseUrl = resolveBaseUrl(options.baseUrl, context, '', resolveBaseUrlOptions)
app.i18n.__onNavigate = onNavigate

Vue.prototype.$nuxtI18nHead = nuxtI18nHead

/** @type {string | undefined} */
let finalLocale = options.detectBrowserLanguage ? doDetectBrowserLanguage(route) : ''

Expand Down
4 changes: 4 additions & 0 deletions test/fixture/basic/store/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {{
* localeDomains: Record<string, string>
* routePathFr: string
* }} State
*
Expand All @@ -8,6 +9,9 @@

/** @return {TestStore['state']} */
export const state = () => ({
localeDomains: {
ua: 'ua-runtime.nuxt-app.localhost'
},
routePathFr: ''
})

Expand Down
18 changes: 14 additions & 4 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ describe('differentDomains enabled', () => {
{
code: 'ua',
iso: 'uk-UA',
name: 'Українська',
domain: 'http://ua.nuxt-app.localhost'
name: 'Українська'
}
]

Expand Down Expand Up @@ -129,6 +128,17 @@ describe('differentDomains enabled', () => {
expect(dom.querySelector('body')?.textContent).toContain('page: Accueil')
})

test('host matches locale\'s runtime-set domain (ua)', async () => {
const requestOptions = {
headers: {
Host: 'ua-runtime.nuxt-app.localhost'
}
}
const html = await get('/', requestOptions)
const dom = getDom(html)
expect(dom.querySelector('body')?.textContent).toContain('locale: ua')
})

test('x-forwarded-host does not match locale\'s domain', async () => {
const requestOptions = {
headers: {
Expand Down Expand Up @@ -234,13 +244,13 @@ describe('differentDomains enabled', () => {
{
tagName: 'link',
rel: 'alternate',
href: 'http://ua.nuxt-app.localhost/locale',
href: 'http://ua-runtime.nuxt-app.localhost/locale',
hreflang: 'uk'
},
{
tagName: 'link',
rel: 'alternate',
href: 'http://ua.nuxt-app.localhost/locale',
href: 'http://ua-runtime.nuxt-app.localhost/locale',
hreflang: 'uk-UA'
},
{
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type RedirectOnOptions = 'all' | 'root' | 'no prefix'
export interface LocaleObject extends Record<string, any> {
code: Locale
dir?: Directions
domain?: string
file?: string
isCatchallLocale?: boolean
iso?: string
Expand Down

0 comments on commit 4d77019

Please sign in to comment.