Skip to content

Commit ff2a4ee

Browse files
committed
extract isBuild into a function
1 parent c7fc025 commit ff2a4ee

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

packages/nextjs/src/index.server.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { escapeStringForRegex, logger } from '@sentry/utils';
77
import * as domainModule from 'domain';
88
import * as path from 'path';
99

10+
import { isBuild } from './utils/isBuild';
1011
import { buildMetadata } from './utils/metadata';
1112
import { NextjsOptions } from './utils/nextjsOptions';
1213
import { addIntegration } from './utils/userIntegrations';
@@ -21,23 +22,6 @@ export { ErrorBoundary, showReportDialog, withErrorBoundary } from '@sentry/reac
2122
type GlobalWithDistDir = typeof global & { __rewriteFramesDistDir__: string };
2223
const domain = domainModule as typeof domainModule & { active: (domainModule.Domain & Carrier) | null };
2324

24-
// During build, the main process is invoked by
25-
// `node next build`
26-
// and child processes are invoked as
27-
// `node <path>/node_modules/.../jest-worker/processChild.js`.
28-
// The former is (obviously) easy to recognize, but the latter could happen at runtime as well. Fortunately, the main
29-
// process hits this file before any of the child processes do, so we're able to set an env variable which the child
30-
// processes can then check. During runtime, the main process is invoked as
31-
// `node next start`
32-
// or
33-
// `node /var/runtime/index.js`,
34-
// so we never drop into the `if` in the first place.
35-
let isBuild = false;
36-
if (process.argv.includes('build') || process.env.SENTRY_BUILD_PHASE) {
37-
process.env.SENTRY_BUILD_PHASE = 'true';
38-
isBuild = true;
39-
}
40-
4125
const isVercel = !!process.env.VERCEL;
4226

4327
/** Inits the Sentry NextJS SDK on node. */
@@ -140,12 +124,13 @@ function addServerIntegrations(options: NextjsOptions): void {
140124
export type { SentryWebpackPluginOptions } from './config/types';
141125
export { withSentryConfig } from './config';
142126
export { withSentry } from './utils/withSentry';
127+
export { isBuild } from './utils/isBuild';
143128

144129
// Wrap various server methods to enable error monitoring and tracing. (Note: This only happens for non-Vercel
145130
// deployments, because the current method of doing the wrapping a) crashes Next 12 apps deployed to Vercel and
146131
// b) doesn't work on those apps anyway. We also don't do it during build, because there's no server running in that
147132
// phase.)
148-
if (!isVercel && !isBuild) {
133+
if (!isVercel && !isBuild()) {
149134
// Dynamically require the file because even importing from it causes Next 12 to crash on Vercel.
150135
// In environments where the JS file doesn't exist, such as testing, import the TS file.
151136
try {

packages/nextjs/src/utils/isBuild.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Decide if the currently running process is part of the build phase or happening at runtime.
3+
*/
4+
export function isBuild(): boolean {
5+
// During build, the main process is invoked by
6+
// `node next build`
7+
// and child processes are invoked as
8+
// `node <path>/node_modules/.../jest-worker/processChild.js`.
9+
// The former is (obviously) easy to recognize, but the latter could happen at runtime as well. Fortunately, the main
10+
// process hits this file before any of the child processes do, so we're able to set an env variable which the child
11+
// processes can then check. During runtime, the main process is invoked as
12+
// `node next start`
13+
// or
14+
// `node /var/runtime/index.js`,
15+
// so we never drop into the `if` in the first place.
16+
if (process.argv.includes('build') || process.env.SENTRY_BUILD_PHASE) {
17+
process.env.SENTRY_BUILD_PHASE = 'true';
18+
return true;
19+
}
20+
21+
return false;
22+
}

0 commit comments

Comments
 (0)