Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion packages/nextjs/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { configureScope, init as reactInit, Integrations } from '@sentry/react';
import { BrowserTracing, defaultRequestInstrumentationOptions, hasTracingEnabled } from '@sentry/tracing';
import type { EventProcessor } from '@sentry/types';

import { getVercelEnv } from '../common/getVercelEnv';
import { buildMetadata } from '../common/metadata';
import { addOrUpdateIntegration } from '../common/userIntegrations';
import { nextRouterInstrumentation } from './performance';
Expand Down Expand Up @@ -38,7 +39,9 @@ const globalWithInjectedValues = global as typeof global & {
export function init(options: BrowserOptions): void {
applyTunnelRouteOption(options);
buildMetadata(options, ['nextjs', 'react']);
options.environment = options.environment || process.env.NODE_ENV;

options.environment = options.environment || getVercelEnv(true) || process.env.NODE_ENV;

addClientIntegrations(options);

reactInit(options);
Expand Down
9 changes: 9 additions & 0 deletions packages/nextjs/src/common/getVercelEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Returns an environment setting value determined by Vercel's `VERCEL_ENV` environment variable.
*
* @param isClient Flag to indicate whether to use the `NEXT_PUBLIC_` prefixed version of the environment variable.
*/
export function getVercelEnv(isClient: boolean): string | undefined {
const vercelEnvVar = isClient ? process.env.NEXT_PUBLIC_VERCEL_ENV : process.env.VERCEL_ENV;
return vercelEnvVar ? `vercel-${vercelEnvVar}` : undefined;
}
6 changes: 3 additions & 3 deletions packages/nextjs/src/edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
stackParserFromStackParserOptions,
} from '@sentry/utils';

import { getVercelEnv } from '../common/getVercelEnv';
import { EdgeClient } from './edgeclient';
import { makeEdgeTransport } from './transport';

Expand Down Expand Up @@ -46,9 +47,8 @@ export function init(options: EdgeOptions = {}): void {
}
}

if (options.environment === undefined && process.env.SENTRY_ENVIRONMENT) {
options.environment = process.env.SENTRY_ENVIRONMENT;
}
options.environment =
options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV;

if (options.autoSessionTracking === undefined && options.dsn !== undefined) {
options.autoSessionTracking = true;
Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { escapeStringForRegex, logger } from '@sentry/utils';
import * as domainModule from 'domain';
import * as path from 'path';

import { getVercelEnv } from '../common/getVercelEnv';
import { buildMetadata } from '../common/metadata';
import type { IntegrationWithExclusionOption } from '../common/userIntegrations';
import { addOrUpdateIntegration } from '../common/userIntegrations';
Expand Down Expand Up @@ -79,7 +80,10 @@ export function init(options: NodeOptions): void {
}

buildMetadata(options, ['nextjs', 'node']);
options.environment = options.environment || process.env.NODE_ENV;

options.environment =
options.environment || process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV;

addServerIntegrations(options);
// Right now we only capture frontend sessions for Next.js
options.autoSessionTracking = false;
Expand Down