Skip to content

Conversation

svozza
Copy link
Contributor

@svozza svozza commented Sep 19, 2025

Summary

Fixes bug where global middleware did not run on every request but only if a specific route had been registered. Route handlers are executed as part of a special middleware that is always appended to the end of the middleware stack, if a route handler was not found we would throw a NotFound error and skip the middleware altogether.

In the process of fixing this issue, I also found a bug where if a handler returned a Response object we did not merge the headers from the middleware that had run before into the res.headers object.

Changes

  • Removed the code that threw a not found error if a route handler was not found
  • Moved the logic to handle returning a 404 to the handler middleware. Returned a 404 Response object rather than throwing to allow middleware stack to run to completion as throwing stops processing completely.
  • Fixed a bug where headers were not merged if handler returned a Response object.
  • Added tests for new middleware behaviour but did not add a specific test for CORS middleware as this bug is not CORS specific so test lives in main middelware test suite.

Note: if testing the behaviour in a lambda you need to ensure that your OPTIONS request has the Access-Control-Request-Method header or it will not be treated as a pre-flight request and still return a 404.

Issue number: closes #4506


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Sep 19, 2025
@boring-cyborg boring-cyborg bot added event-handler This item relates to the Event Handler Utility tests PRs that add or change tests labels Sep 19, 2025
@svozza svozza self-assigned this Sep 19, 2025
@svozza svozza changed the title fix(event-hanlder): run global middleware on all requests for REST API fix(event-handler): run global middleware on all requests for REST API Sep 19, 2025
Copy link

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you - tested on the same website/setup I mentioned in the issue and it works!

@svozza svozza merged commit 49d5f8a into main Sep 19, 2025
38 checks passed
@svozza svozza deleted the cos-preflight-fix branch September 19, 2025 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event-handler This item relates to the Event Handler Utility size/L PRs between 100-499 LOC tests PRs that add or change tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: CORS middleware doesn't handle preflight OPTIONS requests automatically

2 participants