fix(metadata): preserve Content-Length for fully buffered responses#2703
Open
tomvoss wants to merge 1 commit into
Open
fix(metadata): preserve Content-Length for fully buffered responses#2703tomvoss wants to merge 1 commit into
tomvoss wants to merge 1 commit into
Conversation
closeAfterResponseWithBody() wraps every body-bearing response in a TransformStream to support function-form after(), which strips Content-Length even when the body is already fully materialized. Metadata file convention responses (robots(), sitemap(), manifest(), static icons) always serialize to a string or byte array with no producer left, so mark those bodies (markFullyBufferedBody) and skip the wrap when nothing is registered. Arbitrary Route Handler responses and a metadata route's Response passthrough stay wrapped: their body's producer may still call after() after the handler resolves, so a "nothing registered yet" check can't prove them safe. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TqasUFjUPt2Q8gkrzrLGyB
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.
Summary
closeAfterResponseWithBody()wraps every body-bearing App/Pages Routerresponse in a
TransformStreamso it can release deferredafter()workonce the body closes. That wrap strips
Content-Length, even for a responsevinext has already fully materialized in memory.
Metadata file convention responses (
robots(),sitemap(),manifest(),static icons) always serialize their body to a string or byte array before
returning — no producer is left that could still call
after(). This marksthose responses (
markFullyBufferedBody) and skips the wrap when nothing iscurrently registered, letting the runtime emit an accurate
Content-Lengthinstead of chunked transfer encoding.
Arbitrary Route Handler responses, and a metadata route's
Responsepassthrough (a user
icon.tsx/opengraph-image.tsxcan return a streamingImageResponse), are left unmarked — their body's producer can still callafter()after the handler resolves, so a "nothing registered yet" checkcan't prove them safe. Those stay close-tracked.
Test plan
tests/after-response-close-worker.test.ts(real Cloudflare Workersruntime via wrangler):
robots.txtandsitemap.xmlregainContent-Length; a hand-written Route Handler stays chunked; a RouteHandler returning
new Response(stream)still runs a lateafter()callcorrectly after the stream completes or is cancelled.
tests/shims.test.ts: unit coverage for the marker gate, and protectivetests proving a late
after()call from an unmarked stream waits forcompletion/cancellation and connects to
ExecutionContext.waitUntil.after()/dispatch, App/PagesRouter prod and dev servers) pass.
vp checkpasses. Both packages build.(
EXDEV) failures in this sandbox, unrelated to this change andreproducing identically on unmodified
main; not fully green.Limitations
Hand-written Route Handler responses stay close-tracked even when their
current body happens to be buffered, because a returned
ReadableStreammay keep producing and register
after()after the handler functionresolves. Safely optimizing those would need a separate, principled
buffered-body signal.