Skip to content

fix(nextjs): Only warn on missing onRequestError in version 15 #15553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions packages/nextjs/src/config/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as fs from 'fs';
import { sync as resolveSync } from 'resolve';

/**
* Returns the version of Next.js installed in the project, or undefined if it cannot be determined.
*/
export function getNextjsVersion(): string | undefined {
const nextjsPackageJsonPath = resolveNextjsPackageJson();
if (nextjsPackageJsonPath) {
try {
const nextjsPackageJson: { version: string } = JSON.parse(
fs.readFileSync(nextjsPackageJsonPath, { encoding: 'utf-8' }),
);
return nextjsPackageJson.version;
} catch {
// noop
}
}

return undefined;
}

function resolveNextjsPackageJson(): string | undefined {
try {
return resolveSync('next/package.json', { basedir: process.cwd() });
} catch {
return undefined;
}
}
12 changes: 9 additions & 3 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import * as fs from 'fs';
import * as path from 'path';
import { escapeStringForRegex, loadModule, logger } from '@sentry/core';
import { escapeStringForRegex, loadModule, logger, parseSemver } from '@sentry/core';
import * as chalk from 'chalk';
import { sync as resolveSync } from 'resolve';

Expand All @@ -22,6 +22,7 @@ import type {
WebpackEntryProperty,
} from './types';
import { getWebpackPluginOptions } from './webpackPluginOptions';
import { getNextjsVersion } from './util';

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

let rawNewConfig = { ...incomingConfig };
Expand Down Expand Up @@ -443,7 +449,7 @@ async function addSentryToClientEntryProperty(
*
* @param projectDir The root directory of the project, where config files would be located
*/
function warnAboutMissingonRequestErrorHandler(projectDir: string): void {
function warnAboutMissingOnRequestErrorHandler(projectDir: string): void {
const instrumentationPaths = [
['src', 'instrumentation.ts'],
['src', 'instrumentation.js'],
Expand Down
28 changes: 2 additions & 26 deletions packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
import { isThenable, parseSemver } from '@sentry/core';

import * as childProcess from 'child_process';
import * as fs from 'fs';
import { getSentryRelease } from '@sentry/node';
import { sync as resolveSync } from 'resolve';

import type {
ExportedNextConfig as NextConfig,
NextConfigFunction,
NextConfigObject,
SentryBuildOptions,
} from './types';
import { constructWebpackConfigFunction } from './webpack';
import { getNextjsVersion } from './util';

let showedExportModeTunnelWarning = false;

Expand Down Expand Up @@ -299,30 +299,6 @@ function setUpBuildTimeVariables(userNextConfig: NextConfigObject, userSentryOpt
}
}

function getNextjsVersion(): string | undefined {
const nextjsPackageJsonPath = resolveNextjsPackageJson();
if (nextjsPackageJsonPath) {
try {
const nextjsPackageJson: { version: string } = JSON.parse(
fs.readFileSync(nextjsPackageJsonPath, { encoding: 'utf-8' }),
);
return nextjsPackageJson.version;
} catch {
// noop
}
}

return undefined;
}

function resolveNextjsPackageJson(): string | undefined {
try {
return resolveSync('next/package.json', { basedir: process.cwd() });
} catch {
return undefined;
}
}

function getGitRevision(): string | undefined {
let gitRevision: string | undefined;
try {
Expand Down
Loading