From 4d77019f786086b9e496661f8c8d55893556e31d Mon Sep 17 00:00:00 2001 From: Thomas Beduneau Date: Thu, 12 Aug 2021 08:20:18 +0200 Subject: [PATCH] fix(differentDomains): handling of runtime domains from store (#1183) Co-authored-by: Rafal Chlodnicki --- src/templates/plugin.main.js | 34 ++++++++++++++++++------------- test/fixture/basic/store/index.js | 4 ++++ test/module.test.js | 18 ++++++++++++---- types/index.d.ts | 1 + 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/templates/plugin.main.js b/src/templates/plugin.main.js index cd5728f70..0878d5f81 100644 --- a/src/templates/plugin.main.js +++ b/src/templates/plugin.main.js @@ -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 @@ -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) : '' diff --git a/test/fixture/basic/store/index.js b/test/fixture/basic/store/index.js index abd3416f7..da168c86b 100644 --- a/test/fixture/basic/store/index.js +++ b/test/fixture/basic/store/index.js @@ -1,5 +1,6 @@ /** * @typedef {{ + * localeDomains: Record * routePathFr: string * }} State * @@ -8,6 +9,9 @@ /** @return {TestStore['state']} */ export const state = () => ({ + localeDomains: { + ua: 'ua-runtime.nuxt-app.localhost' + }, routePathFr: '' }) diff --git a/test/module.test.js b/test/module.test.js index 6a0eadeb7..d2279e443 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -94,8 +94,7 @@ describe('differentDomains enabled', () => { { code: 'ua', iso: 'uk-UA', - name: 'Українська', - domain: 'http://ua.nuxt-app.localhost' + name: 'Українська' } ] @@ -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: { @@ -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' }, { diff --git a/types/index.d.ts b/types/index.d.ts index f033ccd4b..37a0c487c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -10,6 +10,7 @@ export type RedirectOnOptions = 'all' | 'root' | 'no prefix' export interface LocaleObject extends Record { code: Locale dir?: Directions + domain?: string file?: string isCatchallLocale?: boolean iso?: string