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
According to @ReaganM’s tip, the error lies in the middleware:
I am redirecting to
to.name
, the problem is that the name of the nested route is in my case built likeFolder-File
Admin-Users
. So I changed this part of the code and redirected toto.path
and it worked great.Final middleware