Skip to content

Commit

Permalink
ref(nextjs): Don't initialize Server SDK during build (#9503)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Nov 9, 2023
1 parent b531549 commit 061fe5a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
4 changes: 1 addition & 3 deletions packages/nextjs/src/common/wrapRouteHandlerWithSentry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addTracingExtensions, captureException, flush, getCurrentHub, runWithAsyncContext, trace } from '@sentry/core';
import { captureException, flush, getCurrentHub, runWithAsyncContext, trace } from '@sentry/core';
import { addExceptionMechanism, tracingContextFromHeaders } from '@sentry/utils';

import { isRedirectNavigationError } from './nextNavigationErrorUtils';
Expand All @@ -13,8 +13,6 @@ export function wrapRouteHandlerWithSentry<F extends (...args: any[]) => any>(
routeHandler: F,
context: RouteHandlerContext,
): (...args: Parameters<F>) => ReturnType<F> extends Promise<unknown> ? ReturnType<F> : Promise<ReturnType<F>> {
addTracingExtensions();

const { method, parameterizedRoute, baggageHeader, sentryTraceHeader } = context;

return new Proxy(routeHandler, {
Expand Down
11 changes: 1 addition & 10 deletions packages/nextjs/src/common/wrapServerComponentWithSentry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
addTracingExtensions,
captureException,
flush,
getCurrentHub,
runWithAsyncContext,
startTransaction,
} from '@sentry/core';
import { captureException, flush, getCurrentHub, runWithAsyncContext, startTransaction } from '@sentry/core';
import { addExceptionMechanism, tracingContextFromHeaders } from '@sentry/utils';

import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
Expand All @@ -19,8 +12,6 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
appDirComponent: F,
context: ServerComponentContext,
): F {
addTracingExtensions();

const { componentRoute, componentType } = context;

// Even though users may define server components as async functions, for the client bundles
Expand Down
10 changes: 9 additions & 1 deletion packages/nextjs/src/edge/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { SDK_VERSION } from '@sentry/core';
import { addTracingExtensions, SDK_VERSION } from '@sentry/core';
import { RewriteFrames } from '@sentry/integrations';
import type { SdkMetadata } from '@sentry/types';
import { addOrUpdateIntegration, escapeStringForRegex, GLOBAL_OBJ } from '@sentry/utils';
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
import { init as vercelEdgeInit } from '@sentry/vercel-edge';

import { isBuild } from '../common/utils/isBuild';

export type EdgeOptions = VercelEdgeOptions;

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

/** Inits the Sentry NextJS SDK on the Edge Runtime. */
export function init(options: VercelEdgeOptions = {}): void {
addTracingExtensions();

if (isBuild()) {
return;
}

const opts = {
_metadata: {} as SdkMetadata,
...options,
Expand Down
7 changes: 7 additions & 0 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addTracingExtensions } from '@sentry/core';
import { RewriteFrames } from '@sentry/integrations';
import type { NodeOptions } from '@sentry/node';
import { configureScope, getCurrentHub, init as nodeInit, Integrations } from '@sentry/node';
Expand Down Expand Up @@ -63,6 +64,12 @@ const IS_VERCEL = !!process.env.VERCEL;

/** Inits the Sentry NextJS SDK on node. */
export function init(options: NodeOptions): void {
addTracingExtensions();

if (isBuild()) {
return;
}

const opts = {
environment: process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV,
...options,
Expand Down

0 comments on commit 061fe5a

Please sign in to comment.