Skip to content

feat(nextjs): Add disableSentryWebpackConfig flag #17013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
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
12 changes: 12 additions & 0 deletions packages/nextjs/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,18 @@ export type SentryBuildOptions = {
*/
disableManifestInjection?: boolean;

/**
* Disables automatic injection of Sentry's Webpack configuration.
*
* By default, the Sentry Next.js SDK injects its own Webpack configuration to enable features such as
* source map upload and automatic instrumentation. Set this option to `true` if you want to prevent
* the SDK from modifying your Webpack config (for example, if you want to handle Sentry integration manually
* or if you are on an older version of Next.js while using Turbopack).
*
* @default false
*/
disableSentryWebpackConfig?: boolean;

/**
* Contains a set of experimental flags that might change in future releases. These flags enable
* features that are still in development and may be modified, renamed, or removed without notice.
Expand Down
7 changes: 4 additions & 3 deletions packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,10 @@ function getFinalConfigObject(
],
},
}),
webpack: !isTurbopack
? constructWebpackConfigFunction(incomingUserNextConfigObject, userSentryOptions, releaseName, routeManifest)
: undefined,
webpack:
isTurbopack || userSentryOptions.disableSentryWebpackConfig
? incomingUserNextConfigObject.webpack // just return the original webpack config
: constructWebpackConfigFunction(incomingUserNextConfigObject, userSentryOptions, releaseName, routeManifest),
...(isTurbopackSupported && isTurbopack
? {
turbopack: constructTurbopackConfig({
Expand Down
124 changes: 124 additions & 0 deletions packages/nextjs/test/config/withSentryConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,130 @@ describe('withSentryConfig', () => {
});
});

describe('webpack configuration behavior', () => {
const originalTurbopack = process.env.TURBOPACK;

afterEach(() => {
vi.restoreAllMocks();
process.env.TURBOPACK = originalTurbopack;
});

it('uses constructed webpack function when Turbopack is disabled and disableSentryWebpackConfig is false/undefined', () => {
delete process.env.TURBOPACK;

// default behavior
const finalConfigUndefined = materializeFinalNextConfig(exportedNextConfig);
expect(finalConfigUndefined.webpack).toBeInstanceOf(Function);

const sentryOptions = {
disableSentryWebpackConfig: false,
};
const finalConfigFalse = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);
expect(finalConfigFalse.webpack).toBeInstanceOf(Function);
});

it('preserves original webpack config when disableSentryWebpackConfig is true (regardless of Turbopack)', () => {
const originalWebpackFunction = vi.fn();
const configWithWebpack = {
...exportedNextConfig,
webpack: originalWebpackFunction,
};

const sentryOptions = {
disableSentryWebpackConfig: true,
};

delete process.env.TURBOPACK;
const finalConfigWithoutTurbopack = materializeFinalNextConfig(configWithWebpack, undefined, sentryOptions);
expect(finalConfigWithoutTurbopack.webpack).toBe(originalWebpackFunction);

process.env.TURBOPACK = '1';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.0');
const finalConfigWithTurbopack = materializeFinalNextConfig(configWithWebpack, undefined, sentryOptions);
expect(finalConfigWithTurbopack.webpack).toBe(originalWebpackFunction);
});

it('preserves original webpack config when Turbopack is enabled (ignores disableSentryWebpackConfig flag)', () => {
process.env.TURBOPACK = '1';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.0');

const originalWebpackFunction = vi.fn();
const configWithWebpack = {
...exportedNextConfig,
webpack: originalWebpackFunction,
};

const sentryOptionsWithFalse = {
disableSentryWebpackConfig: false,
};
const finalConfigWithFalse = materializeFinalNextConfig(configWithWebpack, undefined, sentryOptionsWithFalse);
expect(finalConfigWithFalse.webpack).toBe(originalWebpackFunction);

const finalConfigWithUndefined = materializeFinalNextConfig(configWithWebpack);
expect(finalConfigWithUndefined.webpack).toBe(originalWebpackFunction);

const sentryOptionsWithTrue = {
disableSentryWebpackConfig: true,
};
const finalConfigWithTrue = materializeFinalNextConfig(configWithWebpack, undefined, sentryOptionsWithTrue);
expect(finalConfigWithTrue.webpack).toBe(originalWebpackFunction);
});

it('preserves original webpack config when Turbopack is enabled and disableSentryWebpackConfig is true', () => {
process.env.TURBOPACK = '1';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.0');

const sentryOptions = {
disableSentryWebpackConfig: true,
};

const originalWebpackFunction = vi.fn();
const configWithWebpack = {
...exportedNextConfig,
webpack: originalWebpackFunction,
};

const finalConfig = materializeFinalNextConfig(configWithWebpack, undefined, sentryOptions);

expect(finalConfig.webpack).toBe(originalWebpackFunction);
});

it('preserves undefined webpack when Turbopack is enabled, disableSentryWebpackConfig is true, and no original webpack config exists', () => {
process.env.TURBOPACK = '1';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.0');

const sentryOptions = {
disableSentryWebpackConfig: true,
};

const configWithoutWebpack = {
...exportedNextConfig,
};
delete configWithoutWebpack.webpack;

const finalConfig = materializeFinalNextConfig(configWithoutWebpack, undefined, sentryOptions);

expect(finalConfig.webpack).toBeUndefined();
});

it('includes turbopack config when Turbopack is supported and enabled', () => {
process.env.TURBOPACK = '1';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.3.0');

const finalConfig = materializeFinalNextConfig(exportedNextConfig);

expect(finalConfig.turbopack).toBeDefined();
});

it('does not include turbopack config when Turbopack is not enabled', () => {
delete process.env.TURBOPACK;

const finalConfig = materializeFinalNextConfig(exportedNextConfig);

expect(finalConfig.turbopack).toBeUndefined();
});
});

describe('release injection behavior', () => {
afterEach(() => {
vi.restoreAllMocks();
Expand Down
Loading