Skip to content

Commit fbea9d5

Browse files
author
Luca Forstner
committed
ref(nextjs): Remove sentry field in Next.js config as a means of configuration
1 parent afcec2d commit fbea9d5

File tree

3 files changed

+69
-44
lines changed

3 files changed

+69
-44
lines changed

MIGRATION.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,47 @@ Integrations that are now exported from `@sentry/node` and `@sentry/browser` (or
132132
- sessionTimingIntegration
133133
- dedupeIntegration (enabled by default, not pluggable)
134134

135+
## Next.js SDK Changes
136+
137+
### Removal of the `sentry` property in your Next.js options
138+
139+
With version 8 of the Sentry Next.js SDK, the SDK will no longer support passing Next.js options with a `sentry`
140+
property to `withSentryConfig`. Please use the third argument of `withSentryConfig` to configure the SDK instead:
141+
142+
```ts
143+
// OLD
144+
const nextConfig = {
145+
// Your Next.js options...
146+
147+
sentry: {
148+
// Your Sentry SDK options...
149+
},
150+
};
151+
152+
module.exports = withSentryConfig(nextConfig, {
153+
// Your Sentry Webpack Plugin Options...
154+
});
155+
156+
// NEW
157+
const nextConfig = {
158+
// Your Next.js options...
159+
};
160+
161+
module.exports = withSentryConfig(
162+
nextConfig,
163+
{
164+
// Your Sentry Webpack Plugin Options...
165+
},
166+
{
167+
// Your Sentry SDK options...
168+
},
169+
);
170+
```
171+
172+
The reason for this change is to have one consistent way of defining the SDK options. We hope that this change will
173+
reduce confusion when setting up the SDK, with the upside that the explicit option is properly typed and will therefore
174+
have code completion.
175+
135176
# Deprecations in 7.x
136177

137178
You can use the **Experimental** [@sentry/migr8](https://www.npmjs.com/package/@sentry/migr8) to automatically update

packages/nextjs/src/config/types.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,15 @@ import type { DefinePlugin, WebpackPluginInstance } from 'webpack';
44

55
export type SentryWebpackPluginOptions = SentryCliPluginOptions;
66
export type SentryWebpackPlugin = WebpackPluginInstance & { options: SentryWebpackPluginOptions };
7+
78
// Export this from here because importing something from Webpack (the library) in `webpack.ts` confuses the heck out of
89
// madge, which we use for circular dependency checking. We've manually excluded this file from the check (which is
910
// safe, since it only includes types), so we can import it here without causing madge to fail. See
1011
// https://github.com/pahen/madge/issues/306.
1112
export type { WebpackPluginInstance };
1213

13-
/**
14-
* Overall Nextjs config
15-
*/
16-
17-
// The first argument to `withSentryConfig` (which is the user's next config) may contain a `sentry` key, which we'll
18-
// remove once we've captured it, in order to prevent nextjs from throwing warnings. Since it's only in there
19-
// temporarily, we don't include it in the main `NextConfigObject` or `NextConfigFunction` types.
20-
export type ExportedNextConfig = NextConfigObjectWithSentry | NextConfigFunctionWithSentry;
21-
22-
export type NextConfigObjectWithSentry = NextConfigObject & {
23-
sentry?: UserSentryOptions;
24-
};
25-
26-
export type NextConfigFunctionWithSentry = (
27-
phase: string,
28-
defaults: { defaultConfig: NextConfigObject },
29-
) => NextConfigObjectWithSentry | PromiseLike<NextConfigObjectWithSentry>;
14+
// The first argument to `withSentryConfig` (which is the user's next config).
15+
export type ExportedNextConfig = NextConfigObject | NextConfigFunction;
3016

3117
// Vendored from Next.js (this type is not complete - extend if necessary)
3218
type NextRewrite = {

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { isThenable } from '@sentry/utils';
22

33
import type {
4-
ExportedNextConfig,
4+
ExportedNextConfig as NextConfig,
55
NextConfigFunction,
66
NextConfigObject,
7-
NextConfigObjectWithSentry,
87
SentryWebpackPluginOptions,
98
UserSentryOptions,
109
} from './types';
@@ -13,52 +12,51 @@ import { constructWebpackConfigFunction } from './webpack';
1312
let showedExportModeTunnelWarning = false;
1413

1514
/**
16-
* Add Sentry options to the config to be exported from the user's `next.config.js` file.
15+
* Modifies the passed in Next.js configuration
1716
*
18-
* @param exportedUserNextConfig The existing config to be exported prior to adding Sentry
19-
* @param userSentryWebpackPluginOptions Configuration for SentryWebpackPlugin
20-
* @param sentryOptions Optional additional options to add as alternative to `sentry` property of config
17+
* @param nextConfig The existing config to be exported prior to adding Sentry
18+
* @param sentryWebpackPluginOptions Configuration for SentryWebpackPlugin
19+
* @param sentrySDKOptions Optional additional options to add as alternative to `sentry` property of config
2120
* @returns The modified config to be exported
2221
*/
2322
export function withSentryConfig(
24-
exportedUserNextConfig: ExportedNextConfig = {},
25-
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
26-
sentryOptions?: UserSentryOptions,
23+
nextConfig: NextConfig = {},
24+
sentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions> = {},
25+
sentrySDKOptions: UserSentryOptions = {},
2726
): NextConfigFunction | NextConfigObject {
28-
if (typeof exportedUserNextConfig === 'function') {
27+
if (typeof nextConfig === 'function') {
2928
return function (this: unknown, ...webpackConfigFunctionArgs: unknown[]): ReturnType<NextConfigFunction> {
30-
const maybeUserNextConfigObject: NextConfigObjectWithSentry = exportedUserNextConfig.apply(
31-
this,
32-
webpackConfigFunctionArgs,
33-
);
29+
const maybePromiseNextConfig: ReturnType<typeof nextConfig> = nextConfig.apply(this, webpackConfigFunctionArgs);
3430

35-
if (isThenable(maybeUserNextConfigObject)) {
36-
return maybeUserNextConfigObject.then(function (userNextConfigObject: NextConfigObjectWithSentry) {
37-
const userSentryOptions = { ...userNextConfigObject.sentry, ...sentryOptions };
38-
return getFinalConfigObject(userNextConfigObject, userSentryOptions, userSentryWebpackPluginOptions);
31+
if (isThenable(maybePromiseNextConfig)) {
32+
return maybePromiseNextConfig.then(promiseResultNextConfig => {
33+
return getFinalConfigObject(promiseResultNextConfig, sentrySDKOptions, sentryWebpackPluginOptions);
3934
});
4035
}
4136

42-
// Reassign for naming-consistency sake.
43-
const userNextConfigObject = maybeUserNextConfigObject;
44-
const userSentryOptions = { ...userNextConfigObject.sentry, ...sentryOptions };
45-
return getFinalConfigObject(userNextConfigObject, userSentryOptions, userSentryWebpackPluginOptions);
37+
return getFinalConfigObject(maybePromiseNextConfig, sentrySDKOptions, sentryWebpackPluginOptions);
4638
};
4739
} else {
48-
const userSentryOptions = { ...exportedUserNextConfig.sentry, ...sentryOptions };
49-
return getFinalConfigObject(exportedUserNextConfig, userSentryOptions, userSentryWebpackPluginOptions);
40+
return getFinalConfigObject(nextConfig, sentrySDKOptions, sentryWebpackPluginOptions);
5041
}
5142
}
5243

5344
// Modify the materialized object form of the user's next config by deleting the `sentry` property and wrapping the
5445
// `webpack` property
5546
function getFinalConfigObject(
56-
incomingUserNextConfigObject: NextConfigObjectWithSentry,
47+
incomingUserNextConfigObject: NextConfigObject,
5748
userSentryOptions: UserSentryOptions,
5849
userSentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions>,
5950
): NextConfigObject {
60-
// Next 12.2.3+ warns about non-canonical properties on `userNextConfig`.
61-
delete incomingUserNextConfigObject.sentry;
51+
if ('sentry' in incomingUserNextConfigObject) {
52+
// eslint-disable-next-line no-console
53+
console.warn(
54+
'[@sentry/nextjs] Setting a `sentry` property on the Next.js config is no longer supported. Please use the `sentrySDKOptions` argument of `withSentryConfig` instead.',
55+
);
56+
57+
// Next 12.2.3+ warns about non-canonical properties on `userNextConfig`.
58+
delete incomingUserNextConfigObject.sentry;
59+
}
6260

6361
if (userSentryOptions?.tunnelRoute) {
6462
if (incomingUserNextConfigObject.output === 'export') {

0 commit comments

Comments
 (0)