// SPDX-License-Identifier: LicenseRef-ConusAI-Template-1.0 // Next.js runs `register` once when a server instance starts, before it takes a request, and // `onRequestError` for every error a render, a route or an action leaves unhandled (spec ยง9.5). // // `register` parses the deployment contract and nothing else: a missing or forbidden platform // variable stops the server here, with a typed error that names it, before the first request. It // builds nothing. Next.js bundles this file apart from the routes, and a registry or an error client // built in this bundle would be a second copy of every contract the routes then fail to match // ("procedure ... is not registered with this contract"); the runtime, observability included, is // built on the first request by the routes' own copy (src/server/app.ts), and shared with this file only as // the observability handle, which holds no kit module (config/src/observability.ts). import type { Instrumentation } from "next" export async function register(): Promise { if (process.env.NEXT_RUNTIME !== "nodejs") return const { loadAppEnv } = await import("@app/config") loadAppEnv("web") } export const onRequestError: Instrumentation.onRequestError = async (error, request, context) => { if (process.env.NEXT_RUNTIME !== "nodejs") return const [{ currentObservability }, { reportRequestError }] = await Promise.all([ import("@app/config"), import("./src/server/report"), ]) // Nothing has been started when no request has reached the routes yet; there is nothing to report through. const observe = currentObservability() if (observe) reportRequestError(observe, error, request, context) }