Repository navigation
feat(nextjs): Send the trace in the Server-Timing header of document responses - #26107
Conversation
|
bugbot run |
size-limit report 📦Compared: 49b7f01 (PR test merge using develop at aa092cd) Against: cb81d76 (develop) · base run #66242
|
Optional E2E testsSome optional checks were not successful. They do not block the merge. 1 not successful, 9 successful. See the run.
Successful checks
|
|
bugbot run |
|
bugbot run |
|
bugbot run |
|
bugbot run |
2091a1e to
5ad59f6
Compare
|
bugbot run |
5ad59f6 to
ef9e5f1
Compare
| const hasCacheOrigin = !!lastMetaContent(document, CACHE_ORIGIN_META_TAG_NAME); | ||
| const isConnected = hasServerTimingTraceContext(); | ||
| const setServerRenderingType = (type: string): void => { | ||
| if (hasCacheOrigin && span.isRecording()) { |
There was a problem hiding this comment.
M: Can we add a test where the pageload ends before the dynamic part arrives? When there are slow dynamic parts, the rendering type cannot be set as it only does this while the span is still recording.
Before, we had the PAGELOAD_TRACE_META_TAG_TIMEOUT_MS (10 seconds) timeout for the pageload (we still have that). But now the code moved into the observe* function which does not have the timeout anymore. Or is this on purpose? 🤔
There was a problem hiding this comment.
Dropped on purpose, the observe does not wait for the pageload but only watches for a tag after the pageload started. Added a new bound so it stops when the pageload span ends.
Added a test 👍
| // Only the navigation entry of a document exposes the header to the browser SDK. | ||
| function isDocument(response: HttpServerResponse, headers: HeaderObject | undefined): boolean { | ||
| const contentType = readHeader(response, headers, 'content-type'); | ||
| return !contentType || contentType.startsWith('text/html'); |
There was a problem hiding this comment.
...so responses without a content type don't count as document.
| return !contentType || contentType.startsWith('text/html'); | |
| return contentType?.toLowerCase().startsWith('text/html') |
|
bugbot run |
There was a problem hiding this comment.
✅ 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 2e5db30. Configure here.
…responses Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…uire a document content type Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
68d3f3c to
60ae2cb
Compare
…ader connected the pageload Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…26325) Next.js is about to send the trace in a `Server-Timing` header (#26107). This pr just prepares the reusable functions for the consuming SDKs (Next, Remix, Nitro) . `isSharedCacheable` takes a header reader so it works on a fetch `Response` and on a Node `ServerResponse` alike. Refs JS-3864 --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
…26397) ## What Next.js now writes a `sentry-render-start` meta tag (whole UNIX seconds) next to `sentry-cache-origin`. The browser uses it to set `page.server_rendering.age` on the pageload span, so the UI can show how old the cached HTML was. The browser side is in `@sentry/core`, so SvelteKit and Nuxt only need to write the tag. They follow in stacked PRs. ## Why The `cache_origin` link tells you which render produced a cached page, but not how old that page is. That linked span is often not sampled or already gone, so the browser needs the start time itself. The tag holds the start of the linked render span. Without a recording span, it holds the current time, which is the render start, because Next.js computes the meta tags at the start of `prerenderToStream`. The attribute comes from getsentry/sentry-conventions#799, which is not merged yet, so the name is a local constant with a TODO. A `Server-Timing` header (#26107) does not replace the tag: shared-cacheable responses and Vercel do not send it, and it cannot hold the render time of a cached shell.
Stacked on the core and Remix PRs; this one holds the Next.js change only.
Why: Under
cacheComponentsthesentry-tracemeta tag arrives with the resumed part of the document, usually after the browser SDK started, so the pageload has to wait for it (#25142). AServer-Timingheader 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 inwriteHeadrather 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 unattachedAfter, 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.typeThe 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. The check is the one the Remix 3 middleware already had, now shared as
isSharedCacheablein core. Non-document responses get no header either.Since the pageload no longer waits for the tag,
page.server_rendering.typeis settled after span start for a runtime shell with a dynamic part, sotracesSamplerdoes not see it in that case. The observer stops when the pageload span ends, so a dynamic part that outlasts the pageload leaves the type unset.Closes #24945
Refs JS-3864