Skip to content

Commit fad6c48

Browse files
authored
feat(nextjs): Default to VERCEL_ENV as environment (#7227)
1 parent c46c56c commit fad6c48

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

packages/nextjs/src/client/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { configureScope, init as reactInit, Integrations } from '@sentry/react';
44
import { BrowserTracing, defaultRequestInstrumentationOptions, hasTracingEnabled } from '@sentry/tracing';
55
import type { EventProcessor } from '@sentry/types';
66

7+
import { getVercelEnv } from '../common/getVercelEnv';
78
import { buildMetadata } from '../common/metadata';
89
import { addOrUpdateIntegration } from '../common/userIntegrations';
910
import { nextRouterInstrumentation } from './performance';
@@ -38,7 +39,9 @@ const globalWithInjectedValues = global as typeof global & {
3839
export function init(options: BrowserOptions): void {
3940
applyTunnelRouteOption(options);
4041
buildMetadata(options, ['nextjs', 'react']);
41-
options.environment = options.environment || process.env.NODE_ENV;
42+
43+
options.environment = options.environment || getVercelEnv(true) || process.env.NODE_ENV;
44+
4245
addClientIntegrations(options);
4346

4447
reactInit(options);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Returns an environment setting value determined by Vercel's `VERCEL_ENV` environment variable.
3+
*
4+
* @param isClient Flag to indicate whether to use the `NEXT_PUBLIC_` prefixed version of the environment variable.
5+
*/
6+
export function getVercelEnv(isClient: boolean): string | undefined {
7+
const vercelEnvVar = isClient ? process.env.NEXT_PUBLIC_VERCEL_ENV : process.env.VERCEL_ENV;
8+
return vercelEnvVar ? `vercel-${vercelEnvVar}` : undefined;
9+
}

packages/nextjs/src/edge/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
stackParserFromStackParserOptions,
1111
} from '@sentry/utils';
1212

13+
import { getVercelEnv } from '../common/getVercelEnv';
1314
import { EdgeClient } from './edgeclient';
1415
import { makeEdgeTransport } from './transport';
1516

@@ -46,9 +47,8 @@ export function init(options: EdgeOptions = {}): void {
4647
}
4748
}
4849

49-
if (options.environment === undefined && process.env.SENTRY_ENVIRONMENT) {
50-
options.environment = process.env.SENTRY_ENVIRONMENT;
51-
}
50+
options.environment =
51+
options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV;
5252

5353
if (options.autoSessionTracking === undefined && options.dsn !== undefined) {
5454
options.autoSessionTracking = true;

packages/nextjs/src/server/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { escapeStringForRegex, logger } from '@sentry/utils';
99
import * as domainModule from 'domain';
1010
import * as path from 'path';
1111

12+
import { getVercelEnv } from '../common/getVercelEnv';
1213
import { buildMetadata } from '../common/metadata';
1314
import type { IntegrationWithExclusionOption } from '../common/userIntegrations';
1415
import { addOrUpdateIntegration } from '../common/userIntegrations';
@@ -79,7 +80,10 @@ export function init(options: NodeOptions): void {
7980
}
8081

8182
buildMetadata(options, ['nextjs', 'node']);
82-
options.environment = options.environment || process.env.NODE_ENV;
83+
84+
options.environment =
85+
options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV;
86+
8387
addServerIntegrations(options);
8488
// Right now we only capture frontend sessions for Next.js
8589
options.autoSessionTracking = false;

0 commit comments

Comments
 (0)