Skip to content

Commit

Permalink
fix: loop redirection on 'ssr: false' and 'no_prefix' strategy (#2341)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Aug 24, 2023
1 parent 6c2a411 commit f869292
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
5 changes: 5 additions & 0 deletions specs/fixtures/issues/2334/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<NuxtPage />
</div>
</template>
9 changes: 9 additions & 0 deletions specs/fixtures/issues/2334/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -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'
}
})
14 changes: 14 additions & 0 deletions specs/fixtures/issues/2334/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
3 changes: 3 additions & 0 deletions specs/fixtures/issues/2334/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div id="top">Foo</div>
</template>
18 changes: 18 additions & 0 deletions specs/issues/2334.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
10 changes: 5 additions & 5 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export function detectRedirect<Context extends NuxtApp = NuxtApp>({
__DEBUG__ && console.log('detectRedirect: calledWithRouting -> ', calledWithRouting, routeLocaleGetter(route.to))

let redirectPath = ''
const { fullPath: toFullPath } = route.to
const isStaticGenerate = isSSG && process.server

/**
Expand All @@ -310,11 +311,10 @@ export function detectRedirect<Context extends NuxtApp = NuxtApp>({
(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.
Expand All @@ -340,7 +340,7 @@ export function detectRedirect<Context extends NuxtApp = NuxtApp>({
})
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
}
}
Expand Down

0 comments on commit f869292

Please sign in to comment.