Skip to content

Commit b16f01d

Browse files
committed
fix(nextjs): Check process.env for Spotlight config as fallback
The valueInjectionLoader may not run in all scenarios (e.g., Next.js dev mode with Turbopack). Next.js already replaces process.env.NEXT_PUBLIC_* at build time, so we can use this as a fallback when globalThis is not set. Also removes debug logging that was added for CI debugging and fixes prettier formatting issues.
1 parent bf2e634 commit b16f01d

File tree

4 files changed

+10
-37
lines changed

4 files changed

+10
-37
lines changed
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import * as Sentry from '@sentry/nextjs';
22

3-
// Debug: Log what the valueInjectionLoader injected (if anything)
4-
// The loader should inject globalThis["NEXT_PUBLIC_SENTRY_SPOTLIGHT"] = "true" before this code
5-
console.log('[Sentry Debug] globalThis.NEXT_PUBLIC_SENTRY_SPOTLIGHT:', (globalThis as Record<string, unknown>)['NEXT_PUBLIC_SENTRY_SPOTLIGHT']);
6-
console.log('[Sentry Debug] process.env.NEXT_PUBLIC_SENTRY_SPOTLIGHT:', process.env.NEXT_PUBLIC_SENTRY_SPOTLIGHT);
7-
83
Sentry.init({
94
environment: 'qa',
105
dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN,
116
tunnel: `http://localhost:3031/`,
127
tracesSampleRate: 1.0,
138
// Note: We don't explicitly set spotlight here - it should be auto-enabled
14-
// from NEXT_PUBLIC_SENTRY_SPOTLIGHT env var which is injected by the SDK's
15-
// valueInjectionLoader to globalThis
9+
// from NEXT_PUBLIC_SENTRY_SPOTLIGHT env var (replaced by Next.js at build time)
1610
});

dev-packages/e2e-tests/test-applications/nextjs-15-spotlight/tests/spotlight.test.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,17 @@ import { expect, test } from '@playwright/test';
22

33
test.describe('Spotlight auto-enablement in Next.js development mode', () => {
44
test('Spotlight is automatically enabled when NEXT_PUBLIC_SENTRY_SPOTLIGHT=true', async ({ page }) => {
5-
// Capture console logs for debugging
6-
const consoleLogs: string[] = [];
7-
page.on('console', msg => {
8-
const text = msg.text();
9-
consoleLogs.push(`[${msg.type()}] ${text}`);
10-
// Print Sentry debug logs immediately
11-
if (text.includes('[Sentry Debug]') || text.includes('Spotlight')) {
12-
console.log(`BROWSER: ${text}`);
13-
}
14-
});
15-
165
await page.goto('/');
176

187
// Wait for client-side hydration and Sentry initialization
198
await page.waitForTimeout(3000);
209

21-
// Print all console logs for debugging
22-
console.log('=== Browser Console Logs ===');
23-
consoleLogs.forEach(log => console.log(log));
24-
console.log('=== End Console Logs ===');
25-
26-
// Check environment variable is accessible (injected at build time)
10+
// Check environment variable is accessible (injected at build time by Next.js)
2711
const envValue = await page.getByTestId('env-value').textContent();
28-
console.log('env-value content:', envValue);
2912
expect(envValue).toContain('true');
3013

31-
// Check globalThis value (set by valueInjectionLoader)
32-
const globalThisValue = await page.getByTestId('globalthis-value').textContent();
33-
console.log('globalthis-value content:', globalThisValue);
34-
3514
// Check Spotlight integration is enabled
3615
const spotlightStatus = await page.getByTestId('spotlight-enabled').textContent();
37-
console.log('spotlight-enabled content:', spotlightStatus);
3816
expect(spotlightStatus).toBe('ENABLED');
3917
});
4018

packages/browser/test/build-artifacts.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ describe('Build Artifacts - env.ts', () => {
7070
const content = fs.readFileSync(esmDevPath, 'utf-8');
7171

7272
// Filter out comments
73-
const lines = content
74-
.split('\n')
75-
.filter(line => !line.trim().startsWith('//') && !line.trim().startsWith('*'));
73+
const lines = content.split('\n').filter(line => !line.trim().startsWith('//') && !line.trim().startsWith('*'));
7674
const codeOnly = lines.join('\n');
7775

7876
// Should NOT contain import.meta in actual code
@@ -82,9 +80,7 @@ describe('Build Artifacts - env.ts', () => {
8280
if (fs.existsSync(esmProdPath)) {
8381
const content = fs.readFileSync(esmProdPath, 'utf-8');
8482

85-
const lines = content
86-
.split('\n')
87-
.filter(line => !line.trim().startsWith('//') && !line.trim().startsWith('*'));
83+
const lines = content.split('\n').filter(line => !line.trim().startsWith('//') && !line.trim().startsWith('*'));
8884
const codeOnly = lines.join('\n');
8985

9086
expect(codeOnly).not.toContain('import.meta.env');

packages/nextjs/src/client/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ function getDefaultIntegrations(options: BrowserOptions): Integration[] {
149149
//
150150
// We handle this in the Next.js SDK rather than the browser SDK because the browser SDK's
151151
// auto-detection is development-only (stripped from production builds that users install).
152-
const spotlightEnvValue = globalWithInjectedValues.NEXT_PUBLIC_SENTRY_SPOTLIGHT;
152+
//
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.
156+
const spotlightEnvValue =
157+
globalWithInjectedValues.NEXT_PUBLIC_SENTRY_SPOTLIGHT ?? process.env.NEXT_PUBLIC_SENTRY_SPOTLIGHT;
153158

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

0 commit comments

Comments
 (0)