Skip to content

Commit

Permalink
fix: statusCode in rootRedirect not working with strategy 'prefix' (
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Feb 14, 2024
1 parent 06b4e25 commit 933a092
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 2 deletions.
7 changes: 7 additions & 0 deletions specs/fixtures/issues/2758/app.vue
@@ -0,0 +1,7 @@
<script setup lang="ts"></script>

<template>
<div>
<NuxtPage />
</div>
</template>
23 changes: 23 additions & 0 deletions specs/fixtures/issues/2758/nuxt.config.ts
@@ -0,0 +1,23 @@
// https://nuxt.com/docs/guide/directory-structure/nuxt.config
export default defineNuxtConfig({
modules: ['@nuxtjs/i18n'],
debug: false,
i18n: {
defaultLocale: 'en',
strategy: 'prefix',
rootRedirect: {
statusCode: 418,
path: 'test-route'
},
locales: [
{
code: 'en',
iso: 'en-US'
},
{
code: 'fr',
iso: 'fr-FR'
}
]
}
})
14 changes: 14 additions & 0 deletions specs/fixtures/issues/2758/package.json
@@ -0,0 +1,14 @@
{
"name": "nuxt3-test-basic-usage-layers",
"private": true,
"type": "module",
"scripts": {
"dev": "nuxi dev",
"build": "nuxt build",
"start": "node .output/server/index.mjs"
},
"devDependencies": {
"@nuxtjs/i18n": "latest",
"nuxt": "latest"
}
}
3 changes: 3 additions & 0 deletions specs/fixtures/issues/2758/pages/index.vue
@@ -0,0 +1,3 @@
<template>
<h1>Home</h1>
</template>
3 changes: 3 additions & 0 deletions specs/fixtures/issues/2758/pages/test-route.vue
@@ -0,0 +1,3 @@
<template>
<div>Test route</div>
</template>
15 changes: 15 additions & 0 deletions specs/issues/2758.spec.ts
@@ -0,0 +1,15 @@
import { test, expect, describe } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, fetch, url } from '../utils'

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

test('`statusCode` in `rootRedirect` should work with strategy "prefix"', async () => {
const res = await fetch(url('/'))
expect(res.status).toEqual(418)
expect(res.headers.get('location')).toEqual('/en/test-route')
})
})
5 changes: 4 additions & 1 deletion src/runtime/plugins/i18n.ts
Expand Up @@ -178,6 +178,7 @@ export default defineNuxtPlugin({
async () =>
await navigate(
{
nuxtApp: nuxtContext,
i18n,
redirectPath,
locale,
Expand Down Expand Up @@ -424,7 +425,9 @@ export default defineNuxtPlugin({

routeChangeCount++

return await nuxtContext.runWithContext(async () => navigate({ i18n, redirectPath, locale, route: to }))
return await nuxtContext.runWithContext(async () =>
navigate({ nuxtApp: nuxtContext, i18n, redirectPath, locale, route: to })
)
}),
{ global: true }
)
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/utils.ts
Expand Up @@ -321,6 +321,7 @@ function isRootRedirectOptions(rootRedirect: unknown): rootRedirect is RootRedir
const useRedirectState = () => useState<string>(NUXT_I18N_MODULE_ID + ':redirect', () => '')

type NavigateArgs = {
nuxtApp: NuxtApp
i18n: I18n
redirectPath: string
locale: string
Expand All @@ -339,7 +340,7 @@ export async function navigate(
const differentDomains = nuxtI18nOptions.differentDomains ?? nuxtI18nOptionsDefault.differentDomains
const skipSettingLocaleOnNavigate =
nuxtI18nOptions.skipSettingLocaleOnNavigate ?? nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate
const { i18n, locale, route } = args
const { nuxtApp, i18n, locale, route } = args
let { redirectPath } = args

__DEBUG__ &&
Expand All @@ -360,6 +361,7 @@ export async function navigate(
redirectPath = '/' + rootRedirect.path
status = rootRedirect.statusCode
}
redirectPath = nuxtApp.$localePath(redirectPath, locale)
__DEBUG__ && console.log('navigate: rootRedirect mode redirectPath -> ', redirectPath, ' status -> ', status)
return _navigate(redirectPath, status)
}
Expand Down

0 comments on commit 933a092

Please sign in to comment.