Skip to content

Commit 061fe5a

Browse files
authored
ref(nextjs): Don't initialize Server SDK during build (#9503)
1 parent b531549 commit 061fe5a

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addTracingExtensions, captureException, flush, getCurrentHub, runWithAsyncContext, trace } from '@sentry/core';
1+
import { captureException, flush, getCurrentHub, runWithAsyncContext, trace } from '@sentry/core';
22
import { addExceptionMechanism, tracingContextFromHeaders } from '@sentry/utils';
33

44
import { isRedirectNavigationError } from './nextNavigationErrorUtils';
@@ -13,8 +13,6 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
1313
routeHandler: F,
1414
context: RouteHandlerContext,
1515
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>> {
16-
addTracingExtensions();
17-
1816
const { method, parameterizedRoute, baggageHeader, sentryTraceHeader } = context;
1917

2018
return new Proxy(routeHandler, {

packages/nextjs/src/common/wrapServerComponentWithSentry.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
addTracingExtensions,
3-
captureException,
4-
flush,
5-
getCurrentHub,
6-
runWithAsyncContext,
7-
startTransaction,
8-
} from '@sentry/core';
1+
import { captureException, flush, getCurrentHub, runWithAsyncContext, startTransaction } from '@sentry/core';
92
import { addExceptionMechanism, tracingContextFromHeaders } from '@sentry/utils';
103

114
import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
@@ -19,8 +12,6 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
1912
appDirComponent: F,
2013
context: ServerComponentContext,
2114
): F {
22-
addTracingExtensions();
23-
2415
const { componentRoute, componentType } = context;
2516

2617
// Even though users may define server components as async functions, for the client bundles

packages/nextjs/src/edge/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { SDK_VERSION } from '@sentry/core';
1+
import { addTracingExtensions, SDK_VERSION } from '@sentry/core';
22
import { RewriteFrames } from '@sentry/integrations';
33
import type { SdkMetadata } from '@sentry/types';
44
import { addOrUpdateIntegration, escapeStringForRegex, GLOBAL_OBJ } from '@sentry/utils';
55
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
66
import { init as vercelEdgeInit } from '@sentry/vercel-edge';
77

8+
import { isBuild } from '../common/utils/isBuild';
9+
810
export type EdgeOptions = VercelEdgeOptions;
911

1012
const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
@@ -13,6 +15,12 @@ const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
1315

1416
/** Inits the Sentry NextJS SDK on the Edge Runtime. */
1517
export function init(options: VercelEdgeOptions = {}): void {
18+
addTracingExtensions();
19+
20+
if (isBuild()) {
21+
return;
22+
}
23+
1624
const opts = {
1725
_metadata: {} as SdkMetadata,
1826
...options,

packages/nextjs/src/server/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { addTracingExtensions } from '@sentry/core';
12
import { RewriteFrames } from '@sentry/integrations';
23
import type { NodeOptions } from '@sentry/node';
34
import { configureScope, getCurrentHub, init as nodeInit, Integrations } from '@sentry/node';
@@ -63,6 +64,12 @@ const IS_VERCEL = !!process.env.VERCEL;
6364

6465
/** Inits the Sentry NextJS SDK on node. */
6566
export function init(options: NodeOptions): void {
67+
addTracingExtensions();
68+
69+
if (isBuild()) {
70+
return;
71+
}
72+
6673
const opts = {
6774
environment: process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV,
6875
...options,

0 commit comments

Comments
 (0)