nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.75k stars 483 forks source link

Using @nuxtjs/i18n, rootRedirect is set, causing the page to throw a exception. #2710

Closed ZIFEIYU2023 closed 9 months ago

ZIFEIYU2023 commented 10 months ago

Environment

"@nuxtjs/i18n": "^8.0.0",
"nuxt": "^3.9.3",
"vue": "^3.4.14",
"vue-router": "^4.2.5"

Reproduction

https://codesandbox.io/p/devbox/runtime-violet-ns33vj

Describe the bug

// nuxt.config.ts  like this 
export default defineNuxtConfig({
    devtools: {enabled: true},
    modules: [
        '@nuxtjs/i18n',
    ],
    i18n: {
        defaultLocale: 'en',
        strategy: 'prefix',
        locales: [
            {code: 'en', iso: 'en', file: './locales/en.json'},
            {code: 'zh', iso: 'zh-Hans', file: './locales/zh.json'}
        ],
        detectBrowserLanguage: {
            fallbackLocale: 'en',
            useCookie: true,
            cookieKey: 'locale',
            redirectOn: 'root',  
        },
        rootRedirect: {
            statusCode: 301,
            path: 'home'
        }
    }
})

启动项目之后首次访问 localhost:3000 ,url会自动跳转到 http://localhost:3000/en/home 但会报500异常 。

500
[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at `https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables`.

at Module.useRequestEvent (./node_modules/nuxt/dist/app/composables/ssr.js:7:58)
at readRawCookies (./node_modules/nuxt/dist/app/composables/cookie.js:98:101)
at Module.useCookie (./node_modules/nuxt/dist/app/composables/cookie.js:25:19)
at Module.setLocaleCookie (./node_modules/@nuxtjs/i18n/dist/runtime/internal.mjs:115:46)
at composer.setLocaleCookie (./node_modules/@nuxtjs/i18n/dist/runtime/plugins/i18n.mjs:160:72)
at Module.callVueI18nInterfaces (./node_modules/@nuxtjs/i18n/dist/runtime/internal.mjs:27:18)
at setCookieLocale (./node_modules/@nuxtjs/i18n/dist/runtime/utils.mjs:26:32)
at Module.loadAndSetLocale (./node_modules/@nuxtjs/i18n/dist/runtime/utils.mjs:89:5)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

之后我刷新页页面之后,才变成正常状态

Additional context

No response

Logs

500
[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function. This is probably not a Nuxt bug. Find out more at `https://nuxt.com/docs/guide/concepts/auto-imports#vue-and-nuxt-composables`.

at Module.useRequestEvent (./node_modules/nuxt/dist/app/composables/ssr.js:7:58)
at readRawCookies (./node_modules/nuxt/dist/app/composables/cookie.js:98:101)
at Module.useCookie (./node_modules/nuxt/dist/app/composables/cookie.js:25:19)
at Module.setLocaleCookie (./node_modules/@nuxtjs/i18n/dist/runtime/internal.mjs:115:46)
at composer.setLocaleCookie (./node_modules/@nuxtjs/i18n/dist/runtime/plugins/i18n.mjs:160:72)
at Module.callVueI18nInterfaces (./node_modules/@nuxtjs/i18n/dist/runtime/internal.mjs:27:18)
at setCookieLocale (./node_modules/@nuxtjs/i18n/dist/runtime/utils.mjs:26:32)
at Module.loadAndSetLocale (./node_modules/@nuxtjs/i18n/dist/runtime/utils.mjs:89:5)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
BobbieGoede commented 10 months ago

Thanks for reporting your issue, unfortunately I'm unable to open your reproduction as I'm getting the following response on page load "Sandbox not found - It's likely that the sandbox you're trying to access doesn't exist or you don't have the required permissions to access it.".

ZIFEIYU2023 commented 10 months ago

https://codesandbox.io/p/devbox/runtime-violet-ns33vj @BobbieGoede Sorry, Please visit this link

esorribas commented 9 months ago

Same error when fallbackLocale is different of defaultLocale without rootRedirect