diff --git a/specs/fixtures/issues/2334/app.vue b/specs/fixtures/issues/2334/app.vue new file mode 100644 index 000000000..2b1be0907 --- /dev/null +++ b/specs/fixtures/issues/2334/app.vue @@ -0,0 +1,5 @@ + diff --git a/specs/fixtures/issues/2334/nuxt.config.ts b/specs/fixtures/issues/2334/nuxt.config.ts new file mode 100644 index 000000000..f532146a0 --- /dev/null +++ b/specs/fixtures/issues/2334/nuxt.config.ts @@ -0,0 +1,9 @@ +// https://v3.nuxtjs.org/api/configuration/nuxt.config +export default defineNuxtConfig({ + ssr: false, + modules: ['@nuxtjs/i18n'], + i18n: { + debug: true, + strategy: 'no_prefix' + } +}) diff --git a/specs/fixtures/issues/2334/package.json b/specs/fixtures/issues/2334/package.json new file mode 100644 index 000000000..81429fafc --- /dev/null +++ b/specs/fixtures/issues/2334/package.json @@ -0,0 +1,14 @@ +{ + "name": "nuxt3-test-issues-2334", + "private": true, + "scripts": { + "build": "nuxt build", + "dev": "nuxt dev", + "generate": "nuxt generate", + "preview": "nuxt preview" + }, + "devDependencies": { + "@nuxtjs/i18n": "latest", + "nuxt": "latest" + } +} diff --git a/specs/fixtures/issues/2334/pages/index.vue b/specs/fixtures/issues/2334/pages/index.vue new file mode 100644 index 000000000..1fe44ef8b --- /dev/null +++ b/specs/fixtures/issues/2334/pages/index.vue @@ -0,0 +1,3 @@ + diff --git a/specs/issues/2334.spec.ts b/specs/issues/2334.spec.ts new file mode 100644 index 000000000..3404ff594 --- /dev/null +++ b/specs/issues/2334.spec.ts @@ -0,0 +1,18 @@ +import { test, expect, describe } from 'vitest' +import { fileURLToPath } from 'node:url' +import { setup, url, createPage } from '../utils' +import { getText } from '../helper' + +describe('#2334', async () => { + await setup({ + rootDir: fileURLToPath(new URL(`../fixtures/issues/2334`, import.meta.url)) + }) + + test('should not redirect loop, when use no_prefix and ssr: false', async () => { + const home = url('/') + const page = await createPage() + await page.goto(home) + + expect(await getText(page, '#top')).toEqual('Foo') + }) +}) diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index 8696b6381..ce75bd897 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -295,6 +295,7 @@ export function detectRedirect({ __DEBUG__ && console.log('detectRedirect: calledWithRouting -> ', calledWithRouting, routeLocaleGetter(route.to)) let redirectPath = '' + const { fullPath: toFullPath } = route.to const isStaticGenerate = isSSG && process.server /** @@ -310,11 +311,10 @@ export function detectRedirect({ (calledWithRouting || (strategy !== 'no_prefix' && strategy !== 'prefix_and_default')) && routeLocaleGetter(route.to) !== targetLocale ) { - const { fullPath } = route.to // the current route could be 404 in which case attempt to find matching route using the full path - const routePath = context.$switchLocalePath(targetLocale) || context.$localePath(fullPath, targetLocale) - __DEBUG__ && console.log('detectRedirect: calculate routePath -> ', routePath, fullPath) - if (isString(routePath) && routePath && !isEqual(routePath, fullPath) && !routePath.startsWith('//')) { + const routePath = context.$switchLocalePath(targetLocale) || context.$localePath(toFullPath, targetLocale) + __DEBUG__ && console.log('detectRedirect: calculate routePath -> ', routePath, toFullPath) + if (isString(routePath) && routePath && !isEqual(routePath, toFullPath) && !routePath.startsWith('//')) { /** * NOTE: for #1889, #2226 * If it's the same as the previous route path, respect the current route without redirecting. @@ -340,7 +340,7 @@ export function detectRedirect({ }) const routePath = switchLocalePath(targetLocale) __DEBUG__ && console.log('detectRedirect: calculate domain or ssg routePath -> ', routePath) - if (isString(routePath)) { + if (isString(routePath) && routePath && !isEqual(routePath, toFullPath) && !routePath.startsWith('//')) { redirectPath = routePath } }