โ† All posts
RELEASE September 29, 2026 ยท 3 min read

Next.js: wrap the route, not the request

SR
Sentrinel Team
Product & engineering

If you installed Sentrinel's Next.js middleware from our instructions, remove it. It did worse than record nothing: it replaced every page it matched with an empty response. We are sorry. Here is what happened, and what to use instead.

What went wrong

Next.js middleware is a function that runs before a route. It can rewrite, redirect, or let the request continue โ€” and "continue" is not the same as returning a response. Our middleware returned new Response(null, { status: 200 }), intending it as "carry on". Next treats a returned response as the response. Every route the matcher covered โ€” and the snippet we published matched almost everything โ€” served an empty 200.

It was also the wrong place for monitoring regardless. Middleware runs on the Edge runtime, before the route, so it can never see the status your route returned or the error it threw; the collector it tried to start needed Node APIs the Edge runtime does not have; and the function meant to record the response was never called by anything. And for identity it fell back to the x-api-key header โ€” an end user's API key, stored as their consumer id.

The old middleware export is now a pass-through that records nothing, kept only so an existing middleware.ts does not break the build. Delete it when you switch.

Wrap the route handlers instead

// lib/sentrinel.ts
import { createSentrinelNext } from "@sentrinel/plugin/next";

export const { withSentrinel } = createSentrinelNext({
  serverUrl: process.env.SENTRINEL_URL!,
  appName: "storefront",
  env: process.env.VERCEL_ENV ?? "prod",
  apiKey: process.env.SENTRINEL_API_KEY,
});
// app/api/orders/[id]/route.ts
import { withSentrinel } from "@/lib/sentrinel";

export const GET = withSentrinel(async (request: Request, { params }) => {
  const { id } = await params;
  return Response.json(await getOrder(id));
});

The same function wraps a Pages Router API route. Because it sits around the handler rather than in front of it, it records what actually happened: the status the route returned, the time it took, the error it threw with its stack. The handler runs inside the request's trace, so spans and structured logs inside it are nested and correlated.

The endpoint is the route's pattern โ€” /api/orders/[id] โ€” rebuilt from params, so every order id is one row. redirect() and notFound(), which Next implements by throwing, are recorded with their status rather than as errors.

What this does not cover

Route handlers must run on the Node runtime, which is the default. Server Components and pages are not wrapped โ€” they render; they are not API calls โ€” and for what the browser experiences on those pages, the browser SDK is the right tool, sending through a route handler of your own so no key ever reaches the page.

Installing it

The plugin now ships built JavaScript, so Next can import it without transpiling a dependency:

npm install "github:Zaga-ltd/sentinel_packages"

Credentials in headers, query strings and bodies are masked by default before anything leaves your server. The plugin reference has the full option list.

The lesson we took from this one: a monitoring integration should be tested inside the framework it claims to support, doing the thing a user does โ€” not in isolation, with a function called by hand. The Next.js tests now build a route and send it a request.