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.

[Document] Order of middlewares

See original GitHub issue

I just started a project with starlette awesome, and found a small issue about the docs related on the order of execution of middlewares… My case, is so usual, like in django:

auth_middleware that depends on session_middleware

Seems like the order is inverse on the order you declare them:

app.add_middleware(AuthenticationMiddleware, backend=middlewares.AuthBackend())
app.add_middleware(middlewares.SessionMiddleware)

The session one is executed first but needs to be added later. I’m not against it, it’s an onion, and can be seen in both directions… But I thought that at least should be documented.

On the API side, perhaps is a nice idea, to just instantiate them on a single call, like:

app.add_middlewares([
   middlewareX, middlewareY  # in order of execution... (at least when the request is entering..)
])

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
invokermaincommented, Aug 19, 2019

had the same issue, just adding some documentation would be great as this is a pretty opaque bug to fix.

1reaction
aminalaeecommented, Jan 16, 2022

I think this was achieved in Version 0.13.0. From the current docs:

Every Starlette application automatically includes two pieces of middleware by default:

  • ServerErrorMiddleware - Ensures that application exceptions may return a custom 500 page, or display an application traceback in DEBUG mode. This is always the outermost middleware layer.
  • ExceptionMiddleware - Adds exception handlers, so that particular types of expected exception cases can be associated with handler functions. For example raising HTTPException(status_code=404) within an endpoint will end up rendering a custom 404 page.

Middleware is evaluated from top-to-bottom, so the flow of execution in our example application would look like this:

  • Middleware
    • ServerErrorMiddleware
    • TrustedHostMiddleware
    • HTTPSRedirectMiddleware
    • ExceptionMiddleware
  • Routing
  • Endpoint

So this can be closed now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does order matter in a middleware file? - Stack Overflow
Order matters in middleware because middleware functions are executed in a sequence and you'd need to execute one before the other in order...
Read more >
Writing middleware for use in Express apps
The order of middleware loading is important: middleware functions that are loaded first are also executed first. If myLogger is loaded after the...
Read more >
Order of middleware | Mastering ASP.NET Web API
The pictorial representation of request processing describes the execution order of middleware. The sequence order is created by placement of the Use ,...
Read more >
ASP.NET Core Middleware | Microsoft Learn
The order that middleware components are added in the Program.cs file defines the order in which the middleware components are invoked on ...
Read more >
Document middleware order - Caddy Community
Document middleware order ... For debugging uses especially considering the increasing 3rd party middlewares coming with the new site it might ...
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