最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nuxt i18n Calling useRoute within middleware may lead to misleading results - Stack Overflow

programmeradmin3浏览0评论

So I am getting this warning in nuxt3: Calling useRoute within middleware may lead to misleading results. Instead, use the (to, from) arguments passed to the middleware to access the new and old routes.

This happens because I am calling useLocalePath() in my middleware.

This is one of the middleware where it happens:

export default defineNuxtRouteMiddleware(async(to, from) => {
    const localPath = useLocalePath()

    const isUserAuthenticated = await isAuthenticated()

    if (isUserAuthenticated) {
        if (to.fullPath === localPath('login') || to.fullPath === localPath('register')) {
            return navigateTo(localPath('/'))
        }
    } else {
        if (to.fullPath !== localPath('login') && to.fullPath !== localPath('register')) {
            return navigateTo(localPath('login'))
        }
    }

})

I have this in my nuxt.config.ts:

i18n: {
        lazy: true,
        langDir: "locales",
        strategy: "prefix_and_default",
        locales: [
            {
                code: 'nl-Nl',
                iso: 'nl-Nl',
                name: 'Dutch',
                file: 'nl-NL.json'
            },
            {
                code: 'en',
                iso: 'en',
                name: 'English',
                file: 'en.json'
            },
        ],
        detectBrowserLanguage: {
            useCookie: true,
            cookieCrossOrigin: true,
            alwaysRedirect: true,
            cookieKey: 'i18n_redirected',
            redirectOn: 'root'
        },
        defaultLocale: "nl-Nl",
        customRoutes: 'config',
        pages: {
            pricing: {
                en: '/pricing',
                'nl-Nl': '/prijzen',
            }
        }
    }

This is the version of i18n that i'm using: "@nuxtjs/i18n": "^8.0.0-beta.12",

The thing is, the code is working perfectly fine, but I don't have any clue why it's giving me this warning.

Is it safe to ignore this warning?

So I am getting this warning in nuxt3: Calling useRoute within middleware may lead to misleading results. Instead, use the (to, from) arguments passed to the middleware to access the new and old routes.

This happens because I am calling useLocalePath() in my middleware.

This is one of the middleware where it happens:

export default defineNuxtRouteMiddleware(async(to, from) => {
    const localPath = useLocalePath()

    const isUserAuthenticated = await isAuthenticated()

    if (isUserAuthenticated) {
        if (to.fullPath === localPath('login') || to.fullPath === localPath('register')) {
            return navigateTo(localPath('/'))
        }
    } else {
        if (to.fullPath !== localPath('login') && to.fullPath !== localPath('register')) {
            return navigateTo(localPath('login'))
        }
    }

})

I have this in my nuxt.config.ts:

i18n: {
        lazy: true,
        langDir: "locales",
        strategy: "prefix_and_default",
        locales: [
            {
                code: 'nl-Nl',
                iso: 'nl-Nl',
                name: 'Dutch',
                file: 'nl-NL.json'
            },
            {
                code: 'en',
                iso: 'en',
                name: 'English',
                file: 'en.json'
            },
        ],
        detectBrowserLanguage: {
            useCookie: true,
            cookieCrossOrigin: true,
            alwaysRedirect: true,
            cookieKey: 'i18n_redirected',
            redirectOn: 'root'
        },
        defaultLocale: "nl-Nl",
        customRoutes: 'config',
        pages: {
            pricing: {
                en: '/pricing',
                'nl-Nl': '/prijzen',
            }
        }
    }

This is the version of i18n that i'm using: "@nuxtjs/i18n": "^8.0.0-beta.12",

The thing is, the code is working perfectly fine, but I don't have any clue why it's giving me this warning.

Is it safe to ignore this warning?

Share Improve this question edited Aug 3, 2023 at 22:52 Moemen Hussein asked Aug 3, 2023 at 22:41 Moemen HusseinMoemen Hussein 5491 gold badge10 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

This works for me

export default defineNuxtRouteMiddleware(async(to, from) => {
    const nuxt = useNuxtApp()

    const isUserAuthenticated = await isAuthenticated()

    if (isUserAuthenticated) {
        if (to.fullPath === nuxt.$localePath('login') || to.fullPath === nuxt.$localePath('register')) {
            return navigateTo(nuxt.$localePath('/'))
        }
    } else {
        if (to.fullPath !== nuxt.$localePath('login') && to.fullPath !== nuxt.$localePath('register')) {
            return navigateTo(nuxt.$localePath('login'))
        }
    }

})

useLocalePath() uses the useRoute() method under the hood that's why you are getting that warn. one solution could be to use useI18n() instead and accesing the App locale variable.

const locale = useNuxtApp().$i18n.locale;

if (!to.fullPath.includes("login")) {
  return navigateTo(`${locale.value}/login`);
}

or just add a re-usable posable.

export default function useTranslateUrl(url: string): string {
  const locale = useNuxtApp().$i18n.locale;
  return `/${locale.value}${url.startsWith("/") ? url : `/${url}`}`;
}

Note: make sure your locales are never in an iso format like en-US, unless your whole i18n configuration is setup that way.

发布评论

评论列表(0)

  1. 暂无评论