Skip to content

feat(nextjs): Add a tunnel route handler that sends only the event payload to Sentry - #25456

Merged
s1gr1d merged 5 commits into
developfrom
sig/nextjs-tunnel-route-handler
Oct 19, 2026
Merged

s1gr1d merged 5 commits into
developfrom
sig/nextjs-tunnel-route-handler

Conversation

@s1gr1d

@s1gr1d s1gr1d commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Today, tunnelRoute rewrites tunnel requests to Sentry as a proxy. This PR adds an opt-in handler mode: if app/sentry-tunnel/route.ts (or src/app/sentry-tunnel/route.ts) exists, the SDK rewrites the tunnel route internally to it, and the handler sends only the event payload to Sentry. Without the file, nothing changes.

// app/sentry-tunnel/route.ts
export { POST } from '@sentry/nextjs/tunnel';
  • When the file exists, but there's no tunnelRoute option, a warning is shown.
  • The rewrite is in beforeFiles, so a public file or page at the tunnel path cannot shadow it.
  • Like TanStack Start, the handler accepts only the server SDK's DSN. We also export createSentryTunnelRoute({ allowedDsns }) which covers other DSNs.
  • Fixed: Tunnel request spans were sent with span streaming, in rewrite mode too. They are now dropped.

Tested with unit tests, two new nextjs-16-tunnel variants (Turbopack/Node.js, webpack/edge) that also check the forwarded headers.

Also tested with a manual Vercel deploy on Next.js 14-16.

Fixes JS-3927

@s1gr1d
s1gr1d force-pushed the sig/nextjs-tunnel-route-handler branch from fb12663 to de8fd3d Compare October 16, 2026 13:21
@s1gr1d s1gr1d changed the title feat(nextjs): Add tunnel route handler that forwards only the envelope feat(nextjs): Add a tunnel route handler that sends only the event payload to Sentry Oct 16, 2026
@linear-code

linear-code Bot commented Oct 16, 2026

Copy link
Copy Markdown

JS-3927

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.88 kB - -
@sentry/browser - with treeshaking flags 28.02 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.92 kB - -
@sentry/browser (incl. Tracing) 52.07 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 52.07 kB - -
@sentry/browser (incl. Tracing, Profiling) 55.02 kB - -
@sentry/browser (incl. Tracing, Replay) 91.82 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.68 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 96.55 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 109.49 kB - -
@sentry/browser (incl. Feedback) 47.4 kB - -
@sentry/browser (incl. sendFeedback) 34.94 kB - -
@sentry/browser (incl. FeedbackAsync) 40.05 kB - -
@sentry/browser (incl. Metrics) 30.9 kB - -
@sentry/browser (incl. Logs) 31.18 kB - -
@sentry/browser (incl. Metrics & Logs) 31.81 kB - -
@sentry/react 31.7 kB - -
@sentry/react (incl. Tracing) 54.39 kB - -
@sentry/vue 38.04 kB - -
@sentry/vue (incl. Tracing) 55.01 kB - -
@sentry/svelte 29.9 kB - -
@sentry/remix (Remix 3 client bundle) 57.08 kB - -
CDN Bundle 31.64 kB - -
CDN Bundle (incl. Tracing) 52.58 kB - -
CDN Bundle (incl. Logs, Metrics) 33.84 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 54.52 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 74.77 kB - -
CDN Bundle (incl. Tracing, Replay) 90.25 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 92.18 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 96.4 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 98.34 kB - -
CDN Bundle - uncompressed 93.18 kB - -
CDN Bundle (incl. Tracing) - uncompressed 156.03 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 99.72 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 161.99 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 229.76 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 276.2 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 282.14 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 289.9 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 295.84 kB - -
@sentry/nextjs (client) 56.84 kB +0.07% +39 B 🔺
@sentry/sveltekit (client) 52.45 kB - -
@sentry/core/server 41.12 kB - -
@sentry/core/browser 13.81 kB - -
@sentry/node 155.04 kB +0.01% +11 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 84.06 kB -0.01% -2 B 🔽
@sentry/node - without tracing 96.7 kB +0.01% +3 B 🔺
@sentry/node - without channel injection 133.07 kB +0.01% +6 B 🔺
@sentry/aws-serverless 104.89 kB +0.01% +5 B 🔺
@sentry/cloudflare (withSentry) - minified 210.78 kB - -
@sentry/cloudflare (withSentry) 522.89 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 228.46 kB - -

