diff --git a/packages/next/src/server/base-server.ts b/packages/next/src/server/base-server.ts index d8601e571aa94..40afd0988804c 100644 --- a/packages/next/src/server/base-server.ts +++ b/packages/next/src/server/base-server.ts @@ -836,10 +836,7 @@ export default abstract class Server< ) { const [err, req, ctx] = args - if ( - process.env.__NEXT_EXPERIMENTAL_INSTRUMENTATION && - this.instrumentation - ) { + if (this.instrumentation) { try { await this.instrumentation.onRequestError?.( err, diff --git a/packages/next/src/server/instrumentation/types.ts b/packages/next/src/server/instrumentation/types.ts index 55ee90632932c..1c260cf79fb4e 100644 --- a/packages/next/src/server/instrumentation/types.ts +++ b/packages/next/src/server/instrumentation/types.ts @@ -23,3 +23,7 @@ export type InstrumentationModule = { register?(): void onRequestError?: InstrumentationOnRequestError } + +export namespace Instrumentation { + export type onRequestError = InstrumentationOnRequestError +} diff --git a/packages/next/src/server/web/globals.ts b/packages/next/src/server/web/globals.ts index 5e5ce7c8e56cc..6c4bf22ef2e8c 100644 --- a/packages/next/src/server/web/globals.ts +++ b/packages/next/src/server/web/globals.ts @@ -37,12 +37,7 @@ export async function edgeInstrumentationOnRequestError( ) { const instrumentation = await getEdgeInstrumentationModule() try { - if ( - process.env.__NEXT_EXPERIMENTAL_INSTRUMENTATION && - instrumentation?.onRequestError - ) { - await instrumentation.onRequestError(...args) - } + await instrumentation?.onRequestError?.(...args) } catch (err) { // Log the soft error and continue, since the original error has already been thrown console.error('Error in instrumentation.onRequestError:', err) diff --git a/packages/next/src/types.ts b/packages/next/src/types.ts index fcb4c47b73cef..396abc74fdb73 100644 --- a/packages/next/src/types.ts +++ b/packages/next/src/types.ts @@ -38,6 +38,8 @@ export type { ResolvedViewport, } from './lib/metadata/types/metadata-interface' +export type { Instrumentation } from './server/instrumentation/types' + /** * Stub route type for typedRoutes before `next dev` or `next build` is run * @link https://nextjs.org/docs/app/building-your-application/configuring/typescript#statically-typed-links diff --git a/test/e2e/on-request-error/basic/basic.test.ts b/test/e2e/on-request-error/basic/basic.test.ts index 3e018fbf84037..0af57ce7077a9 100644 --- a/test/e2e/on-request-error/basic/basic.test.ts +++ b/test/e2e/on-request-error/basic/basic.test.ts @@ -6,9 +6,6 @@ describe('on-request-error - basic', () => { const { next, skipped } = nextTestSetup({ files: __dirname, skipDeployment: true, - env: { - __NEXT_EXPERIMENTAL_INSTRUMENTATION: '1', - }, }) if (skipped) { diff --git a/test/e2e/on-request-error/basic/instrumentation.js b/test/e2e/on-request-error/basic/instrumentation.ts similarity index 54% rename from test/e2e/on-request-error/basic/instrumentation.js rename to test/e2e/on-request-error/basic/instrumentation.ts index b9944b351236c..6310bffac27fc 100644 --- a/test/e2e/on-request-error/basic/instrumentation.js +++ b/test/e2e/on-request-error/basic/instrumentation.ts @@ -1,8 +1,14 @@ -export function onRequestError(err, request, context) { +import { type Instrumentation } from 'next' + +export const onRequestError: Instrumentation.onRequestError = ( + err, + request, + context +) => { fetch(`http://localhost:${process.env.PORT}/write-log`, { method: 'POST', body: JSON.stringify({ - message: err.message, + message: (err as Error).message, request, context, }), diff --git a/test/e2e/on-request-error/dynamic-routes/dynamic-routes.test.ts b/test/e2e/on-request-error/dynamic-routes/dynamic-routes.test.ts index d2f0e98a02379..a4983de82ea10 100644 --- a/test/e2e/on-request-error/dynamic-routes/dynamic-routes.test.ts +++ b/test/e2e/on-request-error/dynamic-routes/dynamic-routes.test.ts @@ -6,9 +6,6 @@ describe('on-request-error - dynamic-routes', () => { const { next, skipped } = nextTestSetup({ files: __dirname, skipDeployment: true, - env: { - __NEXT_EXPERIMENTAL_INSTRUMENTATION: '1', - }, }) if (skipped) { diff --git a/test/e2e/on-request-error/server-action-error/server-action-error.test.ts b/test/e2e/on-request-error/server-action-error/server-action-error.test.ts index b092494fcfc7a..33f45afa9e652 100644 --- a/test/e2e/on-request-error/server-action-error/server-action-error.test.ts +++ b/test/e2e/on-request-error/server-action-error/server-action-error.test.ts @@ -6,9 +6,6 @@ describe('on-request-error - server-action-error', () => { const { next, skipped } = nextTestSetup({ files: __dirname, skipDeployment: true, - env: { - __NEXT_EXPERIMENTAL_INSTRUMENTATION: '1', - }, }) if (skipped) { diff --git a/test/e2e/on-request-error/skip-next-internal-error/skip-next-internal-error.test.ts b/test/e2e/on-request-error/skip-next-internal-error/skip-next-internal-error.test.ts index de8016c020e39..b34f2e4a1c7c5 100644 --- a/test/e2e/on-request-error/skip-next-internal-error/skip-next-internal-error.test.ts +++ b/test/e2e/on-request-error/skip-next-internal-error/skip-next-internal-error.test.ts @@ -4,9 +4,6 @@ describe('on-request-error - skip-next-internal-error', () => { const { next, skipped } = nextTestSetup({ files: __dirname, skipDeployment: true, - env: { - __NEXT_EXPERIMENTAL_INSTRUMENTATION: '1', - }, }) if (skipped) {