Nuxt3 - Nested page structure not working properly
P粉842215006
P粉842215006 2024-03-27 22:12:41
0
1
432

I wanted to nest routes in NUXT 3, so I created this file/folder structure (showing only the parts I'm having trouble with):

- Pages
   - Admin
     - Index.vue
     - Users
       - Index.vue
       - OtherRoute.vue

This folder structure works fine when I navigate through - I can access pages like this:

- domain.com/admin (Pages/Admin/index.vue)
- domain.com/admin/users (Pages/Admin/Users/index.vue)
- domain.com/admin/users/otherRoute (Pages/Admin/Users/otherRoute.vue)

The problem is that when I reload the page or directly access the url domain.com/admin/users NUXT it changes it to domain.com/admin/admin-users

For some reason NUXT cannot resolve this url when accessed directly instead of via NuxtLink and throws a 404 error. Is there a way to have a page structure like this and resolve unexpected path changes?

Edit 1 What happened video

Edit 2

Users.vue Page

<template>
    <TitleH1>Users</TitleH1>
    <TileList :tiles='tiles'></TileList>
</template>

<script setup>
    definePageMeta({
        layout: "logged",
    });

    const tiles = [
        {title: 'Users', icon: 'ic:baseline-supervisor-account', to: '/admin/users'},
        {title: 'Roles', icon: 'material-symbols:lock-person', to: '/admin/roles'},
        {title: 'Units', icon: 'ph:tree-structure', to: '/admin/units'},
    ]
</script>

logged.vue(layout)

<template>
    <NavigationMain :key="$route.name" />
    <div class="flex">
        <NavigationSide />
        <div class="w-full">
            <div class="px-4 md:mx-auto xl:px-36">
                <Breadcrumb />
                <slot></slot>
            </div>
            <NotificationHub />
        </div>
    </div>
</template>

auth.global.js (the only middleware that runs on this page)

export default defineNuxtRouteMiddleware(async (to, from) => {
    const { isLogged, proceedAutoLogin } = useAuthStore();
        if (!isLogged){
            if (to.name !== 'login'){
                if (await proceedAutoLogin()){
                    return navigateTo(to.name)
                }else{
                    abortNavigation()
                    return navigateTo('/login')
                } 
            }
        }else{
            if (!await proceedAutoLogin()){
                abortNavigation()
                return navigateTo('/login')
            }
        }
  })

Thanks

P粉842215006
P粉842215006

reply all(1)
P粉821231319

According to @ReaganM’s tip, the error lies in the middleware:

export default defineNuxtRouteMiddleware(async (to, from) => {
    const { isLogged, proceedAutoLogin } = useAuthStore();
        if (!isLogged){
            if (to.name !== 'login'){
                if (await proceedAutoLogin()){
                    return navigateTo(to.name)
                }else{
                    abortNavigation()
                    return navigateTo('/login')
                } 
            }
        }else{
            if (!await proceedAutoLogin()){
                abortNavigation()
                return navigateTo('/login')
            }
        }
  })

I am redirecting to to.name, the problem is that the name of the nested route is in my case built like Folder-FileAdmin-Users . So I changed this part of the code and redirected to to.path and it worked great.

Final middleware

export default defineNuxtRouteMiddleware(async (to, from) => {
    const { isLogged, proceedAutoLogin } = useAuthStore();
        if (!isLogged){
            if (to.name !== 'login'){
                if (await proceedAutoLogin()){
                    console.log(to)
                    return navigateTo(to.path)
                }else{
                    abortNavigation()
                    return navigateTo('/login')
                } 
            }
        }else{
            if (!await proceedAutoLogin()){
                abortNavigation()
                return navigateTo('/login')
            }
        }
  })
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template