diff --git a/packages/nextjs/src/config/withSentryConfig.ts b/packages/nextjs/src/config/withSentryConfig.ts index 7eedcc763e9b..543f271c1999 100644 --- a/packages/nextjs/src/config/withSentryConfig.ts +++ b/packages/nextjs/src/config/withSentryConfig.ts @@ -15,6 +15,7 @@ import { getNextjsVersion } from './util'; import { constructWebpackConfigFunction } from './webpack'; let showedExportModeTunnelWarning = false; +let showedExperimentalBuildModeWarning = false; /** * Modifies the passed in Next.js configuration with automatic build-time instrumentation and source map upload. @@ -67,6 +68,27 @@ function getFinalConfigObject( } } + if (process.argv.includes('--experimental-build-mode')) { + if (!showedExperimentalBuildModeWarning) { + showedExperimentalBuildModeWarning = true; + // eslint-disable-next-line no-console + console.warn( + '[@sentry/nextjs] The Sentry Next.js SDK does not currently fully support next build --experimental-build-mode', + ); + } + if (process.argv.includes('generate')) { + // Next.js v15.3.0-canary.1 splits the experimental build into two phases: + // 1. compile: Code compilation + // 2. generate: Environment variable inlining and prerendering (We don't instrument this phase, we inline in the compile phase) + // + // We assume a single “full” build and reruns Webpack instrumentation in both phases. + // During the generate step it collides with Next.js’s inliner + // producing malformed JS and build failures. + // We skip Sentry processing during generate to avoid this issue. + return incomingUserNextConfigObject; + } + } + setUpBuildTimeVariables(incomingUserNextConfigObject, userSentryOptions, releaseName); const nextJsVersion = getNextjsVersion(); diff --git a/packages/nextjs/test/config/withSentryConfig.test.ts b/packages/nextjs/test/config/withSentryConfig.test.ts index 6ad9a83e6791..f9db1a68771e 100644 --- a/packages/nextjs/test/config/withSentryConfig.test.ts +++ b/packages/nextjs/test/config/withSentryConfig.test.ts @@ -50,4 +50,29 @@ describe('withSentryConfig', () => { expect(exportedNextConfigFunction).toHaveBeenCalledWith(defaultRuntimePhase, defaultsObject); }); + + it('handles experimental build mode correctly', () => { + const originalArgv = process.argv; + const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + + try { + process.argv = [...originalArgv, '--experimental-build-mode']; + materializeFinalNextConfig(exportedNextConfig); + + expect(consoleWarnSpy).toHaveBeenCalledWith( + '[@sentry/nextjs] The Sentry Next.js SDK does not currently fully support next build --experimental-build-mode', + ); + + // Generate phase + process.argv = [...process.argv, 'generate']; + const generateConfig = materializeFinalNextConfig(exportedNextConfig); + + expect(generateConfig).toEqual(exportedNextConfig); + + expect(consoleWarnSpy).toHaveBeenCalledTimes(1); + } finally { + process.argv = originalArgv; + consoleWarnSpy.mockRestore(); + } + }); });