I'm trying to ensure one middleware runs before another in my code. Is there a way to enforce the ordering? In particular, I'd like to run a middleware before another known middleware.
I can try ordering the registration of the middleware plugin. However, isn't this going to be impacted by the ordering of the dependencies? I'm not so sure that this is not going to be brittle.
You could add the first middleware as a dependency of the second. The pre-rendering phase of the first middleware will be executed first, while the post-rendering phase it'll execute second. See the below graphic.
Request lifecycle
Note: In case you don't own the plugin you want to run second, you won't be able to update its dependencies, so you could do the following.
Imagine having these plugins:
Instead of registering plugin 2, you could create a wrapper for it and add plugin 1 as dep: