Skip to content

fix(nextjs): Skip init inside a request of @sentry/cloudflare - #24996

Merged
JPeer264 merged 2 commits into
developfrom
jp/nextjs-skip-init-in-cloudflare-request
Oct 6, 2026
Merged

JPeer264 merged 2 commits into
developfrom
jp/nextjs-skip-init-in-cloudflare-request

Conversation

@JPeer264

@JPeer264 JPeer264 commented Oct 2, 2026

Copy link
Copy Markdown
Member

Next.js calls register() inside the first request. When the Worker is wrapped with withSentry from @sentry/cloudflare, the server and edge init installed the OpenTelemetry async context strategy on the storage of that running request, and every later request failed with context.getValue is not a function (#24603).

On Workers, init now creates no client while a request of withSentry runs, because that client already handles the request. The SDK detects this from the installed strategy (AsyncLocalStorage, a store is set, Cloudflare-Workers user agent) instead of a new option, so existing setups need no change. The server init still adds its global event processors and the turbopack tag, which also apply to the events of the Cloudflare client.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.6 kB - -
@sentry/browser - with treeshaking flags 27.75 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.65 kB - -
@sentry/browser (incl. Tracing) 51.52 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 51.52 kB - -
@sentry/browser (incl. Tracing, Profiling) 54.5 kB - -
@sentry/browser (incl. Tracing, Replay) 91.23 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.18 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 95.93 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 108.89 kB - -
@sentry/browser (incl. Feedback) 47.12 kB - -
@sentry/browser (incl. sendFeedback) 34.65 kB - -
@sentry/browser (incl. FeedbackAsync) 39.76 kB - -
@sentry/browser (incl. Metrics) 30.61 kB - -
@sentry/browser (incl. Logs) 30.89 kB - -
@sentry/browser (incl. Metrics & Logs) 31.55 kB - -
@sentry/react 31.43 kB - -
@sentry/react (incl. Tracing) 53.84 kB - -
@sentry/vue 37.55 kB - -
@sentry/vue (incl. Tracing) 54.4 kB - -
@sentry/svelte 29.63 kB - -
@sentry/remix (Remix 3 client bundle) 56.54 kB - -
CDN Bundle 31.33 kB - -
CDN Bundle (incl. Tracing) 52.07 kB - -
CDN Bundle (incl. Logs, Metrics) 33.56 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 54.03 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 74.38 kB - -
CDN Bundle (incl. Tracing, Replay) 89.74 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 91.69 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 95.9 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 97.87 kB - -
CDN Bundle - uncompressed 92.46 kB - -
CDN Bundle (incl. Tracing) - uncompressed 154.77 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.04 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 160.72 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 228.98 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 274.89 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 280.83 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 288.59 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 294.52 kB - -
@sentry/nextjs (client) 56.19 kB - -
@sentry/sveltekit (client) 51.9 kB - -
@sentry/core/server 40.52 kB - -
@sentry/core/browser 13.51 kB - -
@sentry/node 145.01 kB +0.01% +8 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.22 kB - -
@sentry/node - without tracing 93.46 kB +0.01% +3 B 🔺
@sentry/node - without channel injection 123.18 kB +0.01% +10 B 🔺
@sentry/aws-serverless 101.69 kB +0.01% +5 B 🔺
@sentry/cloudflare (withSentry) - minified 208.95 kB - -
@sentry/cloudflare (withSentry) 517.52 kB - -

View base workflow run

@JPeer264
JPeer264 force-pushed the jp/nextjs-skip-init-in-cloudflare-request branch from 20fa56b to 97c3369 Compare October 3, 2026 16:14
@JPeer264
JPeer264 added this pull request to stack #25035 October 5, 2026 08:43
@JPeer264
JPeer264 force-pushed the jp/nextjs-skip-init-in-cloudflare-request branch from 97c3369 to 5fe8fa2 Compare October 5, 2026 09:13
@JPeer264

JPeer264 commented Oct 5, 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.

Stale Bugbot comment from a previous run.

Comment thread packages/nextjs/src/edge/index.ts
@JPeer264
JPeer264 force-pushed the jp/nextjs-skip-init-in-cloudflare-request branch from 5fe8fa2 to aab82c2 Compare October 5, 2026 11:15
Base automatically changed from jp/cloudflare-keep-otel-async-context-strategy to develop October 5, 2026 14:47
@JPeer264
JPeer264 force-pushed the jp/nextjs-skip-init-in-cloudflare-request branch from aab82c2 to 07e8240 Compare October 5, 2026 14:47
@JPeer264

JPeer264 commented Oct 6, 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 6987310. Configure here.

@JPeer264
JPeer264 force-pushed the jp/nextjs-skip-init-in-cloudflare-request branch from 6987310 to 94224ee Compare October 6, 2026 06:42
@JPeer264 JPeer264 self-assigned this Oct 6, 2026
@JPeer264
JPeer264 marked this pull request as ready for review October 6, 2026 07:18
@JPeer264
JPeer264 requested a review from a team as a code owner October 6, 2026 07:18
@JPeer264
JPeer264 requested review from chargome and s1gr1d and removed request for a team October 6, 2026 07:18
Comment thread docs/repeated-init.md
| Nuxt server | Keeps the first client and returns it. Logs that a `--import` preload is no longer needed. |
| Cloudflare (default) | Keeps the first client of the isolate and returns it, unless that client is closed or closing. `cacheClient: false` makes a new client on each call, with no warning. |
| Next.js edge | Warns, then replaces the client. Returns `void`. |
| Next.js server and edge in a request of `withSentry` (Cloudflare) | Creates no client, because the client of `withSentry` handles the request. Returns `undefined`. |

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.

Why do we have this file in here? 😅

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.

This got added via #24962 and acts as a list of defined behaviors for our init() function. This will be then aligned in v12.

* context strategy while the request runs (#24603).
*/
export function isAsyncContextOwnedByCloudflare(): boolean {
const strategy = getAsyncContextStrategy(getMainCarrier());

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.

Just pointing out that this detection might be brittle bc @sentry/nextjs and @sentry/cloudlfare need to be on the exact same version for this to work

@JPeer264 JPeer264 Oct 6, 2026 •

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 it does. But I guess this will be solved when we add this as a optional peer dependency and is documented for the, soon to be added, @sentry/nextjs/cloudflare entrypoint

Edit: Ok maybe a direct dependency might be better. But that is for another PR

JPeer264 and others added 2 commits October 6, 2026 14:41
Next.js calls `register()` inside the first request. When the Worker is
wrapped with `withSentry` from `@sentry/cloudflare`, the server and edge
`init` then installed the OpenTelemetry async context strategy on the storage
of that request, and every later request failed with
`context.getValue is not a function`.

On Cloudflare Workers, `init` now creates no client while a request of
`withSentry` from `@sentry/cloudflare` runs, because that client already
handles the request. Both still set the turbopack tag. The server `init`
adds its event processors to the global scope there, because it has no
client of its own and runs once per isolate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JPeer264
JPeer264 force-pushed the jp/nextjs-skip-init-in-cloudflare-request branch from 2bd0505 to a205928 Compare October 6, 2026 12:57
@JPeer264
JPeer264 merged commit 345064d into develop Oct 6, 2026
86 checks passed
@JPeer264
JPeer264 deleted the jp/nextjs-skip-init-in-cloudflare-request branch October 6, 2026 14:05
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.

2 participants