fix: run onBeforeResponse middleware in declared order#2204
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: 9452baf 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: |
| // 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()); |
There was a problem hiding this comment.
@katywings you probably know the most about middleware. Does this change make sense?
There was a problem hiding this comment.
Makes sense yuppa 👍.
What I wonder is why we arent using h3's onRequest and onResponse helpers 🤔. (onRequest does pretty much nothing, but onResponse would simplify our wrapResponseMiddleware)
|
Got some feedback from fable as well: Suggestions (minor, none blocking)
|
|
Addressed the feedback |
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