mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #330] [MERGED] fix: register instrumentation in rsc entry and put error handler in global scope #482
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#482
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/cloudflare/vinext/pull/330
Author: @james-elicx
Created: 3/7/2026
Status: ✅ Merged
Merged: 3/7/2026
Merged by: @james-elicx
Base:
main← Head:j-branch-2📝 Commits (4)
b1e98b5test: instrumentation051f3a4store onRequestError on globalThisaae5e41add instrumentation registration app-dev-server8d80bb2guard against double calling runinstrumentation for appdir📊 Changes
15 files changed (+471 additions, -23 deletions)
View changed files
➕
examples/app-router-cloudflare/app/api/error-route/route.ts(+5 -0)➕
examples/app-router-cloudflare/app/api/instrumentation-test/route.ts(+29 -0)➕
examples/app-router-cloudflare/instrumentation-state.ts(+51 -0)➕
examples/app-router-cloudflare/instrumentation.ts(+52 -0)📝
examples/app-router-cloudflare/tsconfig.json(+1 -1)📝
packages/vinext/src/global.d.ts(+14 -1)📝
packages/vinext/src/index.ts(+8 -2)📝
packages/vinext/src/server/app-dev-server.ts(+19 -0)📝
packages/vinext/src/server/instrumentation.ts(+45 -14)📝
playwright.config.ts(+16 -5)➕
tests/e2e/app-router/instrumentation.spec.ts(+121 -0)➕
tests/fixtures/app-basic/app/api/instrumentation-test/route.ts(+29 -0)➕
tests/fixtures/app-basic/instrumentation-state.ts(+39 -0)➕
tests/fixtures/app-basic/instrumentation.ts(+38 -0)📝
tests/instrumentation.test.ts(+4 -0)📄 Description
Instrumentation.ts: App Router support + Cloudflare Workers e2e tests
Completes
instrumentation.tssupport for App Router, and adds e2e coverage that runs against the production Cloudflare Workers build.What changed
Core:
register()baked into the RSC entryPreviously
runInstrumentation()was called fromconfigureServer()in the host Node.js process. This broke with@cloudflare/vite-pluginbecause the plugin runs the RSC environment inside a miniflare Worker subprocess — a separate process from the host, with its ownglobalThis. Callingregister()in the host meant it never ran in the same process as request handling.The fix:
generateRscEntry()now accepts aninstrumentationPathparameter and emitsregister()as a top-levelawaitat module evaluation time, before any requests are served. This runs in whatever process/environment evaluates the RSC entry — the Worker subprocess in Cloudflare dev, the RSC Vite environment with@vitejs/plugin-rsc, or the built Worker binary in production.configureServer()still callsrunInstrumentation()for Pages Router only, where there is no RSC entry and@cloudflare/vite-pluginis never present.onRequestErrorhandler storageThe handler is now stored on
globalThis.__VINEXT_onRequestErrorHandler__instead of a module-level variable. This makes it visible across the RSC and SSR Vite environment module graphs (separate instances, sharedglobalThis) and inside the Cloudflare Worker (single global scope). The key is declared inglobal.d.tsfor type safety.e2e coverage
tests/e2e/app-router/instrumentation.spec.ts— new spec coveringregister()startup,onRequestError()on route throws, multiple error capture, and no false positives on success. Uses relative URLs + PlaywrightbaseURLso it's portable across projects.cloudflare-workersPlaywright project now picks up this spec viatestMatch, running the same assertions against the production wrangler build on port 4176.app-router-cloudflareexample getsinstrumentation.ts,instrumentation-state.ts,/api/instrumentation-test, and/api/error-routeto support the tests.Unit tests
tests/instrumentation.test.tsgets abeforeEachcleanup ofglobalThis.__VINEXT_onRequestErrorHandler__to prevent handler bleed between tests after the storage change.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.