Open RohinBhargava opened 2 months ago
This seems to occur because the id is incrementally generated at runtime to maintain the assignment order. The problem is that making any changes to this behavior might be a breaking change unless you have a potential solution which solves your issue without breaking the current global behavior?
Yeah, I think when the router is instantiated, the id becomes 0. I think in the compilation, step, we can add the inline middlewares to the end of the middleware inventory by assigning them an id + an offset, where the offset is the max of the global/router middlewares, as suggested above. Thoughts on that?
I am observing some unexpected behavior when using specific route middlewares in a spread style pattern in a Router:
Scenario 1:
Consider the following code:
When calling
/test
, this results in:Scenario 2:
Now if we use the code:
The above code outputs the correct execution order:
Analysis:
I believe this is due to the compile() function on app startup. When using the spread style arguments in scenario 1, the execution order before and after sorting
middlewares.sort((a, b) => a.id - b.id);
is:In scenario 2, the before/after looks correct:
I believe this is happening because when compiling a route, the line
id: this.id
picks up the id from the Server, which is behind theapp_middlewares
(?). A patch that works for me is to calculate the max id from the global middlewares and then add that to the id in the specific middlewares section of the compile function. E.g.: