Skip to content

Commit

Permalink
fix: loop redirection on differentDomains (#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Aug 15, 2023
1 parent 47494b6 commit 81f5d69
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 2 deletions.
5 changes: 5 additions & 0 deletions specs/fixtures/issues/2313/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<NuxtPage />
</div>
</template>
20 changes: 20 additions & 0 deletions specs/fixtures/issues/2313/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
i18n: {
defaultLocale: 'fr',
differentDomains: true,
locales: [
{
code: 'en',
domain: 'en.nuxt-app.localhost'
},
{
code: 'fr',
domain: 'fr.nuxt-app.localhost'
}
],
strategy: 'prefix'
// detectBrowserLanguage: false
},
ssr: false
})
14 changes: 14 additions & 0 deletions specs/fixtures/issues/2313/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "nuxt3-test-issues-2313",
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"@nuxtjs/i18n": "latest",
"nuxt": "latest"
}
}
9 changes: 9 additions & 0 deletions specs/fixtures/issues/2313/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>
Locale: <code id="locale">{{ locale }}</code>
</div>
</template>

<script setup lang="ts">
const { locale } = useI18n()
</script>
28 changes: 28 additions & 0 deletions specs/issues/2313.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { test, expect, describe } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, url, createPage } from '../utils'
import { getText } from '../helper'

describe('#2313', async () => {
await setup({
rootDir: fileURLToPath(new URL(`../fixtures/issues/2313`, import.meta.url))
})

async function assertDifferenctDomain(locale: string, browserLocale = 'en') {
const home = url('/')
const page = await createPage(undefined, {
extraHTTPHeaders: {
'X-Forwarded-Host': `${locale}.nuxt-app.localhost`
},
locale: browserLocale
})
await page.goto(home)
expect(await getText(page, '#locale')).toEqual(locale)
await page.close()
}

test('detection locale from domain', async () => {
await assertDifferenctDomain('en')
await assertDifferenctDomain('fr', 'fr')
})
})
2 changes: 1 addition & 1 deletion src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ export function getDomainFromLocale(localeCode: Locale, locales: LocaleObject[],
} = useRequestEvent(nuxt)
protocol = req && isHTTPS(req) ? 'https' : 'http'
} else {
protocol = window.location.protocol.split(':')[0]
protocol = new URL(window.location.origin).protocol
}
return protocol + '://' + lang.domain
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export async function navigate<Context extends NuxtApp = NuxtApp>(
const state = useRedirectState()
__DEBUG__ && console.log('redirect state ->', state.value, 'redirectPath -> ', redirectPath)
if (process.client) {
if (state.value !== redirectPath) {
if (state.value && state.value !== redirectPath) {
state.value = '' // reset redirect path
window.location.assign(redirectPath)
}
Expand Down

0 comments on commit 81f5d69

Please sign in to comment.