Skip to content

fix(nextjs): Use env variable for build detection #4608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 23, 2022
Merged
Changes from all 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
16 changes: 12 additions & 4 deletions packages/nextjs/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ const domain = domainModule as typeof domainModule & { active: (domainModule.Dom
// During build, the main process is invoked by
// `node next build`
// and child processes are invoked as
// `node <path>/node_modules/jest-worker/build/workers/processChild.js`,
// whereas at runtime the process is invoked as
// `node <path>/node_modules/.../jest-worker/processChild.js`.
// The former is (obviously) easy to recognize, but the latter could happen at runtime as well. Fortunately, the main
// process hits this file before any of the child processes do, so we're able to set an env variable which the child
// processes can then check. During runtime, the main process is invoked as
// `node next start`
// or
// `node /var/runtime/index.js`.
const isBuild = new RegExp('build').test(process.argv.toString());
// `node /var/runtime/index.js`,
// so we never drop into the `if` in the first place.
let isBuild = false;
if (process.argv.includes('build') || process.env.SENTRY_BUILD_PHASE) {
process.env.SENTRY_BUILD_PHASE = 'true';
isBuild = true;
}

const isVercel = !!process.env.VERCEL;

/** Inits the Sentry NextJS SDK on node. */
Expand Down