Skip to content

Commit afea507

Browse files
authored
docs: add note on how to add a middleware with a constraint through a… (#4435)
1 parent 94073d3 commit afea507

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

docs/usage/middleware/creating-middleware.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ be the *last* middleware on the *last* layer (i.e. the route handler).
130130
]
131131
)
132132
133+
Constraints and plugins
134+
~~~~~~~~~~~~~~~~~~~~~~~
135+
136+
When using plugins that add middleware(s), it is important to understand that these
137+
middleware(s) are added *after* middlewares defined on the application and *before* middlewares defined on the other layers.
138+
139+
Constraints are evaluated after all middlewares have been added though, so an order constraint on a middleware added by
140+
a plugin has to take into account the position it is being added to.
141+
142+
Most of the time in a plugin you would do ``app_config.middleware.append(MyCustomMiddleware)`` which will be
143+
ok if it has no constraints attached.
144+
145+
Now suppose that ``MyCustomMiddleware`` has the constraint ``first=True`` then the correct way to add it is to do
146+
``app_config.middleware.insert(0, MyCustomMiddleware())`` so that it is the first middleware in the stack.
133147

134148

135149
Migrating from ``MiddlewareProtocol`` / ``AbstractMiddleware``

0 commit comments

Comments
 (0)