Skip to content

Commit 6af264a

Browse files
committed
early return in tunnel requests
1 parent a75f456 commit 6af264a

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

packages/nextjs/rollup.npm.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default [
1717
// prevent this internal nextjs code from ending up in our built package (this doesn't happen automatically because
1818
// the name doesn't match an SDK dependency)
1919
packageSpecificConfig: {
20-
external: ['next/router', 'next/constants', 'next/headers', 'stacktrace-parser'],
20+
external: ['next/router', 'next/constants', 'next/headers', 'next/server', 'stacktrace-parser'],
2121

2222
// Next.js and our users are more happy when our client code has the "use client" directive
2323
plugins: [

packages/nextjs/src/common/wrapMiddlewareWithSentry.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
winterCGRequestToRequestData,
1414
withIsolationScope,
1515
} from '@sentry/core';
16+
import { NextResponse } from 'next/server';
1617
import type { EdgeRouteHandler } from '../edge/types';
1718
import { flushSafelyWithTimeout } from './utils/responseEnd';
1819

@@ -27,6 +28,23 @@ export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
2728
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
2829
return new Proxy(middleware, {
2930
apply: async (wrappingTarget, thisArg, args: Parameters<H>) => {
31+
const tunnelRoute =
32+
'_sentryRewritesTunnelPath' in globalThis
33+
? (globalThis as Record<string, unknown>)._sentryRewritesTunnelPath
34+
: undefined;
35+
36+
if (tunnelRoute && typeof tunnelRoute === 'string') {
37+
const req: unknown = args[0];
38+
// Check if the current request matches the tunnel route
39+
if (req instanceof Request) {
40+
const url = new URL(req.url);
41+
const isTunnelRequest = url.pathname.startsWith(tunnelRoute);
42+
43+
if (isTunnelRequest) {
44+
return NextResponse.next() as ReturnType<H>;
45+
}
46+
}
47+
}
3048
// TODO: We still should add central isolation scope creation for when our build-time instrumentation does not work anymore with turbopack.
3149
return withIsolationScope(isolationScope => {
3250
const req: unknown = args[0];

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ function getFinalConfigObject(
107107
showedExportModeTunnelWarning = true;
108108
// eslint-disable-next-line no-console
109109
console.warn(
110-
'[@sentry/nextjs] The Sentry Next.js SDK `tunnelRoute` option will not work in combination with Next.js static exports. The `tunnelRoute` option uses serverside features that cannot be accessed in export mode. If you still want to tunnel Sentry events, set up your own tunnel: https://docs.sentry.io/platforms/javascript/troubleshooting/#using-the-tunnel-option',
110+
'[@sentry/nextjs] The Sentry Next.js SDK `tunnelRoute` option will not work in combination with Next.js static exports. The `tunnelRoute` option uses server-side features that cannot be accessed in export mode. If you still want to tunnel Sentry events, set up your own tunnel: https://docs.sentry.io/platforms/javascript/troubleshooting/#using-the-tunnel-option',
111111
);
112112
}
113113
} else {
114114
const resolvedTunnelRoute =
115-
typeof userSentryOptions.tunnelRoute === 'boolean'
115+
typeof userSentryOptions.tunnelRoute === 'boolean' && userSentryOptions.tunnelRoute === true
116116
? generateRandomTunnelRoute()
117117
: userSentryOptions.tunnelRoute;
118118

119119
// Update the global options object to use the resolved value everywhere
120-
userSentryOptions.tunnelRoute = resolvedTunnelRoute;
120+
userSentryOptions.tunnelRoute = resolvedTunnelRoute || undefined;
121121
setUpTunnelRewriteRules(incomingUserNextConfigObject, resolvedTunnelRoute);
122122
}
123123
}

0 commit comments

Comments
 (0)