N
Nuxt3w ago
JuanMCe

middleware on nested routes

I have an admin/ route with its index.vue page and algo nested routes: admin/users admin/users/[id] I added a middleware in the root admin page but the nested routes don't inherit the middleware
9 Replies
nandi95
nandi953w ago
This is expected behaviour You can either add it on all page files under the admin folder or assign the middleware to all pages in the folder in a hook pages:extends from top of my head
kogratte
kogratte3w ago
@nandi95 are you sure about that? I'm currently trying, and it seems I can protect a subset of routes defining a middleware on parent, ie:
'/myPage.vue' // own middleware
'/myPage/somethingElse.vue' // seems to inherit middleware
'/myPage.vue' // own middleware
'/myPage/somethingElse.vue' // seems to inherit middleware
`
nandi95
nandi953w ago
yeah I'm sure, I'm open to be proven wrong tho
nandi95
nandi953w ago
Cake
Cake3w ago
you need to add the middleware in /pages/admin.vue then both /pages/admin/index.vue and /pages/admin/[id].vue should inherit it /pages/admin.vue rendering <NuxtPage />
JuanMCe
JuanMCeOP3w ago
@Cake ohh I thought about it but didn't try it, using an admin.vue as some sort of layout and nest the other pages just like vue-router Gonna give it a try, thx
kogratte
kogratte3w ago
@nandi95 any idea about how I can write a test to test this behavior, other than e2e?
nandi95
nandi953w ago
Nuxt unit testing sets up nuxt and runs plugins middlewares etc
manniL
manniL3w ago
You could use an env var or runtime config to bypass but I’d be careful

Did you find this page helpful?