diff --git a/packages/nextjs/src/config/util.ts b/packages/nextjs/src/config/util.ts new file mode 100644 index 000000000000..a88e68a57135 --- /dev/null +++ b/packages/nextjs/src/config/util.ts @@ -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; + } +} diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index 6c691424f115..5ff25c31e7ea 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -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'; @@ -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. @@ -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 }; @@ -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'], diff --git a/packages/nextjs/src/config/withSentryConfig.ts b/packages/nextjs/src/config/withSentryConfig.ts index ab15d1c86c0d..e811697cbe86 100644 --- a/packages/nextjs/src/config/withSentryConfig.ts +++ b/packages/nextjs/src/config/withSentryConfig.ts @@ -2,9 +2,8 @@ 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, @@ -12,6 +11,7 @@ import type { SentryBuildOptions, } from './types'; import { constructWebpackConfigFunction } from './webpack'; +import { getNextjsVersion } from './util'; let showedExportModeTunnelWarning = false; @@ -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 {