View base workflow run

@s1gr1d

s1gr1d commented Oct 16, 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.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

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 de8fd3d. Configure here.

@logaretm logaretm 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.

Tiny issue and a small FYI, LGTM and looks nice!

}

// Like the SDK transports, so that the forward creates no spans, breadcrumbs or trace headers.
return suppressTracing(() => handleTunnelRequest({ request, allowedDsns }));

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.

FYI: This will lose the actual client IP, so relay will think the server is the user. Another issue is it parses the whole payload only to re-serialize it again.

This is pr-existing in the current tunnel utils, I'm handling both concern in my project stack atm so let's keep it. Just an FYI.

Check #25199 for reference.

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.

Good point, thanks! I'll leave this as-is here so you can handle this in your stack.

projectDir: string = process.cwd(),
): string | undefined {
// Like Next.js, ignore `src/app` if there is a root `app` directory.
const appDir = ['app', 'src/app'].find(dir => getStats(path.join(projectDir, dir))?.isDirectory());

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.

If the root has pages/ and the handler is in src/app/sentry-tunnel/route.ts, this picks src/app and enables handler mode.

Next.js ignores src/ when app or pages exists in the root, so /sentry-tunnel 404s and all events are lost. Checking for a root pages too fixes it:

Suggested change
const appDir = ['app', 'src/app'].find(dir => getStats(path.join(projectDir, dir))?.isDirectory());
const hasRootDir = ['app', 'pages'].some(dir => getStats(path.join(projectDir, dir))?.isDirectory());
const appDir = hasRootDir ? 'app' : 'src/app';

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.

I tested this with a root pages/ and src/app/sentry-tunnel/route.ts on Next 14.2 and 15.5 (webpack/Turbopack, build/dev). /sentry-tunnel is served from src/app, and the root pages still work.

The docs say:

src/app or src/pages will be ignored if app or pages are present in the root directory.

But the code resolves pages and app separately (find-pages-dir): a root pages/ hides only src/pages/, and a root app/ hides only src/app/. Since Next 16, this mixed layout fails the build anyway (vercel/next.js#83416).

With the suggestion, the SDK wouldn't detect the file in those 14/15 apps, so handler mode would quietly stay off and the tunnel would keep forwarding all headers. So I kept the detection and added a test for this case.

s1gr1d and others added 3 commits October 19, 2026 10:18
@s1gr1d
s1gr1d marked this pull request as ready for review October 19, 2026 08:39
@s1gr1d
s1gr1d requested a review from a team as a code owner October 19, 2026 08:39
@s1gr1d
s1gr1d requested review from chargome and mydea and removed request for a team October 19, 2026 08:39
@s1gr1d
s1gr1d added this pull request to stack #25516 October 19, 2026 11:03
…route-handler

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@s1gr1d
s1gr1d merged commit d885ece into develop Oct 19, 2026
92 checks passed
@s1gr1d
s1gr1d deleted the sig/nextjs-tunnel-route-handler branch October 19, 2026 13:24
logaretm pushed a commit that referenced this pull request Oct 19, 2026
The tunnel route handlers (`@sentry/nextjs/tunnel` and TanStack Start's
`createSentryTunnelRoute`) now check the request size before they read
the body.

A request larger than 20 MiB gets a 413, and a request without
`Content-Length` gets a 411. So one request can no longer make the
server hold more than 20 MiB in memory. SDK envelopes are much smaller
than this limit, so normal traffic does not change.

Checking the header is enough, because the HTTP server stops reading the
body at `Content-Length`.

Stacked on #25456.
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