Skip to content

feat(nextjs): Serve the tunnel from the app with a DSN-derived path - #25424

Draft
logaretm wants to merge 19 commits into
awad/runtime-tunnel-interceptionfrom
awad/tunnel-nextjs
Draft

logaretm wants to merge 19 commits into
awad/runtime-tunnel-interceptionfrom
awad/tunnel-nextjs

Conversation

@logaretm

@logaretm logaretm commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

The managed tunnel from 11.10 ignored the Next.js basePath, fixed by serving tunnel: true under it in the browser and on the server, which #25917 answers before Next sees the request, and by moving the tunnel route onto core's handleTunnelRequest.

// instrumentation-client.ts
Sentry.init({ dsn, tunnel: true });

// sentry.server.config.ts / sentry.edge.config.ts
Sentry.init({ dsn, integrations: [Sentry.serveTunnelIntegration()] });

// proxy.ts, only where no Node server runs (e.g. edge middleware on Vercel)
const tunnelResponse = Sentry.handleTunnelRequestIfMatched(request);
if (tunnelResponse) {
  return tunnelResponse;
}

The e2e variants serve the tunnel under a base path with and without an edge middleware, and check the forwarded events and that no tunnel spans are sent.

closes #25466

@logaretm
logaretm added this pull request to stack #25201 October 15, 2026 20:01
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: 93d5729 (PR test merge using awad/runtime-tunnel-interception at 5845bd4)

Against: 53daa00 (awad/runtime-tunnel-interception) · base run #64191

⚠️ Baseline mismatch: 39 awad/runtime-tunnel-interception commit(s) in the PR build are missing from the baseline. Their size changes also affect this comparison. Compare commits. Re-run once awad/runtime-tunnel-interception's baseline build for 5845bd4 finishes.

Path Size % Change Change
@sentry/browser 30.39 kB -0.21% -62 B 🔽
@sentry/browser - with treeshaking flags 28.53 kB -0.11% -31 B 🔽
@sentry/browser - with treeshaking flags tracing without tracing 28.42 kB -0.14% -38 B 🔽
@sentry/browser (incl. Tracing) 52.7 kB -0.18% -94 B 🔽
@sentry/browser (incl. Tracing + Span Streaming) 52.71 kB -0.17% -87 B 🔽
@sentry/browser (incl. Tracing, Profiling) 55.68 kB -0.13% -70 B 🔽
@sentry/browser (incl. Tracing, Replay) 93.01 kB -0.08% -71 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 81.6 kB -0.07% -49 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas) 97.75 kB -0.08% -73 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback) 110.74 kB -0.08% -83 B 🔽
@sentry/browser (incl. Feedback) 47.97 kB -0.11% -51 B 🔽
@sentry/browser (incl. sendFeedback) 35.49 kB -0.12% -41 B 🔽
@sentry/browser (incl. FeedbackAsync) 40.6 kB -0.09% -34 B 🔽
@sentry/browser (incl. Metrics) 31.42 kB -0.14% -41 B 🔽
@sentry/browser (incl. Logs) 31.72 kB -0.02% -6 B 🔽
@sentry/browser (incl. Metrics & Logs) 32.44 kB -0.01% -1 B 🔽
@sentry/react 32.27 kB +0.08% +24 B 🔺
@sentry/react (incl. Tracing) 55.07 kB -0.06% -28 B 🔽
@sentry/vue 38.66 kB -0.21% -80 B 🔽
@sentry/vue (incl. Tracing) 55.72 kB +0.09% +48 B 🔺
@sentry/svelte 30.42 kB -0.2% -58 B 🔽
@sentry/remix (Remix 3 client bundle) 57.79 kB -0.08% -43 B 🔽
CDN Bundle 32.18 kB -0.02% -4 B 🔽
CDN Bundle (incl. Tracing) 53.23 kB -0.03% -14 B 🔽
CDN Bundle (incl. Logs, Metrics) 34.45 kB +0.08% +25 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 55.17 kB -0.02% -7 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) 75.86 kB -0.02% -9 B 🔽
CDN Bundle (incl. Tracing, Replay) 91.44 kB -0.01% -6 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 93.38 kB -0.03% -24 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) 97.56 kB -0.02% -11 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.55 kB -0.04% -33 B 🔽
CDN Bundle - uncompressed 94.62 kB +0.01% +2 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 157.58 kB -0.09% -129 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed 101.09 kB -0.08% -74 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 163.31 kB -0.22% -360 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 232.65 kB -0.04% -74 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed 279.27 kB -0.05% -129 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 284.99 kB -0.13% -360 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 292.97 kB -0.05% -129 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 298.69 kB -0.13% -360 B 🔽
@sentry/nextjs (client) 57.97 kB +0.06% +30 B 🔺
@sentry/sveltekit (client) 53.22 kB -0.11% -57 B 🔽
@sentry/core/server 44.58 kB +0.55% +242 B 🔺
@sentry/core/browser 13.93 kB -0.16% -22 B 🔽
@sentry/node 156.77 kB +1.08% +1.67 kB 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 82.08 kB +0.51% +412 B 🔺
@sentry/node - without tracing 98.11 kB +2.31% +2.21 kB 🔺
@sentry/node - without channel injection 137.21 kB +1.06% +1.44 kB 🔺
@sentry/aws-serverless 105.78 kB +1.63% +1.69 kB 🔺
@sentry/cloudflare (withSentry) - minified 201.91 kB +2.01% +3.97 kB 🔺
@sentry/cloudflare (withSentry) 501.89 kB +2.03% +9.97 kB 🔺
@sentry/nextjs/cloudflare (withSentry) - minified 222.65 kB +1.95% +4.24 kB 🔺

