-
Notifications
You must be signed in to change notification settings - Fork 292
fix: apply middleware headers to intercept route and server action responses #762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
james-elicx
merged 3 commits into
cloudflare:main
from
NathanDrake2406:fix/middleware-headers-intercept-action
Apr 3, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9027123
fix: apply middleware headers to intercept route and server action re…
NathanDrake2406 d0bf61a
fix: merge middleware headers before x-action-redirect control headers
NathanDrake2406 c89252a
fix: update RSC entry snapshots for middleware headers
NathanDrake2406 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,6 +157,34 @@ export function resolveAppPageHtmlResponsePolicy( | |
| return { shouldWriteToCache: false }; | ||
| } | ||
|
|
||
| /** | ||
| * Merge middleware response headers into a target Headers object. | ||
| * | ||
| * Set-Cookie and Vary are accumulated (append) since multiple sources can | ||
| * contribute values. All other headers use set() so middleware owns singular | ||
| * response headers like Cache-Control. | ||
| * | ||
| * Used by buildAppPageRscResponse and the generated entry for intercepting | ||
| * route and server action responses that bypass the normal page render path. | ||
| */ | ||
| export function mergeMiddlewareResponseHeaders( | ||
| target: Headers, | ||
| middlewareHeaders: Headers | null, | ||
| ): void { | ||
| if (!middlewareHeaders) { | ||
| return; | ||
| } | ||
|
|
||
| for (const [key, value] of middlewareHeaders) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| const lowerKey = key.toLowerCase(); | ||
| if (lowerKey === "set-cookie" || lowerKey === "vary") { | ||
| target.append(key, value); | ||
| } else { | ||
| target.set(key, value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function buildAppPageRscResponse( | ||
| body: ReadableStream, | ||
| options: BuildAppPageRscResponseOptions, | ||
|
|
@@ -178,20 +206,7 @@ export function buildAppPageRscResponse( | |
| headers.set("X-Vinext-Cache", options.policy.cacheState); | ||
| } | ||
|
|
||
| if (options.middlewareContext.headers) { | ||
| for (const [key, value] of options.middlewareContext.headers) { | ||
| const lowerKey = key.toLowerCase(); | ||
| if (lowerKey === "set-cookie" || lowerKey === "vary") { | ||
| headers.append(key, value); | ||
| } else { | ||
| // Keep parity with the old inline RSC path: middleware owns singular | ||
| // response headers like Cache-Control here, while Set-Cookie and Vary | ||
| // are accumulated. The HTML helper intentionally keeps its legacy | ||
| // append-for-everything behavior below. | ||
| headers.set(key, value); | ||
| } | ||
| } | ||
| } | ||
| mergeMiddlewareResponseHeaders(headers, options.middlewareContext.headers); | ||
|
|
||
| applyTimingHeader(headers, options.timing); | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the action-redirect branch, the code builds
x-action-redirect*headers and then merges middleware headers afterward; becausemergeMiddlewareResponseHeaders()usesset()for most keys, middleware can overwrite these control headers. If a middleware setsx-action-redirect,x-action-redirect-type, orx-action-redirect-status(intentionally or via header passthrough), the client can navigate to the wrong URL or fail to process the redirect correctly. Merge middleware headers before writing the action redirect control headers, or explicitly protect these keys from override.Useful? React with 👍 / 👎.