un1t
@un1t

Nuxt 3 — как пофиксить предупреждение «onServerPrefetch is called when there is no active component instance to be associated with»?

Как пофиксить предупреждение?

onServerPrefetch is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.


middleware/auth.global.ts

export default defineNuxtRouteMiddleware(async (to, from) => {
    let authStore = useAuthStore();
    if (!authStore.loaded) {
        await authStore.fetchUser();
    }

    let isPrivate = to.fullPath.startsWith('/my/')
    if (isPrivate && !authStore.user) {
        return navigateTo({ path: '/login', query: { next: to.fullPath }})
    }
})


Варнинг возникает на строчке await authStore.fetchUser() внутри там юзается useFetch, см. ниже.

stores/auth.ts

import { defineStore } from 'pinia'

...

export const useAuthStore = defineStore('auth', {
    state: () => {
        return {
            user: null as User | null,
            loaded: false,
        }
    },
    actions: {
        async refresh() {
            try {
                const { data } = await useFetch<Response>('http://myserver/getme', {
                    key: "auth:fetch-user",
                    headers: useRequestHeaders(["cookie"]),
                })
                this.user = data.value.user
                this.loaded = true
            } catch (err) {
                ...
            }
        },
    },
})


Нельзя вызывать useFetch внутри миддлевари? А как тогда получить информацию, о пользователе?
  • Вопрос задан
  • 401 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы