[PR #334] [MERGED] fix: pages router instrumentation error reporting for api routes #486

Closed
opened 2026-05-06 13:08:21 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/334
Author: @james-elicx
Created: 3/7/2026
Status: Merged
Merged: 3/7/2026
Merged by: @james-elicx

Base: mainHead: j-branch-4


📝 Commits (4)

📊 Changes

7 files changed (+295 additions, -0 deletions)

View changed files

examples/app-router-cloudflare/pages/index.tsx (+4 -0)
📝 packages/vinext/src/server/api-handler.ts (+12 -0)
tests/e2e/pages-router/instrumentation.spec.ts (+136 -0)
tests/fixtures/pages-basic/instrumentation-state.ts (+63 -0)
tests/fixtures/pages-basic/instrumentation.ts (+44 -0)
tests/fixtures/pages-basic/pages/api/error-route.ts (+7 -0)
tests/fixtures/pages-basic/pages/api/instrumentation-test.ts (+29 -0)

📄 Description

fix: instrumentation.ts Pages Router support + onRequestError for API routes

Extends instrumentation.ts support to Pages Router and fixes a gap where onRequestError was never called for API route errors.

What changed

onRequestError in API route handler

api-handler.ts caught errors from Pages Router API routes but never called reportRequestError, so onRequestError would silently not fire for any pages/api/* handler. Added the call in the catch block with routerKind: "Pages Router" and routeType: "render".

Pages Router fixture

tests/fixtures/pages-basic gets instrumentation.ts, instrumentation-state.ts, /api/instrumentation-test, and /api/error-route to support the new e2e tests. The state module uses globalThis keys rather than plain module-level variables — necessary because register() runs in an isolated ModuleRunner module graph (separate from the Vite SSR graph that handles API routes), so plain exports wouldn't be shared between them.

Pages Router e2e spec

tests/e2e/pages-router/instrumentation.spec.ts covers:

  • Dev server starts without crashing when instrumentation.ts is present (regression guard for the outsideEmitter crash)
  • register() was called before the first request
  • onRequestError() fires when an API handler throws, with correct routerKind/routeType/routePath
  • Multiple errors are captured independently
  • Successful requests don't trigger onRequestError

cloudflare-dev added to CI matrix

The **cloudflare-dev` added to CI matrix**

The cloudflare-dev Playwright project was missing from the CI matrix despite existing in playwright.config.ts. Added it so the Vite dev + @cloudflare/vite-plugin startup tests run in CI.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/334 **Author:** [@james-elicx](https://github.com/james-elicx) **Created:** 3/7/2026 **Status:** ✅ Merged **Merged:** 3/7/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `j-branch-4` --- ### 📝 Commits (4) - [`f24ee8e`](https://github.com/cloudflare/vinext/commit/f24ee8eb43e4ba1ea97043a2618f9fb86cb5b475) test: pages router instrumentation - [`7b9d7d7`](https://github.com/cloudflare/vinext/commit/7b9d7d7baf8da9f773bea835a9881d60588b91f5) fix error handling - [`a7ac963`](https://github.com/cloudflare/vinext/commit/a7ac9631591da674bea2694c910190c35cd21f2c) add pages route to app dir - [`3235045`](https://github.com/cloudflare/vinext/commit/3235045a5bfc66fd97e6946711c2c0d9ba5def31) fix api route type ### 📊 Changes **7 files changed** (+295 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `examples/app-router-cloudflare/pages/index.tsx` (+4 -0) 📝 `packages/vinext/src/server/api-handler.ts` (+12 -0) ➕ `tests/e2e/pages-router/instrumentation.spec.ts` (+136 -0) ➕ `tests/fixtures/pages-basic/instrumentation-state.ts` (+63 -0) ➕ `tests/fixtures/pages-basic/instrumentation.ts` (+44 -0) ➕ `tests/fixtures/pages-basic/pages/api/error-route.ts` (+7 -0) ➕ `tests/fixtures/pages-basic/pages/api/instrumentation-test.ts` (+29 -0) </details> ### 📄 Description **fix: instrumentation.ts Pages Router support + onRequestError for API routes** Extends instrumentation.ts support to Pages Router and fixes a gap where `onRequestError` was never called for API route errors. ## What changed **`onRequestError` in API route handler** `api-handler.ts` caught errors from Pages Router API routes but never called `reportRequestError`, so `onRequestError` would silently not fire for any `pages/api/*` handler. Added the call in the catch block with `routerKind: "Pages Router"` and `routeType: "render"`. **Pages Router fixture** `tests/fixtures/pages-basic` gets `instrumentation.ts`, `instrumentation-state.ts`, `/api/instrumentation-test`, and `/api/error-route` to support the new e2e tests. The state module uses `globalThis` keys rather than plain module-level variables — necessary because `register()` runs in an isolated `ModuleRunner` module graph (separate from the Vite SSR graph that handles API routes), so plain exports wouldn't be shared between them. **Pages Router e2e spec** `tests/e2e/pages-router/instrumentation.spec.ts` covers: - Dev server starts without crashing when `instrumentation.ts` is present (regression guard for the `outsideEmitter` crash) - `register()` was called before the first request - `onRequestError()` fires when an API handler throws, with correct `routerKind`/`routeType`/`routePath` - Multiple errors are captured independently - Successful requests don't trigger `onRequestError` **`cloudflare-dev` added to CI matrix** The `**`cloudflare-dev` added to CI matrix** The `cloudflare-dev` Playwright project was missing from the CI matrix despite existing in `playwright.config.ts`. Added it so the Vite dev + `@cloudflare/vite-plugin` startup tests run in CI. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:08:21 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#486
No description provided.