Skip to content

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

Closed

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

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jun 23, 2026
  2. linear-code commented on Jun 23, 2026

    @linear-code
  3. self-assigned this
    on Jun 24, 2026
  4. JPeer264 commented on Jun 24, 2026

    @JPeer264
    Member

    confirmed. It works locally just fine while it doesn't when deployed.

    Local trace: https://sentry-sdks.sentry.io/explore/traces/trace/0f24de98cc184d7f954069f4bc6ccb5d
    vs. deployed trace: https://sentry-sdks.sentry.io/explore/traces/trace/82d79e4999364d02a8aa0a36efc2e6a4

    I tried to have fix it, but failed. I would leave this to our NextJS gurus

  5. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 24, 2026
  6. chargome commented on Jul 6, 2026

    @chargome
    Member
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