fix: run onBeforeResponse middleware in declared order#2204
Open
brenelz wants to merge 1 commit into
Open
Conversation
h3 v2 middleware compose onion-style, so logic after `await next()` unwinds innermost-first (reverse registration order). The onBeforeResponse wrappers do their work after awaiting next(), which made them execute backwards relative to the array the user passed to createMiddleware. Register the wrappers reversed so the callbacks run in their declared order, matching v1 behavior. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: c3d63de The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for solid-start-landing-page ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
commit: |
brenelz
commented
Jul 17, 2026
| // h3 middleware compose onion-style: logic after `await next()` unwinds | ||
| // innermost-first (reverse registration order). Register the wrappers | ||
| // reversed so onBeforeResponse functions run in their declared order. | ||
| mw.push(...args.onBeforeResponse.map(wrapResponseMiddleware).reverse()); |
Contributor
Author
There was a problem hiding this comment.
@katywings you probably know the most about middleware. Does this change make sense?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2131
What is the current behavior?
onBeforeResponsefunctions passed tocreateMiddlewareexecute in reverse array order. h3 v2 middleware compose onion-style, so logic afterawait next()unwinds innermost-first — andwrapResponseMiddlewaredoes its work after awaitingnext(), as noted by @sabercoy in the issue.What is the new behavior?
The response wrappers are registered reversed, so the
onBeforeResponsecallbacks run in the order they were declared, matching v1 semantics.onRequestordering is unchanged.Other information
Added
packages/start/src/middleware/index.spec.tscovering onRequest order, onBeforeResponse order (fails without this fix), request→handler→response sequencing, and response replacement from an onBeforeResponse middleware.🤖 Generated with Claude Code