Skip to content

Commit

Permalink
fix: routing on prefix_and_default strategy (#2235)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Jul 14, 2023
1 parent c223af3 commit 2e108fa
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 375 deletions.
380 changes: 8 additions & 372 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions specs/fixtures/issues/2226/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<template>
<div class="lang-switcher">
<div id="default-locale">Default locale: {{ selectedLocale === defaultLocale }}</div>
<NuxtLink id="lang-switch" :to="switchLocalePath(availableLocale)">Switch</NuxtLink>
</div>
<div id="content">
<NuxtPage />
</div>
</template>

<script setup>
const { locale: selectedLocale, defaultLocale, locales } = useI18n()
const switchLocalePath = useSwitchLocalePath()
const availableLocale = computed(() => locales.value.find(locale => locale !== selectedLocale.value))
</script>

<style scoped>
.lang-switcher {
margin-bottom: 30px;
}
</style>
4 changes: 4 additions & 0 deletions specs/fixtures/issues/2226/i18n.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default defineI18nConfig(() => ({
legacy: false,
locale: 'en'
}))
8 changes: 8 additions & 0 deletions specs/fixtures/issues/2226/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
i18n: {
locales: ['en', 'de'],
defaultLocale: 'en',
strategy: 'prefix_and_default'
}
})
14 changes: 14 additions & 0 deletions specs/fixtures/issues/2226/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "nuxt3-test-issues-2226",
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"@nuxtjs/i18n": "latest",
"nuxt": "latest"
}
}
8 changes: 8 additions & 0 deletions specs/fixtures/issues/2226/pages/about.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
This is about page.
<NuxtLink id="goto-index" :to="localePath({ name: 'index' })">To home page</NuxtLink>
</template>

<script setup>
const localePath = useLocalePath()
</script>
8 changes: 8 additions & 0 deletions specs/fixtures/issues/2226/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
This is home page.
<NuxtLink id="goto-about" :to="localePath({ name: 'about' })">To about page</NuxtLink>
</template>

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

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

test('navigate on `prefix_and_default`', async () => {
const home = url('/')
const page = await createPage()
await page.goto(home)

await page.locator('#lang-switch').click()
expect(await getText(page, '#default-locale')).include(`Default locale: false`)

await page.locator('#goto-about').click()
expect(await getText(page, '#content')).include(`This is about page. To home page`)

await page.locator('#goto-index').click()
expect(await getText(page, '#content')).include(`This is home page. To about pag`)

await page.locator('#lang-switch').click()
await page.locator('#goto-about').click()
expect(await getText(page, '#content')).include(`This is about page. To home page`)
})
})
5 changes: 2 additions & 3 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,10 @@ export function detectRedirect<Context extends NuxtApp = NuxtApp>(
// locale, in which case we might still redirect as we prefer unprefixed route in this case.
(routeLocaleGetter(route) !== targetLocale || (strategy === 'prefix_and_default' && targetLocale === defaultLocale))
) {
// the current route could be 404 in which case attempt to find matching route using the full path since
// "switchLocalePath" can only find routes if the current route exists.
const { fullPath } = route
const decodedRoute = decodeURI(fullPath)
const routePath = context.$switchLocalePath(targetLocale) || context.$localePath(fullPath, targetLocale)
// the current route could be 404 in which case attempt to find matching route using the full path
const routePath = context.$localePath(fullPath, targetLocale)
__DEBUG__ && console.log('detectRedirect: calculate routePath -> ', routePath, fullPath)
if (
isString(routePath) &&
Expand Down

0 comments on commit 2e108fa

Please sign in to comment.