Skip to content

feat(nextjs): Send the trace in the Server-Timing header of document responses - #26107

Open
chargome wants to merge 1 commit into
chargome/ref/remix-core-server-timing-utilsfrom
chargome/feat/nextjs-server-timing-header
Open

chargome wants to merge 1 commit into
chargome/ref/remix-core-server-timing-utilsfrom
chargome/feat/nextjs-server-timing-header

Conversation

@chargome

@chargome chargome commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Under cacheComponents the sentry-trace meta tag arrives with the resumed part of the document, usually after the browser SDK started, so the pageload has to wait for it (#25142). A Server-Timing header reaches the browser before any HTML. The browser SDK already reads it, and Remix and Nitro already send it. On a self-hosted server Next.js flushes the headers with the first chunk, inside the request span, so the header is set in writeHead rather than when the request comes in.

Before, with the meta tag only:

sequenceDiagram
    participant B as Browser
    participant S as Next.js server
    B->>S: GET /page
    Note over S: request span starts, shell comes from cache
    S-->>B: headers + shell (no trace context)
    Note over B: SDK starts, pageload has to wait
    Note over S: dynamic part renders
    S-->>B: resumed part with sentry-trace meta tag
    Note over B: pageload starts late, fetches and errors before it are unattached
Loading

After, with the header:

sequenceDiagram
    participant B as Browser
    participant S as Next.js server
    B->>S: GET /page
    Note over S: request span starts, shell comes from cache
    Note over S: writeHead on the first chunk: Server-Timing carries the active trace
    S-->>B: headers + shell (Server-Timing: sentry-trace, baggage)
    Note over B: SDK starts, pageload continues the trace at once
    Note over S: dynamic part renders
    S-->>B: resumed part with sentry-trace meta tag
    Note over B: tag only settles page.server_rendering.type
Loading

The header is left off responses a shared cache may store, which is what Next.js sends for static and ISR app router documents. A CDN in front of the server would otherwise replay one visitor's trace and sampling decision to everyone. Non-document responses get no header either.

Since the pageload no longer waits for the tag, page.server_rendering.type is settled after span start for a runtime shell with a dynamic part, so tracesSampler does not see it in that case.

Closes #24945
Refs JS-3864

@chargome chargome self-assigned this Nov 4, 2026
@chargome

chargome commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@linear-code

linear-code Bot commented Nov 4, 2026

Copy link
Copy Markdown

JS-3864

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: fc3667f (PR test merge using chargome/ref/remix-core-server-timing-utils at abd84dd)

Against: f49009b (chargome/ref/remix-core-server-timing-utils) · base run #65866

⚠️ Baseline mismatch: 3 chargome/ref/remix-core-server-timing-utils commit(s) in the PR build are missing from the baseline. Their size changes also affect this comparison. Compare commits. Re-run once chargome/ref/remix-core-server-timing-utils's baseline build for abd84dd finishes.

Path Size % Change Change
@sentry/browser 30.04 kB - -
@sentry/browser - with treeshaking flags 28.23 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.12 kB - -
@sentry/browser (incl. Tracing) 52.2 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 52.2 kB - -
@sentry/browser (incl. Tracing, Profiling) 55.17 kB - -
@sentry/browser (incl. Tracing, Replay) 92.73 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 81.52 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 97.08 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 110.51 kB - -
@sentry/browser (incl. Feedback) 47.66 kB - -
@sentry/browser (incl. sendFeedback) 30.62 kB - -
@sentry/browser (incl. FeedbackAsync) 35.62 kB - -
@sentry/browser (incl. Metrics) 31.36 kB - -
@sentry/browser (incl. Logs) 31.64 kB - -
@sentry/browser (incl. Metrics & Logs) 32.3 kB - -
@sentry/react 32.04 kB - -
@sentry/react (incl. Tracing) 54.86 kB - -
@sentry/vue 38.21 kB - -
@sentry/vue (incl. Tracing) 55.14 kB - -
@sentry/svelte 30.05 kB - -
@sentry/remix (Remix 3 client bundle) 57.96 kB - -
CDN Bundle 31.79 kB - -
CDN Bundle (incl. Tracing) 52.58 kB - -
CDN Bundle (incl. Logs, Metrics) 34.58 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 55.14 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 75.61 kB - -
CDN Bundle (incl. Tracing, Replay) 90.63 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 93.25 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 96 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 98.6 kB - -
CDN Bundle - uncompressed 92.69 kB - -
CDN Bundle (incl. Tracing) - uncompressed 155.13 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 101.03 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 162.76 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 231.11 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 275.27 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 282.9 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 287.07 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 294.69 kB - -
@sentry/nextjs (client) 58.31 kB +0.13% +74 B 🔺
@sentry/sveltekit (client) 53.26 kB - -
@sentry/core/server 44.53 kB - -
@sentry/core/browser 13.99 kB - -
@sentry/node 149.35 kB -0.01% -4 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.18 kB - -
@sentry/node - without tracing 88.27 kB -0.01% -4 B 🔽
@sentry/node - without channel injection 138.45 kB -0.01% -2 B 🔽
@sentry/aws-serverless 97.19 kB -0.01% -6 B 🔽
@sentry/cloudflare (withSentry) - minified 204.35 kB - -
@sentry/cloudflare (withSentry) 474.32 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 229.66 kB - -

View base workflow run

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 1 not successful, 10 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E nextjs-16-cacheComponents (cloudflare) Test (optional) Failing after 10m Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E nextjs-16 (cloudflare, latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E supabase-nextjs Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (deno) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-orpc Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-14 (latest) Test (optional) Successful in 2m Details

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@chargome

chargome commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/nextjs/src/server/index.ts Resolved
@chargome

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@chargome

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@chargome

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@chargome
chargome force-pushed the chargome/feat/nextjs-server-timing-header branch from 2091a1e to 5ad59f6 Compare November 12, 2026 15:09
@chargome
chargome changed the base branch from develop to chargome/ref/remix-core-server-timing-utils November 12, 2026 15:09
@chargome

Copy link
Copy Markdown
Member Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 5ad59f6. Configure here.

…responses

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@chargome
chargome force-pushed the chargome/feat/nextjs-server-timing-header branch from 5ad59f6 to ef9e5f1 Compare November 12, 2026 15:30
@chargome
chargome added this pull request to stack #26327 November 12, 2026 16:15
@chargome
chargome marked this pull request as ready for review November 12, 2026 16:25
@chargome
chargome requested a review from a team November 12, 2026 16:25
@chargome
chargome requested a review from a team as a code owner November 12, 2026 16:25
@chargome
chargome removed request for a team, Lms24 and nicohrubec November 12, 2026 16:25

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Send trace context in the Server-Timing header for self-hosted Next.js

1 participant