Allow running configured middlewares before scanned middlewares
#26.012 aperta il 29 feb 2024
Metriche repository
- Star
- (60.221 star)
- Metriche merge PR
- (Merge medio 3g 7h) (114 PR mergiate in 30 g)
Descrizione
Describe the feature
The module I'm working on includes a server middleware component which adds properties to the event.context. Consumers of the module may also want to develop server middleware which depends on these properties. For this to work, we need to ensure that the application's server middleware runs after the module's.
It's possible to register the middleware as the first handler by directly setting the serverHandlers option instead of using addServerHandler. For example:
nuxt.options.serverHandlers.unshift({
middleware: true,
handler: resolve('./runtime/server/middleware/bootstrap'),
})
But it appears that any middlewares automatically scanned from the middlewares directory, will still be executed first. https://github.com/unjs/nitro/blob/3cdf3d96e02c2765e7269f6054418982dd876a09/src/rollup/plugins/handlers.ts#L6 The current workaround is to require applications to not use the auto-scanning and instead add serverHandlers in their nuxt.config, but this is non-obvious and goes against the default approach.
Is there a better way that the priorities of server middlewares could be explicitly defined?
Alternatively, maybe it makes sense to default to executing scannedMiddlewares last? I think that would make the workflow a bit easier. If you care about the execution order, you manually set the nitro config and if the ordering is not important you can then rely on the scanning.
(Also, I'm not sure if this would be better as a Nitro issue. Please let me know if I should refile elsewhere. :)The code I linked is from Nitro, but perhaps this use case could be solved at the Nuxt level)
Additional information
- Would you be willing to help implement this feature?
- Could this feature be implemented as a module?
Final checks
- Read the contribution guide.
- Check existing discussions and issues.