Skip to content

Commit dfc5d20

Browse files
committed
Extract into function
1 parent 3be7e84 commit dfc5d20

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

packages/nextjs/src/client/index.ts

Lines changed: 2 additions & 4 deletions
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';
@@ -39,10 +40,7 @@ export function init(options: BrowserOptions): void {
3940
applyTunnelRouteOption(options);
4041
buildMetadata(options, ['nextjs', 'react']);
4142

42-
options.environment =
43-
options.environment ||
44-
(process.env.NEXT_PUBLIC_VERCEL_ENV ? `vercel-${process.env.NEXT_PUBLIC_VERCEL_ENV}` : undefined) ||
45-
process.env.NODE_ENV;
43+
options.environment = options.environment || getVercelEnv(true) || process.env.NODE_ENV;
4644

4745
addClientIntegrations(options);
4846

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: 2 additions & 4 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

@@ -47,10 +48,7 @@ export function init(options: EdgeOptions = {}): void {
4748
}
4849

4950
options.environment =
50-
options.environment ||
51-
process.env.SENTRY_ENVIRONMENT ||
52-
(process.env.VERCEL_ENV ? `vercel-${process.env.VERCEL_ENV}` : undefined) ||
53-
process.env.NODE_ENV;
51+
options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV;
5452

5553
if (options.autoSessionTracking === undefined && options.dsn !== undefined) {
5654
options.autoSessionTracking = true;

packages/nextjs/src/server/index.ts

Lines changed: 2 additions & 4 deletions
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';
@@ -81,10 +82,7 @@ export function init(options: NodeOptions): void {
8182
buildMetadata(options, ['nextjs', 'node']);
8283

8384
options.environment =
84-
options.environment ||
85-
process.env.SENTRY_ENVIRONMENT ||
86-
(process.env.VERCEL_ENV ? `vercel-${process.env.VERCEL_ENV}` : undefined) ||
87-
process.env.NODE_ENV;
85+
options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV;
8886

8987
addServerIntegrations(options);
9088
// Right now we only capture frontend sessions for Next.js

0 commit comments

Comments
 (0)