Skip to content

Sentry.captureException/captureMessage break Next.js prerendering with Cache Components enabled #21333

@qnten

Description

@qnten

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.56.0

Framework Version

Next.js 16.2.7

Link to Sentry event

No response

Reproduction Example/SDK Setup

https://github.com/qnten/sentry-cache-components-repro

export default async function Home({ params }: { params: Promise<{ id: string }> }) {
	const { id } = await params;
	if (id === "exception") {
		Sentry.captureException(new Error("Test error"));
		return `Error captured for id exception`;
	}
	if (id === "message") {
		Sentry.captureMessage("Test message");
		return `Message captured for id message`;
	}
	return `Hello, ${id}!`;
}

Steps to Reproduce

  1. Set up @sentry/nextjs in a Next.js 16.2.7 app with cacheComponents: true in next.config.ts.
  2. Create a dynamic route app/[id]/page.tsx with generateStaticParams returning [{ id: "test" }] and conditional Sentry calls for /exception (using captureException) and /message (using captureMessage) as shown above.
  3. Run next dev and visit /exception and /message.
  4. Run next build.

Expected Result

Calling Sentry.captureException or Sentry.captureMessage inside a Server Component should work correctly when the page is (re)generated, for example during build, ISR or on-demand revalidation on Vercel.

Actual Result

next dev

Visiting /exception works fine in the dev server.

Visiting /message breaks with:

[browser] Error: Route "/[id]" used `new Date()` before accessing either uncached data
(e.g. `fetch()`) or Request data (e.g. `cookies()`, `headers()`, `connection()`, and
`searchParams`). Accessing the current time in a Server Component requires reading one of
these data sources first. Alternatively, consider moving this expression into a Client
Component or Cache Component.
See more info here: https://nextjs.org/docs/messages/next-prerender-current-time

    at Module.createEventEnvelopeHeaders
       (node_modules/.pnpm/@sentry+core@10.56.0/node_modules/@sentry/core/src/utils/envelope.ts:258:14)
    at Module.createEventEnvelope
       (node_modules/.pnpm/@sentry+core@10.56.0/node_modules/@sentry/core/src/envelope.ts:106:27)
    at NodeClient.sendEvent
       (node_modules/.pnpm/@sentry+core@10.56.0/node_modules/@sentry/core/src/client.ts:557:15)

next build

Both routes fail to prerender:

Error: Route "/[id]" used `crypto.randomUUID()` before accessing either uncached data
(e.g. `fetch()`) or Request data (e.g. `cookies()`, `headers()`, `connection()`, and
`searchParams`). Accessing random cryptographic values synchronously in a Server Component
requires reading one of these data sources first. Alternatively, consider moving this
expression into a Client Component or Cache Component.
See more info here: https://nextjs.org/docs/messages/next-prerender-crypto

    at d (app/[id]/page.tsx:18:3)
  16 |  const { id } = await params;
  17 |  if (id === "exception") {
> 18 |          Sentry.captureException(new Error("Test error"));
     | ^
  19 |          return `Error captured for id exception`;
  20 |  }

Error occurred prerendering page "/exception". Read more: https://nextjs.org/docs/messages/prerender-error
Export encountered an error on /[id]/page: /exception, exiting the build.
⨯ Next.js build worker exited with code: 1 and signal: null

Deployed to Vercel

When deploying our production app to Vercel, I'm seeing the same new Date() violation at runtime, during page (re)generation:

Error: Route "..." used `new Date()` before accessing either uncached data (e.g. `fetch()`) or Request data (e.g. `cookies()`, `headers()`, `connection()`, and `searchParams`). Accessing the current time in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time
    at n.createSessionEnvelope (.next/server/chunks/_0han8ax._.js:7:1154)
    at c.sendSession (.next/server/chunks/_0han8ax._.js:8:48560)
    at c.captureSession (.next/server/chunks/_0han8ax._.js:8:46952)
    at c._updateSessionFromEvent (.next/server/chunks/_0han8ax._.js:8:49736)
    at <unknown> (.next/server/chunks/_0han8ax._.js:8:52764)

I can't reliably reproduce it in isolation, but it looks like it's the same root cause as the reproduction dev/build errors.

Additional Context

I already posted about this in #17895 (comment). The reply suggested not calling it during prerendering, but that doesn't really help me for the runtime case I observed in our production app (cc @chargome).

Just to reiterate, the next build failure is a secondary concern for me because it only happens in the reproduction, but it seems to be caused by the same underlying incompatibility, so fixing one will likely fix the other.

Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Metadata

Metadata

Assignees

No fields configured for issues without a type.

Projects

Status
Waiting for: Product Owner

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions