Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/nextjs/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getDefaultIntegrations as getReactDefaultIntegrations, init as reactIni
import { devErrorSymbolicationEventProcessor } from '../common/devErrorSymbolicationEventProcessor';
import { getVercelEnv } from '../common/getVercelEnv';
import { isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
import { isTurbopack } from '../common/utils/isTurbopack';
import { browserTracingIntegration } from './browserTracingIntegration';
import { nextjsClientStackFrameNormalizationIntegration } from './clientNormalizationIntegration';
import { INCOMPLETE_APP_ROUTER_INSTRUMENTATION_TRANSACTION_NAME } from './routing/appRouterRoutingInstrumentation';
Expand Down Expand Up @@ -77,8 +78,7 @@ export function init(options: BrowserOptions): Client | undefined {
}

try {
// @ts-expect-error `process.turbopack` is a magic string that will be replaced by Next.js
if (process.turbopack) {
if (isTurbopack()) {
getGlobalScope().setTag('turbopack', true);
}
} catch {
Expand Down
8 changes: 8 additions & 0 deletions packages/nextjs/src/common/utils/isTurbopack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Detect whether running under Turbopack
*/
export function isTurbopack(): boolean {
if (typeof process === 'undefined') return false;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return process.env?.TURBOPACK === '1' || !!(process as any)?.turbopack;
}
Copy link
Member

Choose a reason for hiding this comment

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

According to the comment in src/client/index.ts:

process.turbopack is a magic string that will be replaced by Next.js

which makes me suspect, this function needs to literally contain process.turbopack without any type casts or optional chain operators. If the env variable check solves something in addition, it's fine to keep. But let's maybe just try/catch process.turbopack access instead of checking for definedness?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review @Lms24!

You’re absolutely right about the magic string concern.
After checking the Next.js source, I found process.turbopack and process.env.TURBOPACK are always set together. Since process.env.TURBOPACK is safer in Edge Runtime and avoids the magic string issue, I’ve simplified the code to only rely on it.

You can saw the code context diff, process.turbopack and process.env.TURBOPACK

Really appreciate your feedback—please feel free to point out anything else.

4 changes: 2 additions & 2 deletions packages/nextjs/src/edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';
import { isBuild } from '../common/utils/isBuild';
import { isTurbopack } from '../common/utils/isTurbopack';
import { flushSafelyWithTimeout } from '../common/utils/responseEnd';
import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration';

Expand Down Expand Up @@ -95,8 +96,7 @@ export function init(options: VercelEdgeOptions = {}): void {
});

try {
// @ts-expect-error `process.turbopack` is a magic string that will be replaced by Next.js
if (process.turbopack) {
if (isTurbopack()) {
getGlobalScope().setTag('turbopack', true);
}
} catch {
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION,
} from '../common/span-attributes-with-logic-attached';
import { isBuild } from '../common/utils/isBuild';
import { isTurbopack } from '../common/utils/isTurbopack';
import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration';

export * from '@sentry/node';
Expand Down Expand Up @@ -368,8 +369,7 @@ export function init(options: NodeOptions): NodeClient | undefined {
}

try {
// @ts-expect-error `process.turbopack` is a magic string that will be replaced by Next.js
if (process.turbopack) {
if (isTurbopack()) {
getGlobalScope().setTag('turbopack', true);
}
} catch {
Expand Down
Loading