Repository: nuxt-community/nuxt-i18n (js)

Issue:
how to setup nuxt i18n for multi domains and deploy on multiple domain? 
When I set differentDomains: true, it throws an error while generating.

TypeError: Cannot read property 'headers' of undefined
    at getLocaleDomain (.nuxt/nuxt-i18n/utils-common.js:88:0)
    at plugin_main (.nuxt/nuxt-i18n/plugin.main.js:238:0)
    at createApp (.nuxt/index.js:202:0)
    at async module.exports.__webpack_exports__.default (.nuxt/server.js:81:0)

has anyone deployed on multiple domains for eg example.com and example.fr ? 
<!--

 **IMPORTANT!**
Please make sure to look for an answer to your question in our documentation before asking a question here.

If relevant to your question, provide nuxt-i18n configuration.

If you have a general question regarding nuxt-i18n use Discord `modules` channel. Thanks!

Documentation: https://nuxt-community.github.io/nuxt-i18n/
Nuxt Discord: https://discord.nuxtjs.org/

-->


Pull request description:
Don't crash during generate with differentDomains enabled
Resolves #794

Patch:
diff --git a/src/templates/plugin.routing.js b/src/templates/plugin.routing.js
index 8841a142..c642eb45 100644
--- a/src/templates/plugin.routing.js
+++ b/src/templates/plugin.routing.js
@@ -121,7 +121,7 @@ function switchLocalePath (locale) {
       let protocol
       if (process.server) {
         const isHTTPS = require('is-https')
-        protocol = isHTTPS(this.req) ? 'https' : 'http'
+        protocol = (this.req && isHTTPS(this.req)) ? 'https' : 'http'
       } else {
         protocol = window.location.protocol.split(':')[0]
       }
diff --git a/src/templates/utils-common.js b/src/templates/utils-common.js
index 86a5aacf..64e2469a 100644
--- a/src/templates/utils-common.js
+++ b/src/templates/utils-common.js
@@ -85,7 +85,13 @@ export const resolveBaseUrl = (baseUrl, context) => {
  * @return {string | null} Locade code found if any
  */
 export const getLocaleDomain = (locales, req, { localDomainKey, localeCodeKey }) => {
-  const hostname = process.client ? window.location.hostname : (req.headers['x-forwarded-host'] || req.headers.host)
+  let hostname = null
+
+  if (process.client) {
+    hostname = window.location.hostname
+  } else if (req) {
+    hostname = req.headers['x-forwarded-host'] || req.headers.host
+  }
 
   if (hostname) {
     const localeDomain = locales.find(l => l[localDomainKey] === hostname)


Test patch:
diff --git a/test/fixture/no-lang-switcher/pages/about.vue b/test/fixture/no-lang-switcher/pages/about.vue
index 84fbf594..8fc1f7c9 100644
--- a/test/fixture/no-lang-switcher/pages/about.vue
+++ b/test/fixture/no-lang-switcher/pages/about.vue
@@ -1,6 +1,6 @@
 <template>
   <div>
     <div id="current-page">page: {{ $t('about') }}</div>
-    <nuxt-link id="link-home" exact :to="localePath('index')">{{ $t('home') }}</nuxt-link>
+    <a id="link-home" :href="localePath('index')">{{ $t('home') }}</a>
   </div>
 </template>
diff --git a/test/fixture/no-lang-switcher/pages/index.vue b/test/fixture/no-lang-switcher/pages/index.vue
index a689af04..f9507b1d 100644
--- a/test/fixture/no-lang-switcher/pages/index.vue
+++ b/test/fixture/no-lang-switcher/pages/index.vue
@@ -1,7 +1,7 @@
 <template>
   <div>
     <div id="current-page">page: {{ $t('home') }}</div>
-    <nuxt-link id="link-about" exact :to="localePath('about')">{{ $t('about') }}</nuxt-link>
+    <a id="link-about" :href="localePath('about')">{{ $t('about') }}</a>
     <div id="current-locale">locale: {{ $i18n.locale }}</div>
   </div>
 </template>
diff --git a/test/module.test.js b/test/module.test.js
index 5a5fa1a7..0a2eb68b 100644
--- a/test/module.test.js
+++ b/test/module.test.js
@@ -1405,3 +1405,42 @@ describe('generate with detectBrowserLanguage.fallbackLocale', () => {
     expect(dom.querySelector('#current-page')?.textContent).toBe('page: Accueil')
   })
 })
+
+describe('generate with differentDomains enabled', () => {
+  const distDir = resolve(__dirname, 'fixture', 'no-lang-switcher', '.nuxt-generate')
+
+  beforeAll(async () => {
+    /** @type {import('@nuxt/types').NuxtConfig} */
+    const overrides = {
+      generate: { dir: distDir },
+      i18n: {
+        differentDomains: true
+      }
+    }
+
+    const testConfig = loadConfig(__dirname, 'no-lang-switcher', overrides, { merge: true })
+    // Override those after merging to overwrite original values.
+    testConfig.i18n.locales = [
+      {
+        code: 'en',
+        domain: 'en-domain',
+        iso: 'en',
+        name: 'English'
+      },
+      {
+        code: 'fr',
+        domain: 'fr-domain',
+        iso: 'fr-FR',
+        name: 'Français'
+      }
+    ]
+    await generate(testConfig)
+  })
+
+  test('does not crash on generating', () => {
+    const contents = readFileSync(resolve(distDir, 'about', 'index.html'), 'utf-8')
+    const dom = getDom(contents)
+    expect(dom.querySelector('#current-page')).toBeDefined()
+    expect(dom.querySelector('#current-page')?.textContent).toBe('page: About us')
+  })
+})