Skip to content

Add opt-in Server-Timing for content fetches and evaluations - #18

Open
Fryuni wants to merge 1 commit into
masterfrom
fryuni/ssr-server-timing-headers
Open

Fryuni wants to merge 1 commit into
masterfrom
fryuni/ssr-server-timing-headers

Conversation

@Fryuni

@Fryuni Fryuni commented Nov 23, 2026

Copy link
Copy Markdown
Member

Summary

Adds an opt-in flag that times every server-side fetchContent and evaluate call and reports it in the Server-Timing of the response being rendered, following croct-tech/plug-next#215.

Enable via nuxt.config.ts (croct: { serverTiming: true }) or at runtime via NUXT_CROCT_SERVER_TIMING=true:

Server-Timing: croct;desc="Croct evaluate";dur=64.5
Server-Timing: croct;desc="Croct fetch home-hero@1";dur=252.9

When disabled (the default), nothing is wrapped and no header or trailer is added.

How it works

  • When runtimeConfig.croct.serverTiming is true, a Nitro server plugin wraps nitroApp.h3App.handler and tracks each top-level request's ServerResponse in a globalThis-shared AsyncLocalStorage (Symbol.for('@croct/plug-nuxt/server-timing')).
  • Internal subrequests made during SSR (such as useRequestFetch() calling /api/_croct/content or /api/_croct/evaluate) inherit the outer request's store so timings are written to the SSR response being rendered rather than the discarded internal subrequest response.
  • Each fetchContent and evaluate call records its elapsed duration to that response: via appendHeader('Server-Timing', metric) while headers are still open, and via addTrailers({'Server-Timing': ...}) (accumulating trailers per response because Node replaces trailers on every addTrailers() call) once response.headersSent is true.

Validation

  • npm run validate, npm run lint, npm run build, and npm test (12 files, 124 tests) pass.
  • Added unit tests for serverTiming (including a real node:http server verifying headers before streaming, accumulated trailers after streaming, description sanitization, rejected calls, nested subrequest inheritance, concurrent isolation, and ended responses) and for the Nitro plugin.
  • Added Playwright E2E coverage in e2e/specs/server-timing.spec.ts and verified opt-in omission in e2e/specs/prerender/hybrid-rendering.spec.ts.
  • Verified end-to-end against both a production Nitro build (nuxi build e2e/app && node e2e/app/.output/server/index.mjs) and nuxi dev (including streamed headers + trailers and 120 concurrent keep-alive requests).

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

@Fryuni Fryuni added the feature New feature label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant