Skip to content

Next.js proxy.ts/middleware not instrumented under Turbopack builds (metrics/logs/init dropped in prod) #21713

Description

@sergical

SDK

@sentry/nextjs 10.59.0 · Next.js 16.2.9 · Turbopack production build on Vercel · Sentry SaaS

What happens

Sentry.metrics.count() and Sentry.logger.info() (and any capture) called from proxy.ts (the renamed middleware.ts) are never delivered in production. Client-side traces work fine. It works in next dev only because the proxy shares the dev-server process where instrumentation.ts already ran Sentry.init.

In Next.js 16 the proxy runs as its own Node serverless function, separate from the main server, and Sentry is never initialized in that isolate. Vercel runtime logs show the proxy executing (source: serverless-middleware, 200) with an empty logs: []; querying Sentry shows zero vercel-production proxy metrics/logs while client vercel-production traces arrive.

Cause

Under Turbopack, the SDK only injects loaders into instrumentation.* and instrumentation-client.* (packages/nextjs/src/config/turbopack/generateValueInjectionRules.ts). The middleware/proxy auto-instrumentation exists only in the webpack path (packages/nextjs/src/config/webpack.ts:267-282, which already matches both middleware.* and proxy.*). So under a Turbopack build the proxy is neither wrapped nor initialized.

Already flagged in-code: packages/nextjs/src/common/wrapMiddlewareWithSentry.ts:55 — "We still should add central isolation scope creation for when our build-time instrumentation does not work anymore with turbopack."

Note that wrapping alone isn't sufficient here — the gap is Sentry.init not reaching the proxy's Node runtime, not just the missing span wrapper.

Minimal reproduction

https://github.com/getsentry/nextjs-proxy-sentry-api — proxy.ts emits a page.view counter; metric is absent in prod, present in dev.

Workaround

Add import "./sentry.server.config"; at the top of proxy.ts to run Sentry.init in the proxy runtime; the existing event.waitUntil(Sentry.flush()) then delivers. Verified: metrics/logs appear tagged environment: vercel-production.

Expected

@sentry/nextjs instruments proxy.ts/middleware.ts under Turbopack builds so emissions from the proxy are delivered in production without a manual config import.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions