! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡·
! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡·
Explore posts from servers
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 4/1/2024 in #❓・help
Middleware called 3 times instead of 2 ? (stackblitz reproduction)
Here my real middleware I think you will understand
import type { FetchApiRedirectInterface } from '~/assets/js/interfaces/FetchApiRedirectInterface'

export default defineNuxtRouteMiddleware(async (to, from) => {
if (process.client && from && from.meta.key === to.meta.key) return
const nuxtApp = useNuxtApp()
if (nuxtApp.isHydrating) return

const mainResourceCategorySlug = to.params
.main_resource_category_slug as string | undefined
const resourceCategorySlug = to.params.resource_category_slug as
| string
| undefined

if (!mainResourceCategorySlug && !resourceCategorySlug) {
return
}

const categoryStore = useCategoryStore()

// Send slug to API and check if it's fine or redirection needed
const response = await useCheckResourceCategorySlug({
category_id: categoryStore.category!.id,
main_resource_category_slug: mainResourceCategorySlug,
resource_category_slug: resourceCategorySlug ?? null,
fetch_type: 'fetch', // its use useFetch here
})

const data = response.data.value as FetchApiRedirectInterface


const localePath = useLocalePath()
if (!data) {
return navigateTo(localePath('/'))
}

if (data.redirect) {
return navigateTo(localePath(data.redirect), {
redirectCode: data.code,
})
}
})
import type { FetchApiRedirectInterface } from '~/assets/js/interfaces/FetchApiRedirectInterface'

export default defineNuxtRouteMiddleware(async (to, from) => {
if (process.client && from && from.meta.key === to.meta.key) return
const nuxtApp = useNuxtApp()
if (nuxtApp.isHydrating) return

const mainResourceCategorySlug = to.params
.main_resource_category_slug as string | undefined
const resourceCategorySlug = to.params.resource_category_slug as
| string
| undefined

if (!mainResourceCategorySlug && !resourceCategorySlug) {
return
}

const categoryStore = useCategoryStore()

// Send slug to API and check if it's fine or redirection needed
const response = await useCheckResourceCategorySlug({
category_id: categoryStore.category!.id,
main_resource_category_slug: mainResourceCategorySlug,
resource_category_slug: resourceCategorySlug ?? null,
fetch_type: 'fetch', // its use useFetch here
})

const data = response.data.value as FetchApiRedirectInterface


const localePath = useLocalePath()
if (!data) {
return navigateTo(localePath('/'))
}

if (data.redirect) {
return navigateTo(localePath(data.redirect), {
redirectCode: data.code,
})
}
})
When i try to go on a page with wrong slugs, here this middleware is calling 3 times. If I put everything here in my setup page instead of here my middleware, api is called only two times
15 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 4/1/2024 in #❓・help
Middleware called 3 times instead of 2 ? (stackblitz reproduction)
My middleware is making an api request to check slugs in the link. If I put this logic in my middleware, it does 3 API calls. If I put this logic in page setup, only two (as intended). What i'm understing in Nuxt, middleware shouldn't be used If I have redirection and API call ? If I use middleware for that atm, it does one more call for nothing
15 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 4/1/2024 in #❓・help
Middleware called 3 times instead of 2 ? (stackblitz reproduction)
Like it works If I put the script in the setup instead of the middleware
15 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 4/1/2024 in #❓・help
Middleware called 3 times instead of 2 ? (stackblitz reproduction)
Only A is hard loading. It should be A -> A -> Middleware -> Redirecting to B -> Middleware -> Showing B
15 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 4/1/2024 in #❓・help
Middleware called 3 times instead of 2 ? (stackblitz reproduction)
.
15 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 4/1/2024 in #❓・help
Middleware called 3 times instead of 2 ? (stackblitz reproduction)
I updated nuxt to 3.11.1 and same issue here https://stackblitz.com/edit/github-3enzca-ixdqzf
15 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 4/1/2024 in #❓・help
Middleware called 3 times instead of 2 ? (stackblitz reproduction)
Issue still here
15 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 4/1/2024 in #❓・help
Middleware called 3 times instead of 2 ? (stackblitz reproduction)
Ill update reproduction but atm in my local project i have the last vr
15 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 3/1/2024 in #❓・help
Hydratation issue using pinia store (stackblitz reproduction)
I solve it in a different way but I don't remember on how i did it
4 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 3/25/2024 in #❓・help
Nested routes but not for all pages
No description
2 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 3/11/2024 in #❓・help
[useFetch] Component is already mounted, please use $fetch instead (in middleware)
.
10 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 3/11/2024 in #❓・help
[useFetch] Component is already mounted, please use $fetch instead (in middleware)
That's why I came here
10 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 3/11/2024 in #❓・help
[useFetch] Component is already mounted, please use $fetch instead (in middleware)
Using $fetch in middleware is calling twice. First time on server, second time when the page is renderer the first time. Maybe could i use
if (useNuxtApp().isHydrating) return
if (useNuxtApp().isHydrating) return
10 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 3/11/2024 in #❓・help
[useFetch] Component is already mounted, please use $fetch instead (in middleware)
I'm here for explanations. πŸ™‚ (I already read the error)
10 replies
NNuxt
Created by ! GhostvOne.tv πŸ‘»πŸ‡«πŸ‡· on 3/1/2024 in #❓・help
Hydratation issue using pinia store (stackblitz reproduction)
.
4 replies