getRouterParams(event) is always empty in nitro middlewares
Environment
nuxt@latest
Reproduction
https://stackblitz.com/edit/github-fxp3q3?file=middleware%2Fexample.js
Describe the bug
const params = getRouterParams(event) is always empty in nitro middlewares
Additional context
No response
Logs
No response
To me, this behavior makes sense as the middleware runs before the route handlers, meaning that the middleware might not have any knowledge about possible router params.
@manniL I'm not sure, in theory I agree, but we do have access to the URL, even the nuxt example is:
// Example nuxt middleware from docs
export default defineEventHandler((event) => {
console.log('New request: ' + getRequestURL(event))
})
I have a lot of use cases where if I have [_book_id] in the URL I want to make sure the user has access to the the book, the middleware would be a perfect place to do this, so I can be sure any route which has [_book_id] is automatically validated.
If you do have access to the URL, you should have also access to the params, no?
Access to the URL makes totally sense, but as middleware is triggered before any router matching logic happens (AFAIK), the event is not aware of any route parameters yet (when using global middleware)
So would there be a problem if the middleware was given knowledge of the URL that made it to run, and of course the previous one?
Access to the URL makes totally sense, but as middleware is triggered before any router matching logic happens (AFAIK), the event is not aware of any route parameters yet (when using global middleware)
The middleware already knows the URL of the request :)
@manniL Should this be resolved, and router params are able to be read from the event parameter in nitro middlewares, would it impose any security vulnerability?
Any updates?