8000 feat(nextjs): Future-proof Next.js config options overriding (#13586) · billyjanitsch/sentry-javascript@c4f68d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4f68d8

Browse files
authored
feat(nextjs): Future-proof Next.js config options overriding (getsentry#13586)
The logic in this PR is slightly intense but all it does is check for the Next.js version and if we are on a version that included [this change](vercel/next.js#68853) which stabilized `instrumentation.ts` we no longer inject the `experimental.instrumentationHook` option in `next.config.js`. This is necessary because otherwise Next.js prints scary warnings and users may be confused why.
1 parent 63863cd commit c4f68d8

File tree

1 file changed

+70
-13
lines changed

1 file changed

+70
-13
lines changed

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable complexity */
12
import { isThenable, parseSemver } from '@sentry/utils';
23

34
import * as fs from 'fs';
@@ -47,6 +48,7 @@ function getFinalConfigObject(
4748
incomingUserNextConfigObject: NextConfigObject,
4849
userSentryOptions: SentryBuildOptions,
4950
): NextConfigObject {
51+
// TODO(v9): Remove this check for the Sentry property
5052
if ('sentry' in incomingUserNextConfigObject) {
5153
// eslint-disable-next-line no-console
5254
console.warn(
@@ -71,21 +73,10 @@ function getFinalConfigObject(
7173
}
7274
}
7375

74-
// We need to enable `instrumentation.ts` for users because we tell them to put their `Sentry.init()` calls inside of it.
75-
if (incomingUserNextConfigObject.experimental?.instrumentationHook === false) {
76-
// eslint-disable-next-line no-console
77-
console.warn(
78-
'[@sentry/nextjs] You turned off the `instrumentationHook` option. Note that Sentry will not be initialized if you did not set it up inside `instrumentation.ts`.',
79-
);
80-
}
81-
incomingUserNextConfigObject.experimental = {
82-
instrumentationHook: true,
83-
...incomingUserNextConfigObject.experimental,
84-
};
76+
const nextJsVersion = getNextjsVersion();
8577

8678
// Add the `clientTraceMetadata` experimental option based on Next.js version. The option got introduced in Next.js version 15.0.0 (actually 14.3.0-canary.64).
8779
// Adding the option on lower versions will cause Next.js to print nasty warnings we wouldn't confront our users with.
88-
const nextJsVersion = getNextjsVersion();
8980
if (nextJsVersion) {
9081
const { major, minor } = parseSemver(nextJsVersion);
9182
if (major !== undefined && minor !== undefined && (major >= 15 || (major === 14 && minor >= 3))) {
@@ -99,10 +90,76 @@ function getFinalConfigObject(
9990
} else {
10091
// eslint-disable-next-line no-console
10192
console.log(
102-
"[@sentry/nextjs] The Sentry SDK was not able to determine your Next.js version. If you are using Next.js 15 or greater, please add `experimental.clientTraceMetadata: ['sentry-trace', 'baggage']` to your Next.js config to enable pageload tracing for App Router.",
93+
"[@sentry/nextjs] The Sentry SDK was not able to determine your Next.js version. If you are using Next.js version 15 or greater, please add `experimental.clientTraceMetadata: ['sentry-trace', 'baggage']` to your Next.js config to enable pageload tracing for App Router.",
10394
);
10495
}
10596

97+
// From Next.js version (15.0.0-canary.124) onwards, Next.js does no longer require the `experimental.instrumentationHook` option and will
98+
// print a warning when it is set, so we need to conditionally provide it for lower versions.
99+
if (nextJsVersion) {
100+
const { major, minor, patch, prerelease } = parseSemver(nextJsVersion);
101+
const isFullySupportedRelease =
102+
major !== undefined &&
103+
minor !== undefined &&
104+
patch !== undefined &&
105+
major >= 15 &&
106+
((minor === 0 && patch === 0 && prerelease === undefined) || minor > 0 || patch > 0);
107+
const isSupportedV15Rc =
108+
major !== undefined &&
109+
minor !== undefined &&
110+
patch !== undefined &&
111+
prerelease !== undefined &&
112+
major === 15 &&
113+
minor === 0 &&
114+
patch === 0 &&
115+
prerelease.startsWith('rc.') &&
116+
parseInt(prerelease.split('.')[1] || '', 10) > 0;
117+
const isSupportedCanary =
118+
minor !== undefined &&
119+
patch !== undefined &&
120+
prerelease !== undefined &&
121+
major === 15 &&
122+
minor === 0 &&
123+
patch === 0 &&
124+
prerelease.startsWith('canary.') &&
125+
parseInt(prerelease.split('.')[1] || '', 10) >= 124;
126+
127+
if (!isFullySupportedRelease && !isSupportedV15Rc && !isSupportedCanary) {
128+
if (incomingUserNextConfigObject.experimental?.instrumentationHook === false) {
129+
// eslint-disable-next-line no-console
130+
console.warn(
131+
'[@sentry/nextjs] You turned off the `experimental.instrumentationHook` option. Note that Sentry will not be initialized if you did not set it up inside `instrumentation.(js|ts)`.',
132+
);
133+
}
134+
incomingUserNextConfigObject.experimental = {
135+
instrumentationHook: true,
136+
...incomingUserNextConfigObject.experimental,
137+
};
138+
}
139+
} else {
140+
// If we cannot detect a Next.js version for whatever reason, the sensible default is to set the `experimental.instrumentationHook`, even though it may create a warning.
141+
if (
142+
incomingUserNextConfigObject.experimental &&
143+
'instrumentationHook' in incomingUserNextConfigObject.experimental
144+
) {
145+
if (incomingUserNextConfigObject.experimental.instrumentationHook === false) {
146+
// eslint-disable-next-line no-console
147+
console.warn(
148+
'[@sentry/nextjs] You set `experimental.instrumentationHook` to `false`. If you are using Next.js version 15 or greater, you can remove that option. If you are using Next.js version 14 or lower, you need to set `experimental.instrumentationHook` in your `next.config.(js|mjs)` to `true` for the SDK to be properly initialized in combination with `instrumentation.(js|ts)`.',
149+
);
150+
}
151+
} else {
152+
// eslint-disable-next-line no-console
153+
console.log(
154+
"[@sentry/nextjs] The Sentry SDK was not able to determine your Next.js version. If you are using Next.js version 15 or greater, Next.js will probably show you a warning about the `experimental.instrumentationHook` being set. To silence Next.js' warning, explicitly set the `experimental.instrumentationHook` option in your `next.config.(js|mjs|ts)` to `undefined`. If you are on Next.js version 14 or lower, you can silence this particular warning by explicitly setting the `experimental.instrumentationHook` option in your `next.config.(js|mjs)` to `true`.",
155+
);
156+
incomingUserNextConfigObject.experimental = {
157+
instrumentationHook: true,
158+
...incomingUserNextConfigObject.experimental,
159+
};
160+
}
161+
}
162+
106163
if (process.env.TURBOPACK && !process.env.SENTRY_SUPPRESS_TURBOPACK_WARNING) {
107164
// eslint-disable-next-line no-console
108165
console.warn(

0 commit comments

Comments
 (0)
0