Menu

Monitoring, Logging, and Upgrading

Lesson 4: Monitoring, Logging, and Upgrading

What should you watch in production?

Signal Tool Question it answers
Errors instrumentation.ts + Sentry or similar What is failing, and for whom?
Performance Web Vitals, Speed Insights, APM tools Which pages are slow?
Logs Platform logs, structured JSON logs What happened before the failure?
Traces OpenTelemetry Which step in a request took longest?
Uptime External monitor Is the site reachable?

Example: instrumentation.ts

// instrumentation.ts (project root)
export async function register() {
  // Runs once when the server starts: set up OpenTelemetry or an error SDK
  if (process.env.NEXT_RUNTIME === 'nodejs') {
    await import('./monitoring/server');
  }
}

export async function onRequestError(
  error: { digest?: string } & Error,
  request: { path: string; method: string },
  context: { routerKind: string; routePath: string; routeType: string },
) {
  await reportError({
    message: error.message,
    digest: error.digest,
    path: request.path,
    route: context.routePath,
  });
}

Example: work after the response with after()

import { after } from 'next/server';

export async function enrol(prev: State, formData: FormData) {
  const enrolment = await saveEnrolment(formData);
  after(async () => {
    await sendWelcomeEmail(enrolment.email); // Runs after the user gets a response
    await trackEvent('enrolled', { courseId: enrolment.courseId });
  });
  redirect('/dashboard');
}

Upgrading safely

Step Command or action
Read the release notes Next.js blog and the version upgrade guide
Run the automated upgrade npx @next/codemod@canary upgrade latest
Run specific codemods e.g. npx @next/codemod@latest middleware-to-proxy .
Test Lint, unit tests, E2E tests, production build
Deploy to preview first Compare behaviour before production

Next.js 16.x is the active long-term-support line, and the team publishes scheduled security releases. Apply security patches promptly, even if you delay feature upgrades.

How it works

  • register() runs once per server start; onRequestError runs for every unhandled server error, with the route that failed.
  • after() keeps slow side effects such as emails and analytics out of the user's wait time.
  • Codemods rewrite common breaking changes automatically, but you still need to test.

Common mistakes

Mistake Effect Fix
No error reporting Users find bugs before you do Report from onRequestError and error boundaries
Sending emails before responding Slow form submissions Move them into after()
Skipping several major versions Large, risky migration Upgrade one major version at a time
Upgrading straight to production Outage from an unnoticed breaking change Test on a preview deployment first

Key takeaways

  • Monitor errors, performance, and logs from day one.
  • Use after() for non-urgent work and instrumentation.ts for observability.
  • Upgrade regularly with codemods, tests, and previews.

Try it: Add instrumentation.ts with an onRequestError handler that logs the route and digest, then trigger an error and find it in your logs.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details