mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #344] [MERGED] fix: middleware crash in dev mode with cloudflare plugin #498
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#498
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/344
Author: @james-elicx
Created: 3/8/2026
Status: ✅ Merged
Merged: 3/8/2026
Merged by: @james-elicx
Base:
main← Head:j-branch-3📝 Commits (3)
27d314atest: middleware with cloudflare plugin and routing bypass6617773fix: use module runner for pages middleware70c2fc8add pages router dev test📊 Changes
22 files changed (+883 additions, -129 deletions)
View changed files
📝
.github/workflows/ci.yml(+1 -0)➕
examples/app-router-cloudflare/middleware.ts(+14 -0)➕
examples/pages-router-cloudflare/instrumentation-state.ts(+144 -0)➕
examples/pages-router-cloudflare/instrumentation.ts(+27 -0)➕
examples/pages-router-cloudflare/middleware.ts(+17 -0)➕
examples/pages-router-cloudflare/pages/api/error-route.ts(+7 -0)➕
examples/pages-router-cloudflare/pages/api/instrumentation-test.ts(+29 -0)📝
examples/pages-router-cloudflare/pages/ssr.tsx(+8 -2)📝
packages/vinext/src/index.ts(+75 -5)➕
packages/vinext/src/server/dev-module-runner.ts(+137 -0)📝
packages/vinext/src/server/instrumentation.ts(+21 -7)📝
packages/vinext/src/server/middleware.ts(+12 -6)📝
playwright.config.ts(+22 -1)➕
tests/e2e/cloudflare-dev/middleware.spec.ts(+89 -0)➕
tests/e2e/cloudflare-pages-router-dev/middleware.spec.ts(+91 -0)➕
tests/e2e/cloudflare-pages-router-dev/pages-router.spec.ts(+25 -0)➕
tests/e2e/cloudflare-pages-router/middleware.spec.ts(+44 -0)➕
tests/e2e/pages-router/instrumentation-startup.spec.ts(+48 -0)📝
tests/e2e/pages-router/instrumentation.spec.ts(+23 -59)📝
tests/features.test.ts(+17 -17)...and 2 more files
📄 Description
Adds regression tests for two bugs that occur when
@cloudflare/vite-pluginis present alongsidemiddleware.ts.Bug 1 —
outsideEmittercrashThe connect handler called
runMiddleware()→server.ssrLoadModule(), which constructs anSSRCompatModuleRunner. That runner requires a hot channel that doesn't exist in the host process when the Cloudflare plugin is loaded, crashing immediately with:Fixed by switching to
createDirectRunner(), which callsenvironment.fetchModule()directly and never touches the hot channel.Bug 2 — requests intercepted before reaching the Worker
After the crash was resolved, the connect handler was still calling
createSSRHandler()for any Pages Router route match, rendering pages in the host process instead of dispatching to the Cloudflare Worker. Routes like/that matched both apages/stub and anapp/route were served by the host instead of miniflare.Fixed by the
hasCloudflarePluginearly-return: after middleware runs, the connect handler callsnext()unconditionally, handing off to the Cloudflare plugin.How the tests assert Worker execution
/api/helloreturnsnavigator.userAgent—"Cloudflare-Workers"inside miniflare, absent or wrong if intercepted by the host./has competing handlers:app/page.tsxrenders"vinext on Cloudflare Workers",pages/index.tsxrenders"pages index". The wrong text appearing means the Worker was bypassed.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.