question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middlewares run in unexpected order

See original GitHub issue

Unexpected 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:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Rich-Harriscommented, Mar 17, 2018

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:

polka()
  .use('/my-basepath', serve('assets'), sapper({routes}))
  .listen(PORT);

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:

app.use('/requests-that-should-not-be-logged', someMiddleware());
app.use(loggingMiddleware());

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.

0reactions
lukeedcommented, Mar 17, 2018

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:


const logger = morgan('dev'); // instantiate early

polka()
  .use((req, res, next) => {
    return req.url.includes('ignore-this') ? next() : logger(req, res, next);
  })
  .use('/items', require('./items')
  .use('/ignore-this', something())
  // ...

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found