Middlewares run in unexpected order
See original GitHub issueUnexpected to me, anyway 😀 Wanted to raise an issue before attempting a PR, in case this is the desired behaviour.
Repro: https://github.com/Rich-Harris/polka-middleware-order-repro
Essentially, it seems that middlewares without a basepath run before middlewares with one, regardless of the order in which they’re added to the app. This means that a generic app.use(foo)
will take precedence over an earlier app.use(basepath, serve('assets'))
, which can prevent assets from being served.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Express router middleware gets called on unexpected routes
A router is just inserted into a chain of request handlers and is searched/executed in order. Because you do this: app.use(router).
Read more >Unexpected route/middleware execution order #26 - GitHub
Looking at the source comments, it seems that routes are parsed before middleware is run. Is there a reason for that? To get...
Read more >Express Middlewares, Demystified - Medium
In case of an unexpected error, it can throw the error or call the next() function by passing the error as its first...
Read more >Middleware - Redux
To ensure that you may only apply middleware once, it operates on createStore() rather than on store itself. Instead of (store, middlewares) => ......
Read more >Middlewares – GraphQL Modules
Without strict rules on the order of execution, you might get unexpected results. -> Application *.* -> Module *.* -> Application Type.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for helping me think this stuff through, by the way 😀
You’re absolutely right, Sapper should only deal with requests pertaining to its basepath. In fact, that insight led me to a much nicer way of doing things — now, all you need to do to mount an app on a basepath is this:
No configuration or environment variables necessary, it just works!
So I’ll close this issue rather than continuing to clutter up your tracker. The one thing that does give me pause is that I often order my middlewares to do this sort of thing:
With Express, requests handled by earlier middlewares don’t get handled by later middlewares (unless the earlier middleware wants that to happen, with
next()
), and controlling that is fairly intuitive. If I understand correctly, that’s a little harder with Polka? You’ve spent more time thinking about this than me though, so your way probably makes more sense — I only mention it because it’s a potential sticking point for Express refugees.Great! Happy to help~! And not clutter at all haha
My general recommendation is that if you have a particular exception/filter, that you should manually filter that route from the global scope. That is, of course, assuming you’re using an off-the-shelf solution and can’t work in the behavior into the middleware directly.
Looking at the
morgan
logger example, you could do something like this:Although, most of the big-name loggers out there already include some kind of skip/ignore option. Morgan, for example, has
opts.skip
, which essentially does the same thing.