Skip to content

fix(cloudflare): Keep the http.server span open for streamed HTML and RSC responses - #24932

Open
JPeer264 wants to merge 5 commits into
developfrom
jp/cloudflare-streaming-response-detection
Open

JPeer264 wants to merge 5 commits into
developfrom
jp/cloudflare-streaming-response-detection

Conversation

@JPeer264

@JPeer264 JPeer264 commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

closes #22876
closes JS-3236

Description

This adds an isStreamingResponse option, so users can keep the http.server span open until a streamed response has been sent, for example for streamed SSR pages. Spans created while the body streams then stay inside the request.

  • text/x-component (RSC) responses without a Content-Length header now count as streamed by default. The classifier is shared, so this also applies to Bun and Deno.
  • Precompressed bodies with encodeBody: 'manual' are no longer compressed a second time.
  • Workers with a compatibility date before 2022-11-30 send the span again. They ignore the TransformStream transformer, so the SDK ends the span at handler return there.
export default defineCloudflareOptions(env => ({
  dsn: env.SENTRY_DSN, 
  isStreamingResponse: response =>
    response.headers.get('content-type')?.startsWith('text/html') ? true : undefined,
}));

Detection decisions

HTML is not streamed by default, because a buffered new Response(html) has no Content-Length header either and would also go through the TransformStream. The alternative is to treat HTML without a Content-Length as streamed, which fixes streamed SSR without configuration.

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.8 kB - -
@sentry/browser - with treeshaking flags 27.92 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.84 kB - -
@sentry/browser (incl. Tracing) 51.85 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 51.87 kB - -
@sentry/browser (incl. Tracing, Profiling) 54.8 kB - -
@sentry/browser (incl. Tracing, Replay) 91.58 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.45 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 96.29 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 109.27 kB - -
@sentry/browser (incl. Feedback) 47.32 kB - -
@sentry/browser (incl. sendFeedback) 34.84 kB - -
@sentry/browser (incl. FeedbackAsync) 39.95 kB - -
@sentry/browser (incl. Metrics) 30.82 kB - -
@sentry/browser (incl. Logs) 31.11 kB - -
@sentry/browser (incl. Metrics & Logs) 31.75 kB - -
@sentry/react 31.63 kB - -
@sentry/react (incl. Tracing) 54.17 kB - -
@sentry/vue 37.84 kB - -
@sentry/vue (incl. Tracing) 54.78 kB - -
@sentry/svelte 29.83 kB - -
@sentry/remix (Remix 3 client bundle) 56.83 kB - -
CDN Bundle 31.53 kB - -
CDN Bundle (incl. Tracing) 52.34 kB - -
CDN Bundle (incl. Logs, Metrics) 33.72 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 54.3 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 74.65 kB - -
CDN Bundle (incl. Tracing, Replay) 90.01 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 91.96 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 96.17 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 98.15 kB - -
CDN Bundle - uncompressed 92.92 kB - -
CDN Bundle (incl. Tracing) - uncompressed 155.46 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.46 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 161.41 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 229.5 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 275.63 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 281.57 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 289.33 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 295.26 kB - -
@sentry/nextjs (client) 56.53 kB - -
@sentry/sveltekit (client) 52.23 kB - -
@sentry/core/server 40.87 kB +0.04% +14 B 🔺
@sentry/core/browser 13.71 kB - -
@sentry/node 145.89 kB -0.38% -554 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.33 kB -0.06% -48 B 🔽
@sentry/node - without tracing 93.69 kB -0.06% -53 B 🔽
@sentry/node - without channel injection 124.04 kB -0.47% -579 B 🔽
@sentry/aws-serverless 101.91 kB -0.06% -56 B 🔽
@sentry/cloudflare (withSentry) - minified 209.78 kB +0.04% +75 B 🔺
@sentry/cloudflare (withSentry) 520.11 kB +0.04% +167 B 🔺
@sentry/nextjs/cloudflare (withSentry) - minified 227.45 kB +0.04% +74 B 🔺

View base workflow run

@JPeer264
JPeer264 added this pull request to stack #24934 October 1, 2026 11:32
@JPeer264

JPeer264 commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 4356442. Configure here.

