Skip to content

captureException during next build prerender fails cacheComponents routes (safe-random runner never registered in build workers) #21510

Description

@pnodet

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

10.57.0

Framework Version

Next.js 16.2.9 (cacheComponents: true)

Link to Sentry event

No response

Reproduction Example/SDK Setup

https://github.com/pnodet/next-prerender-random-repro — pnpm build fails on app/capture-metadata/page.tsx:

export const generateMetadata = (): Metadata => {
  // real-world pattern: report a recoverable data problem, return fallback metadata
  captureException(new Error('diagnostic: data missing for this page'));
  return { title: 'capture-metadata' };
};

Steps to Reproduce

  1. pnpm install && pnpm build

Expected Result

The route prerenders; the event is dropped (no client at build) or buffered — either way the build succeeds. This is the same class as #21333, which was fixed for Date.now()/new Date() in v10.57.0 (#21351), but the crypto.randomUUID() path still fails at build time.

Root cause: uuid4() is correctly wrapped in withRandomSafeContext, but the runner under Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__') is only registered by prepareSafeIdGeneratorContext() inside init() — and next build prerender workers never load instrumentation.ts, so no runner exists there and crypto.randomUUID() runs unwrapped inside the prerender scope. (Observable in the repro: a tracer stashed on globalThis by register() is undefined during build prerendering.)

A possible fix: resolve the runner lazily per call instead of caching RESOLVED_RUNNER forever, and/or fall back to a fresh AsyncLocalStorage.snapshot() taken at @sentry/core module scope when no runner was registered.

Actual Result

Error: Route "/capture-metadata" used `crypto.randomUUID()` before accessing either uncached data (e.g. `fetch()`) or Request data (...). See more info here: https://nextjs.org/docs/messages/next-prerender-crypto
Error occurred prerendering page "/capture-metadata".
> Build error occurred

Also related (runtime side, filed against Next.js since the bug is in their tracer patch, but the trigger is @sentry/node's integrations): vercel/next.js#94753 — integration tracers are acquired in instrumentation constructors before initOpenTelemetry() registers the provider, so they are ProxyTracers whose getDelegateTracer() resolution bypasses Next's #82350 patch; OTel RandomIdGenerator Math.random() then fails every ISR revalidation of PPR routes that touch an instrumented resource (pg, undici, …). Sentry reports those as message-less Error: null (StaticGenBailoutError). You may want to track it, since it hits every @sentry/nextjs + cacheComponents app with a DB query in a prerendered shell — a defensive option on the SDK side would be re-acquiring instrumentation tracers after provider registration, or routing OTel id generation through the safe-random context.

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