View base workflow run

Comment thread packages/nextjs/src/config/types.ts Outdated
@logaretm

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.

@logaretm

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/config/withSentryConfig/tunnel.ts Outdated
@logaretm

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/config/withSentryConfig/tunnel.ts Outdated
Comment thread packages/nextjs/src/common/sentryTunnelRouteHandler.ts Outdated
Comment thread packages/nextjs/src/common/utils/dropMiddlewareTunnelRequests.ts
@logaretm

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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5472088. Configure here.

basePath: userNextConfig.basePath ?? '',
...(path && { path }),
...(allowedDsns && { allowedDsns }),
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tunnel ignores Next.js trailingSlash

Medium Severity

getManagedTunnelOptions copies basePath from the Next.js config but not trailingSlash, so the browser posts to the slashless DSN path. With trailingSlash: true, Next.js 308s that POST before beforeFiles rewrites, and the DSN rewrite source also omits the optional trailing slash that the fixed-path rewrite includes. Unload-time keepalive fetches often will not follow that redirect, so events never reach the tunnel.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 5472088. Configure here.

@logaretm
logaretm removed this pull request from stack #25201 October 27, 2026 19:24
Base automatically changed from awad/tunnel-solidstart to develop October 27, 2026 19:48
Comment thread packages/nuxt/src/runtime/plugins/tunnel-legacy.server.ts Outdated
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 4 not successful, 50 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E node-openai-decisions Test (optional) Failing after 19s Details
❌ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E gen-ai-libraries (latest) Test (optional) Failing after 37s Details
❌ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Failing after 12s Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (latest) Test (optional) Successful in 40s Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 14s Details
✅ CI: Optional E2E Tests / E2E cloudflare-think Test (optional) Successful in 46s Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (latest) Test (optional) Successful in 31s Details
✅ CI: Optional E2E Tests / E2E nextjs-orpc Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (cloudflare) Test (optional) Successful in 36s Details
✅ CI: Optional E2E Tests / E2E node-eve Test (optional) Successful in 40s Details
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable Test (optional) Successful in 24s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) Test (optional) Successful in 46s Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry-static Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nextjs-14 (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (deno) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (cloudflare, latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 (tracer provider) Test (optional) Successful in 23s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 22s Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 56s Details
✅ CI: Optional E2E Tests / E2E node-flue Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable Test (optional) Successful in 41s Details
✅ CI: Optional E2E Tests / E2E cloudflare-mastra Test (optional) Successful in 29s Details
✅ CI: Optional E2E Tests / E2E astro-7-static Test (optional) Successful in 52s Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 22s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 20s Details
✅ CI: Optional E2E Tests / E2E react-send-to-sentry Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 23s Details
✅ CI: Optional E2E Tests / E2E node-mastra Test (optional) Successful in 53s Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 17s Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E vue-3 (no Options API) Test (optional) Successful in 54s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 19s Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 43s Details
✅ CI: Optional E2E Tests / E2E supabase-nextjs Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 26s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 31s Details

isaacs and others added 19 commits October 29, 2026 12:22
Moves Cloudflare's `wrapRequestHandler` onto `wrapServerRequestHandler`,
which `Deno.serve` and `Bun.serve` use since the previous PRs. Cloudflare
keeps only what is unique to it: client init, the `cf` and cloud
resource contexts, the protocol from `cf.httpProtocol`, the
`isStreamingResponse` option, `captureErrors`, and the flush through
`waitUntil`, which skips the dispose for a 101 response. The helpers
that only the old code used (`captureIncomingRequestBody`,
`shouldCreateIncomingRequestSpan`, `getIncomingRequestSpanHooks` and
`addRequest`) are gone.

`wrapRequestHandler` must run on workers without the `nodejs_compat`
flag, such as Shopify Oxygen, and every `@sentry/server-utils` entry
loads `node:async_hooks`. So the wrapper moves to a new entry,
`@sentry/server-utils/request-handler`, with no Node.js imports. Deno
and Bun import it from there too. Cloudflare's module graph test allows
this one entry, and a new test in server-utils walks the entry's module
graph and fails on any Node.js import.

The wrapper gets three optional fields: `isStreamingResponse`, `onDone`
(called once when the request is done, with the response if there is
one), and a `mechanism` that can be left out to not capture errors.

For streamed responses, the shared stream monitor replaces Cloudflare's
`TransformStream`. Workers with a compatibility date before 2022-11-30
cannot construct a `ReadableStream` without the
`streams_enable_constructors` flag, so the wrapper then ends at handler
return, as the `TransformStream` fallback did. The monitor now locks the
source body only in `start()`, so the original response stays usable in
that case. The wrapper builds the new response from the original one,
which keeps `encodeBody: 'manual'`.

Visible changes for Cloudflare:

- Request spans set `client.address` from `CF-Connecting-IP` (or
  another forwarding header), as on the other runtimes. This follows
  `dataCollection.userInfo`, which is `true` by default.
- Request spans get the response header attributes, and events get the
  `response` context.
- An error from the source of a streamed response is captured (unless
  `captureErrors` is `false`), and the span gets an error status.
- If the user removed `httpServerIntegration`, request bodies follow
  `dataCollection.httpBodies`. Before, the SDK attached no body.

The minified `withSentry` bundle grows by about 2.4 KB (212,385 to
214,853 bytes), under the 211 KiB limit. Most of it is the request
session and client address code, which Cloudflare now shares.

Fixes #25577
Fixes JS-3975

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…ncLocalStorage

Without `AsyncLocalStorage` (for example on Shopify Oxygen, or on
Workers without `nodejs_compat`), the SDK uses the stack fallback, and
`withInvocationIsolationScope` does not fork. The scope it passes to its
callback is a clone that is not the current isolation scope. The shared
request handler wrote the request data and body to that clone, so an
event captured in the handler, which reads the current scope, had no
request body. The `hydrogen-react-router-7` E2E test caught this.

The handler now gets the current isolation scope. With
`AsyncLocalStorage`, that is the same scope as before. Without it, the
request data goes where the body went before the move to the shared
handler, and events now also get the request URL and method.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
With `serveTunnelIntegration`, Node, Bun and Deno servers and Cloudflare Workers now answer tunnel requests before
the app sees them, whatever framework runs on top. `intercept: false` opts out, for apps that serve the tunnel from
their own code.

The tunnel never throws into the runtime: before a request is claimed any failure hands it to the app, after that it
ends in a 502. The forward to Sentry is aborted when the browser goes away, and the http server instrumentation skips
tunnel requests.
The runtimes answer managed tunnel requests now, so the framework SDKs stop answering them in their own request
hooks. They keep passing their base path, and TanStack Start's `tunnelRoute` still enables the tunnel.
The Node, Bun and Deno SDKs install their tunnel hooks in `init` rather than from `serveTunnelIntegration`, so a
tunnel a framework SDK enables, like TanStack Start's `tunnelRoute`, is answered too. The hooks answer nothing
unless the tunnel is on, so `serveTunnelIntegration` is core's again on every runtime.
Lambda runs no HTTP server, so `wrapHandler` answers tunnel requests from API Gateway, function URL and ALB events
before the handler runs.
`sentryCloudflareNitroPlugin` passes the body Nitro already read, so the runtime can answer tunnel requests. On
Nitro v3, which has no `localFetch`, it wraps `fetch` instead of throwing.
Pages builds get the same Worker entry wrap as Workers again, which answers managed tunnel requests. It was dropped
in #19558 only to keep it out of the bundle.
Lambda runs no HTTP server, so when `init` runs from a preload with the tunnel on, it redirects `_HANDLER` to a
shim that answers tunnel requests from HTTP events before the handler. `@sentry/aws-serverless` keeps its own
shim, and the handler resolution and tunnel helpers move to `@sentry/node` so both share them.
A framework's server bundle can carry its own copy of `@sentry/core`, so the base path and `tunnelRoute` defaults it set never reached the runtime's copy that answers the tunnel.
Its dynamic `require` made webpack warn in Next.js builds. Only the handler shims need it.
Non-POST requests to the tunnel path now go through the app.
A tunnel request from a prerendered page on a cold server reached the app, because the server config only ran from SSR pages and the middleware. Builds now also import it from the server entry, and the server `init` keeps the first client, so a `--import` preload doesn't initialize twice.
Runs the same cases against Node, Cloudflare, Bun and Deno servers, checking the app keeps serving after each, the tunnel answers what it owns, and nothing is reported to the app's project.
`@sentry/node` doesn't answer the tunnel on Bun, and Bun drops pipelined requests behind an async one. `packages/bun` runs the same cases against Bun's servers.
…equest`

The tunnel route handler now leaves DSN normalization, the allowlist and
tracing suppression to core. As in every other SDK, `allowedDsns` adds to
the server's own DSN, and the default `POST` accepts the DSNs
`serveTunnelIntegration` allows.
…ePath`

`tunnel: true` in the browser now sends to `<basePath>/<hash>`, like the
SvelteKit and React Router SDKs do with their base paths. A `basePath` the
user set and a `tunnel` URL stay as written.
…the server

`withSentryConfig` now injects `basePath` into production server and edge
builds, not only in dev, and the server, edge and Cloudflare inits pass it
to `setManagedTunnelDefaults`. `serveTunnelIntegration` then matches
`/<basePath>/<hash>`, like the browser sends.
@github-actions

Copy link
Copy Markdown
Contributor

One or more gzipped bundles increased by more than 500 bytes. If this increase is intentional, add the Accept Bundlesize Increase label to this PR to rerun and accept the check.

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.

Next.js managed tunnel on the tunnel route handler

2 participants