Skip to content

Commit 40c4967

Browse files
committed
fix(nextjs): Inject _sentrySpotlight via globalThis for reliable Spotlight auto-enablement
The valueInjectionLoader now injects both NEXT_PUBLIC_SENTRY_SPOTLIGHT and _sentrySpotlight into globalThis. This provides a reliable fallback chain: 1. globalThis.NEXT_PUBLIC_SENTRY_SPOTLIGHT - primary (valueInjectionLoader) 2. process.env._sentrySpotlight - fallback (Next.js env config replacement) 3. globalThis._sentrySpotlight - final fallback (valueInjectionLoader) This ensures Spotlight auto-enablement works in Next.js 15 Turbopack dev mode where Next.js doesn't reliably replace process.env values in node_modules code. Also adds _sentrySpotlight to the env config buildTimeVariables for build-time replacement in production builds.
1 parent b16f01d commit 40c4967

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

packages/nextjs/src/client/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
4343
_sentryBasePath?: string;
4444
_sentryRelease?: string;
4545
_experimentalThirdPartyOriginStackFrames?: string;
46+
_sentrySpotlight?: string;
4647
NEXT_PUBLIC_SENTRY_SPOTLIGHT?: string;
4748
};
4849

@@ -150,11 +151,14 @@ function getDefaultIntegrations(options: BrowserOptions): Integration[] {
150151
// We handle this in the Next.js SDK rather than the browser SDK because the browser SDK's
151152
// auto-detection is development-only (stripped from production builds that users install).
152153
//
153-
// Check both globalThis (valueInjectionLoader) and process.env (Next.js build-time replacement).
154-
// globalThis is for Turbopack builds, process.env is replaced by Next.js at build time for
155-
// NEXT_PUBLIC_* variables. We need both because valueInjectionLoader may not run in all scenarios.
154+
// Check multiple locations for Spotlight config (in priority order):
155+
// 1. globalThis.NEXT_PUBLIC_SENTRY_SPOTLIGHT - set by valueInjectionLoader
156+
// 2. process.env._sentrySpotlight - set via Next.js env config (works in node_modules)
157+
// 3. process.env.NEXT_PUBLIC_SENTRY_SPOTLIGHT - direct env var (may not work in node_modules in dev mode)
156158
const spotlightEnvValue =
157-
globalWithInjectedValues.NEXT_PUBLIC_SENTRY_SPOTLIGHT ?? process.env.NEXT_PUBLIC_SENTRY_SPOTLIGHT;
159+
globalWithInjectedValues.NEXT_PUBLIC_SENTRY_SPOTLIGHT ??
160+
process.env._sentrySpotlight ??
161+
globalWithInjectedValues._sentrySpotlight;
158162

159163
// Parse the env var value if present (could be 'true', 'false', or a URL)
160164
let envSpotlight: boolean | string | undefined;

packages/nextjs/src/config/turbopack/generateValueInjectionRules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ export function generateValueInjectionRules({
3838
// Inject Spotlight config for client so the browser SDK can auto-enable Spotlight.
3939
// Next.js doesn't expose NEXT_PUBLIC_* vars to node_modules, so we inject it via
4040
// globalThis. The browser SDK's getEnvValue() checks globalThis as a fallback.
41+
// We also inject _sentrySpotlight as a fallback for the Next.js SDK's client/index.ts.
4142
if (spotlightConfig) {
4243
clientValues.NEXT_PUBLIC_SENTRY_SPOTLIGHT = spotlightConfig;
44+
clientValues._sentrySpotlight = spotlightConfig;
4345
}
4446

4547
if (Object.keys(isomorphicValues).length > 0) {

packages/nextjs/src/config/webpack.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,9 @@ function addValueInjectionLoader({
770770
_sentryRouteManifest: JSON.stringify(routeManifest),
771771
// Inject Spotlight config so the browser SDK can auto-enable Spotlight.
772772
// The browser SDK's getEnvValue() checks globalThis as a fallback for bundler-injected values.
773+
// We also inject _sentrySpotlight as a fallback for the Next.js SDK's client/index.ts.
773774
NEXT_PUBLIC_SENTRY_SPOTLIGHT: spotlightConfig,
775+
_sentrySpotlight: spotlightConfig,
774776
};
775777

776778
if (buildContext.isServer) {

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,13 @@ function setUpBuildTimeVariables(
623623
buildTimeVariables._sentryRelease = releaseName;
624624
}
625625

626-
// Note: Spotlight config (_sentrySpotlight) is injected via webpack DefinePlugin in webpack.ts
627-
// because Next.js doesn't replace process.env.* in node_modules code via next.config.js env.
626+
// Inject Spotlight config so the Next.js SDK can auto-enable Spotlight.
627+
// We use an internal name (_sentrySpotlight) because Next.js replaces process.env.* in ALL code
628+
// (including node_modules) for variables defined in the env config.
629+
const spotlightConfig = userNextConfig.env?.NEXT_PUBLIC_SENTRY_SPOTLIGHT ?? process.env.NEXT_PUBLIC_SENTRY_SPOTLIGHT;
630+
if (spotlightConfig) {
631+
buildTimeVariables._sentrySpotlight = spotlightConfig;
632+
}
628633

629634
if (typeof userNextConfig.env === 'object') {
630635
userNextConfig.env = { ...buildTimeVariables, ...userNextConfig.env };

0 commit comments

Comments
 (0)