nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Middleware registered in plugins is never executed

Open FeldrinH opened this issue 1 year ago • 9 comments

Environment

nitropack v0.5.4, no extra config

Reproduction

Given the following plugin:

// plugins/test.ts
export default defineNitroPlugin(nitroApp => {
    nitroApp.h3App.use('/', () => console.log('Middleware ran'))
    console.log('Plugin registered')
})

The plugin appears to register correctly ('Plugin registered' is printed), but the middleware added with h3App.use is never executed ('Middleware ran' is never printed when navigating to any route).

Describe the bug

Any middleware added inside a nitro plugin with nitroApp.h3App.use or other related methods is never executed. This might be an intentional / known limitation of how plugins work, but it feels unexpected, given that the method throws no error and is readily accessible.

Additional context

No response

Logs

No response

FeldrinH avatar Oct 08 '22 16:10 FeldrinH

See https://github.com/unjs/nitro/pull/302 and https://github.com/nuxt/framework/issues/5527#issuecomment-1160789814.

danielroe avatar Oct 09 '22 14:10 danielroe

Those issues appear to be about adding new routes, not middleware. Unless there is some way to add middleware directly to the router that I'm unaware of, I don't really get why this issue was closed.

FeldrinH avatar Oct 09 '22 15:10 FeldrinH