Skip to content

Commit 79d9231

Browse files
committed
add application key top level
1 parent 862359b commit 79d9231

5 files changed

Lines changed: 66 additions & 2 deletions

File tree

packages/nextjs/src/config/getBuildPluginOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ export function getBuildPluginOptions({
309309
const skipSourcemapsUpload = shouldSkipSourcemapUpload(buildTool, useRunAfterProductionCompileHook);
310310

311311
return {
312+
applicationKey: sentryBuildOptions.applicationKey,
312313
authToken: sentryBuildOptions.authToken,
313314
headers: sentryBuildOptions.headers,
314315
org: sentryBuildOptions.org,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export function constructTurbopackConfig({
6666
// so it is safe even for node_modules with strict initialization order.
6767
// We only exclude Next.js build polyfills which contain non-standard syntax that causes
6868
// parse errors when any code is prepended (Turbopack re-parses the loader output).
69-
const applicationKey = userSentryOptions?._experimental?.turbopackApplicationKey;
69+
// eslint-disable-next-line deprecation/deprecation
70+
const applicationKey = userSentryOptions?.applicationKey ?? userSentryOptions?._experimental?.turbopackApplicationKey;
7071
if (applicationKey && nextJsVersion && supportsTurbopackRuleCondition(nextJsVersion)) {
7172
newConfig.rules = safelyAddTurbopackRule(newConfig.rules, {
7273
matcher: '*.{ts,tsx,js,jsx,mjs,cjs}',

packages/nextjs/src/config/types.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,18 @@ export type SentryBuildOptions = {
463463
};
464464
};
465465

466+
/**
467+
* A key that is used to identify the application in the Sentry bundler plugins.
468+
* This key is used by the `thirdPartyErrorFilterIntegration` to filter out errors
469+
* originating from third-party scripts.
470+
*
471+
* For webpack builds, this is forwarded to the `@sentry/webpack-plugin`.
472+
* For Turbopack builds, this injects module metadata via a custom loader.
473+
*
474+
* @see https://docs.sentry.io/platforms/javascript/configuration/filtering/#using-thirdpartyerrorfilterintegration
475+
*/
476+
applicationKey?: string;
477+
466478
/**
467479
* Options to configure various bundle size optimizations related to the Sentry SDK.
468480
*/
@@ -738,8 +750,10 @@ export type SentryBuildOptions = {
738750
* webpack builds via its `moduleMetadata` / `applicationKey` option.
739751
*
740752
* Requires Next.js 16+
753+
*
754+
* @deprecated Use the top-level `applicationKey` option instead, which works for both webpack and Turbopack builds.
741755
*/
742-
turbopackApplicationKey?: string;
756+
turbopackApplicationKey?: string; // TODO(v11): remove this option
743757
/**
744758
* Options for React component name annotation in Turbopack builds.
745759
* When enabled, JSX elements are annotated with `data-sentry-component`,

packages/nextjs/test/config/getBuildPluginOptions.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ describe('getBuildPluginOptions', () => {
6161
});
6262
});
6363

64+
it('forwards applicationKey to plugin options', () => {
65+
const sentryBuildOptions: SentryBuildOptions = {
66+
applicationKey: 'my-app-key',
67+
};
68+
69+
const result = getBuildPluginOptions({
70+
sentryBuildOptions,
71+
releaseName: mockReleaseName,
72+
distDirAbsPath: mockDistDirAbsPath,
73+
buildTool: 'after-production-compile-webpack',
74+
});
75+
76+
expect(result.applicationKey).toBe('my-app-key');
77+
});
78+
6479
it('normalizes Windows paths to posix for glob patterns in after-production-compile builds', () => {
6580
const windowsPath = 'C:\\Users\\test\\.next';
6681
const sentryBuildOptions: SentryBuildOptions = {

packages/nextjs/test/config/turbopack/constructTurbopackConfig.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,39 @@ describe('moduleMetadataInjection with applicationKey', () => {
977977
});
978978
});
979979

980+
it('should add metadata loader rule when top-level applicationKey is set and Next.js >= 16', () => {
981+
const userNextConfig: NextConfigObject = {};
982+
983+
const result = constructTurbopackConfig({
984+
userNextConfig,
985+
userSentryOptions: { applicationKey: 'my-top-level-key' },
986+
nextJsVersion: '16.0.0',
987+
});
988+
989+
const rule = result.rules!['*.{ts,tsx,js,jsx,mjs,cjs}'] as {
990+
loaders: Array<{ loader: string; options: { applicationKey: string } }>;
991+
};
992+
expect(rule.loaders[0]!.options.applicationKey).toBe('my-top-level-key');
993+
});
994+
995+
it('should prefer top-level applicationKey over deprecated _experimental.turbopackApplicationKey', () => {
996+
const userNextConfig: NextConfigObject = {};
997+
998+
const result = constructTurbopackConfig({
999+
userNextConfig,
1000+
userSentryOptions: {
1001+
applicationKey: 'top-level-key',
1002+
_experimental: { turbopackApplicationKey: 'deprecated-key' },
1003+
},
1004+
nextJsVersion: '16.0.0',
1005+
});
1006+
1007+
const rule = result.rules!['*.{ts,tsx,js,jsx,mjs,cjs}'] as {
1008+
loaders: Array<{ loader: string; options: { applicationKey: string } }>;
1009+
};
1010+
expect(rule.loaders[0]!.options.applicationKey).toBe('top-level-key');
1011+
});
1012+
9801013
it('should only exclude Next.js polyfills, not all foreign modules', () => {
9811014
const userNextConfig: NextConfigObject = {};
9821015

0 commit comments

Comments
 (0)