Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 11 additions & 10 deletions packages/nextjs/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,22 +501,23 @@ export type SentryBuildOptions = {
*/
disableSentryWebpackConfig?: boolean;

/**
* When true (and Next.js >= 15), use the runAfterProductionCompile hook to consolidate sourcemap uploads
* into a single operation after builds complete, reducing build time.
*
* When false, use the traditional approach of uploading sourcemaps during each webpack build. For Turbopack no sourcemaps will be uploaded.
*
* @default true for Turbopack, false for Webpack
*/
useRunAfterProductionCompileHook?: 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.
* Use with caution in production environments.
*/
_experimental?: Partial<{
/**
* When true (and Next.js >= 15), use the runAfterProductionCompile hook to consolidate sourcemap uploads
* into a single operation after turbopack builds complete, reducing build time.
*
* When false, use the traditional approach of uploading sourcemaps during each webpack build.
*
* @default false
*/
useRunAfterProductionCompileHook?: boolean;
thirdPartyOriginStackFrames: boolean;
thirdPartyOriginStackFrames?: boolean;
}>;
};

Expand Down
8 changes: 6 additions & 2 deletions packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ function getFinalConfigObject(
}
}

if (userSentryOptions?._experimental?.useRunAfterProductionCompileHook === true && supportsProductionCompileHook()) {
// If not explicitly set, turbopack uses the runAfterProductionCompile hook (as there are no alternatives), webpack does not.
const shouldUseRunAfterProductionCompileHook =
userSentryOptions?.useRunAfterProductionCompileHook ?? (isTurbopack ? true : false);

if (shouldUseRunAfterProductionCompileHook && supportsProductionCompileHook()) {
if (incomingUserNextConfigObject?.compiler?.runAfterProductionCompile === undefined) {
incomingUserNextConfigObject.compiler ??= {};
incomingUserNextConfigObject.compiler.runAfterProductionCompile = async ({ distDir }) => {
Expand Down Expand Up @@ -379,7 +383,7 @@ function getFinalConfigObject(
releaseName,
routeManifest,
nextJsVersion,
useRunAfterProductionCompileHook: userSentryOptions._experimental?.useRunAfterProductionCompileHook,
useRunAfterProductionCompileHook: shouldUseRunAfterProductionCompileHook,
}),
...(isTurbopackSupported && isTurbopack
? {
Expand Down
3 changes: 1 addition & 2 deletions packages/nextjs/test/config/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ export async function materializeFinalWebpackConfig(options: {
routeManifest: options.routeManifest,
nextJsVersion: options.nextJsVersion,
useRunAfterProductionCompileHook:
options.useRunAfterProductionCompileHook ??
options.sentryBuildTimeOptions?._experimental?.useRunAfterProductionCompileHook,
options.useRunAfterProductionCompileHook ?? options.sentryBuildTimeOptions?.useRunAfterProductionCompileHook,
});

// call it to get concrete values for comparison
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ describe('constructWebpackConfigFunction()', () => {
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
sentryBuildTimeOptions: {
_experimental: {
useRunAfterProductionCompileHook: true,
},
useRunAfterProductionCompileHook: true,
},
});

Expand All @@ -128,9 +126,7 @@ describe('constructWebpackConfigFunction()', () => {
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: serverBuildContext,
sentryBuildTimeOptions: {
_experimental: {
useRunAfterProductionCompileHook: false,
},
useRunAfterProductionCompileHook: false,
},
});

Expand Down
119 changes: 34 additions & 85 deletions packages/nextjs/test/config/withSentryConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,27 +769,23 @@ describe('withSentryConfig', () => {
vi.restoreAllMocks();
});

it('sets up runAfterProductionCompile hook when experimental flag is enabled and version is supported', () => {
it('sets up runAfterProductionCompile hook when flag is enabled and version is supported', () => {
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: true,
},
useRunAfterProductionCompileHook: true,
};

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

expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
});

it('does not set up hook when experimental flag is disabled', () => {
it('does not set up hook when flag is disabled', () => {
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: false,
},
useRunAfterProductionCompileHook: false,
};

const cleanConfig = { ...exportedNextConfig };
Expand All @@ -804,9 +800,7 @@ describe('withSentryConfig', () => {
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false);

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: true,
},
useRunAfterProductionCompileHook: true,
};

const cleanConfig = { ...exportedNextConfig };
Expand All @@ -829,9 +823,7 @@ describe('withSentryConfig', () => {
};

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: true,
},
useRunAfterProductionCompileHook: true,
};

const finalConfig = materializeFinalNextConfig(configWithExistingHook, undefined, sentryOptions);
Expand All @@ -852,9 +844,7 @@ describe('withSentryConfig', () => {
};

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: true,
},
useRunAfterProductionCompileHook: true,
};

materializeFinalNextConfig(configWithInvalidHook, undefined, sentryOptions);
Expand All @@ -873,9 +863,7 @@ describe('withSentryConfig', () => {
delete configWithoutCompiler.compiler;

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: true,
},
useRunAfterProductionCompileHook: true,
};

const finalConfig = materializeFinalNextConfig(configWithoutCompiler, undefined, sentryOptions);
Expand All @@ -884,16 +872,12 @@ describe('withSentryConfig', () => {
expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
});

it('works with turbopack builds when TURBOPACK env is set', () => {
it('defaults to true for turbopack when useRunAfterProductionCompileHook is not specified', () => {
process.env.TURBOPACK = '1';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: true,
},
};
const sentryOptions = {}; // No useRunAfterProductionCompileHook specified

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

Expand All @@ -902,51 +886,27 @@ describe('withSentryConfig', () => {
delete process.env.TURBOPACK;
});

it('works with webpack builds when TURBOPACK env is not set', () => {
it('defaults to false for webpack when useRunAfterProductionCompileHook is not specified', () => {
delete process.env.TURBOPACK;
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: true,
},
};

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

expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
});
});

describe('experimental flag handling', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('respects useRunAfterProductionCompileHook: true', () => {
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: true,
},
};
const sentryOptions = {}; // No useRunAfterProductionCompileHook specified

const cleanConfig = { ...exportedNextConfig };
delete cleanConfig.compiler;

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

expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
expect(finalConfig.compiler?.runAfterProductionCompile).toBeUndefined();
});

it('respects useRunAfterProductionCompileHook: false', () => {
it('respects explicit false setting for turbopack', () => {
process.env.TURBOPACK = '1';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: false,
},
useRunAfterProductionCompileHook: false,
};

const cleanConfig = { ...exportedNextConfig };
Expand All @@ -955,61 +915,50 @@ describe('withSentryConfig', () => {
const finalConfig = materializeFinalNextConfig(cleanConfig, undefined, sentryOptions);

expect(finalConfig.compiler?.runAfterProductionCompile).toBeUndefined();

delete process.env.TURBOPACK;
});

it('does not set up hook when experimental flag is undefined', () => {
it('respects explicit true setting for webpack', () => {
delete process.env.TURBOPACK;
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);

const sentryOptions = {
_experimental: {
// useRunAfterProductionCompileHook not specified
},
useRunAfterProductionCompileHook: true,
};

const cleanConfig = { ...exportedNextConfig };
delete cleanConfig.compiler;

const finalConfig = materializeFinalNextConfig(cleanConfig, undefined, sentryOptions);
const finalConfig = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);

expect(finalConfig.compiler?.runAfterProductionCompile).toBeUndefined();
expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
});

it('does not set up hook when _experimental is undefined', () => {
it('works with turbopack builds when TURBOPACK env is set', () => {
process.env.TURBOPACK = '1';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);

const sentryOptions = {
// no _experimental property
useRunAfterProductionCompileHook: true,
};

const cleanConfig = { ...exportedNextConfig };
delete cleanConfig.compiler;
const finalConfig = materializeFinalNextConfig(exportedNextConfig, undefined, sentryOptions);

const finalConfig = materializeFinalNextConfig(cleanConfig, undefined, sentryOptions);
expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);

expect(finalConfig.compiler?.runAfterProductionCompile).toBeUndefined();
delete process.env.TURBOPACK;
});

it('combines experimental flag with other configurations correctly', () => {
process.env.TURBOPACK = '1';
vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.4.1');
it('works with webpack builds when TURBOPACK env is not set', () => {
delete process.env.TURBOPACK;
vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true);

const sentryOptions = {
_experimental: {
useRunAfterProductionCompileHook: true,
},
sourcemaps: {},
tunnelRoute: '/tunnel',
useRunAfterProductionCompileHook: true,
};

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

// Should have both turbopack sourcemap config AND runAfterProductionCompile hook
expect(finalConfig.productionBrowserSourceMaps).toBe(true);
expect(finalConfig.compiler?.runAfterProductionCompile).toBeInstanceOf(Function);
expect(finalConfig.rewrites).toBeInstanceOf(Function);

delete process.env.TURBOPACK;
});
});
});
Loading