@JPeer264
JPeer264 marked this pull request as ready for review October 1, 2026 13:44
@JPeer264
JPeer264 requested a review from a team as a code owner October 1, 2026 13:44
@JPeer264
JPeer264 requested review from isaacs and mydea and removed request for a team October 1, 2026 13:44
@JPeer264 JPeer264 self-assigned this Oct 1, 2026
@JPeer264
JPeer264 requested review from andreiborza and removed request for mydea October 1, 2026 13:44

@isaacs isaacs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The coercion of all buffered responses to streaming feels a bit risky without an escape hatch to work around it, since workerd responses don't have a corollary to Deno's info.completed.

I think this is a good compromise, but would recommend adding the explicit hook option for users that find they need to disable this in some cases.

/^application\/(x-)?ndjson\b/i.test(contentType) ||
/^application\/stream\+json\b/i.test(contentType) ||
(/^text\/plain\b/i.test(contentType) && !contentLength)
(/^text\/(plain|html|x-component)\b/i.test(contentType) && !contentLength)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, if I'm reading this right, if you have a handler returns new Response(htmlString), without setting a content-length, then it'll be turned into a streaming response, and get transfer-encoding: chunked.

The runtime knows the body length from the string and content-length only when it sends the response. So res.headers.get('content-length') returns null for a buffered string, an ArrayBuffer, c.html(), and other fully-known response bodies. Before this PR, text/html never matched the regex, so the length check didn't matter. Now any HTML without an explicit header counts as "streaming", whether it streams or not.

At least, we should describe this cost in the PR body and changelog, because it changes the wire output for users who aren't using streamed SSR. Also, maybe we could give users a way out? One option is the hook that the issue asked for (for example isStreamingResponse?: (res: Response) => boolean | undefined, where undefined means "use the default"). Then users with buffered HTML can opt out, and users with custom streamed types can opt in. If detection stays the only mechanism, a workerd benchmark (similar to the Deno one in the next PR in the stack) would show whether the CPU cost matters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, other streamed content types aren't covered, which could be a possible follow-up. Eg, React Router 7 / Remix single fetch sends .data requests as text/x-script (turbo-stream) and streams deferred promises after the handler returns.

The suggested user hook could be used as a workaround for those cases, as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That is absolutely right. I just removed the html one as it could be dangerous and added only text/x-component as this seems to be a safe default. The additional isStreamingResponse is also good and I added it.

};

// Without the `transformstream_enable_standard_constructor` compatibility flag (default from
// 2022-11-30, but Hydrogen's mini-oxygen uses 2022-10-31), workerd ignores the transformer, so

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I just checked, and Hydrogen's mini-oxygen is now using a compat date of 2025-04-01.

Suggestion: say "mini-oxygen before 4.0.0 (and any Worker with a compat date before 2022-11-30)" or just "Workers with a compat date before 2022-11-30 (for example older mini-oxygen releases)".

Ie, the workaround is still worth having, but I'd avoid leaning on a comment that we might later go "oh, that's outdated, let's remove this".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah that is better. I like it

JPeer264 and others added 4 commits October 8, 2026 13:38
… RSC responses

Streamed SSR (`text/html`) and RSC (`text/x-component`) responses were
classified as non-streaming, so the root span ended when the handler
returned and spans that ended while the body streamed were lost. Both
content types are now treated as streaming when the response has no
Content-Length, the same rule that `text/plain` already uses.

The SDK now creates the streamed response with the original response as
the init. This keeps `encodeBody: 'manual'`, so workerd no longer
compresses an already compressed body a second time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…TML and RSC responses

Without the `transformstream_enable_standard_constructor` compatibility
flag (compatibility dates before 2022-11-30, which Hydrogen's mini-oxygen
uses), workerd ignores the transformer, so `flush` and `cancel` never ran
and the span was never sent. Such Workers now end the span at handler
return, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JPeer264
JPeer264 force-pushed the jp/cloudflare-streaming-response-detection branch from 4356442 to ee216da Compare October 8, 2026 11:51
@linear-code

linear-code Bot commented Oct 8, 2026

Copy link
Copy Markdown

JS-3236

@JPeer264
JPeer264 requested a review from isaacs October 9, 2026 07:10

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a mechanism to override classifyResponseStreaming behaviour

2 participants