Skip to content

Commit 2049ade

Browse files
authored
fix(nextjs): Only warn on missing onRequestError in version 15 (#15553)
1 parent c6760c4 commit 2049ade

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

packages/nextjs/src/config/util.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as fs from 'fs';
2+
import { sync as resolveSync } from 'resolve';
3+
4+
/**
5+
* Returns the version of Next.js installed in the project, or undefined if it cannot be determined.
6+
*/
7+
export function getNextjsVersion(): string | undefined {
8+
const nextjsPackageJsonPath = resolveNextjsPackageJson();
9+
if (nextjsPackageJsonPath) {
10+
try {
11+
const nextjsPackageJson: { version: string } = JSON.parse(
12+
fs.readFileSync(nextjsPackageJsonPath, { encoding: 'utf-8' }),
13+
);
14+
return nextjsPackageJson.version;
15+
} catch {
16+
// noop
17+
}
18+
}
19+
20+
return undefined;
21+
}
22+
23+
function resolveNextjsPackageJson(): string | undefined {
24+
try {
25+
return resolveSync('next/package.json', { basedir: process.cwd() });
26+
} catch {
27+
return undefined;
28+
}
29+
}

packages/nextjs/src/config/webpack.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import * as fs from 'fs';
55
import * as path from 'path';
6-
import { escapeStringForRegex, loadModule, logger } from '@sentry/core';
6+
import { escapeStringForRegex, loadModule, logger, parseSemver } from '@sentry/core';
77
import * as chalk from 'chalk';
88
import { sync as resolveSync } from 'resolve';
99

@@ -22,6 +22,7 @@ import type {
2222
WebpackEntryProperty,
2323
} from './types';
2424
import { getWebpackPluginOptions } from './webpackPluginOptions';
25+
import { getNextjsVersion } from './util';
2526

2627
// Next.js runs webpack 3 times, once for the client, the server, and for edge. Because we don't want to print certain
2728
// warnings 3 times, we keep track of them here.
@@ -58,7 +59,12 @@ export function constructWebpackConfigFunction(
5859
warnAboutDeprecatedConfigFiles(projectDir, runtime);
5960
}
6061
if (runtime === 'server') {
61-
warnAboutMissingonRequestErrorHandler(projectDir);
62+
const nextJsVersion = getNextjsVersion();
63+
const { major } = parseSemver(nextJsVersion || '');
64+
// was added in v15 (https://github.com/vercel/next.js/pull/67539)
65+
if (major && major >= 15) {
66+
warnAboutMissingOnRequestErrorHandler(projectDir);
67+
}
6268
}
6369

6470
let rawNewConfig = { ...incomingConfig };
@@ -443,7 +449,7 @@ async function addSentryToClientEntryProperty(
443449
*
444450
* @param projectDir The root directory of the project, where config files would be located
445451
*/
446-
function warnAboutMissingonRequestErrorHandler(projectDir: string): void {
452+
function warnAboutMissingOnRequestErrorHandler(projectDir: string): void {
447453
const instrumentationPaths = [
448454
['src', 'instrumentation.ts'],
449455
['src', 'instrumentation.js'],

packages/nextjs/src/config/withSentryConfig.ts

+2-26
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
import { isThenable, parseSemver } from '@sentry/core';
33

44
import * as childProcess from 'child_process';
5-
import * as fs from 'fs';
65
import { getSentryRelease } from '@sentry/node';
7-
import { sync as resolveSync } from 'resolve';
6+
87
import type {
98
ExportedNextConfig as NextConfig,
109
NextConfigFunction,
1110
NextConfigObject,
1211
SentryBuildOptions,
1312
} from './types';
1413
import { constructWebpackConfigFunction } from './webpack';
14+
import { getNextjsVersion } from './util';
1515

1616
let showedExportModeTunnelWarning = false;
1717

@@ -299,30 +299,6 @@ function setUpBuildTimeVariables(userNextConfig: NextConfigObject, userSentryOpt
299299
}
300300
}
301301

302-
function getNextjsVersion(): string | undefined {
303-
const nextjsPackageJsonPath = resolveNextjsPackageJson();
304-
if (nextjsPackageJsonPath) {
305-
try {
306-
const nextjsPackageJson: { version: string } = JSON.parse(
307-
fs.readFileSync(nextjsPackageJsonPath, { encoding: 'utf-8' }),
308-
);
309-
return nextjsPackageJson.version;
310-
} catch {
311-
// noop
312-
}
313-
}
314-
315-
return undefined;
316-
}
317-
318-
function resolveNextjsPackageJson(): string | undefined {
319-
try {
320-
return resolveSync('next/package.json', { basedir: process.cwd() });
321-
} catch {
322-
return undefined;
323-
}
324-
}
325-
326302
function getGitRevision(): string | undefined {
327303
let gitRevision: string | undefined;
328304
try {

0 commit comments

Comments
 (0)