Skip to content

fix(vite-plugin): preserve worker websocket service bindings in dev#13098

Open
raashish1601 wants to merge 3 commits into
cloudflare:mainfrom
raashish1601:contributor-01/workers-sdk-13054
Open

fix(vite-plugin): preserve worker websocket service bindings in dev#13098
raashish1601 wants to merge 3 commits into
cloudflare:mainfrom
raashish1601:contributor-01/workers-sdk-13054

Conversation

@raashish1601
Copy link
Copy Markdown

@raashish1601 raashish1601 commented Mar 27, 2026

Fixes #13054.

WebSocket upgrade requests for dev-mode worker service bindings currently get routed through __VITE_MIDDLEWARE__, which only has the Node middleware bridge. That makes proxied worker-to-worker WebSocket upgrades miss the real entry worker and crash in Miniflare's loopback upgrade path.

This change routes Upgrade: websocket requests in __vite_proxy_worker__ straight to ENTRY_USER_WORKER.fetch(...) while keeping normal HTTP requests on the existing Vite middleware path. I also added a focused unit test for the proxy dispatch logic and a multi-worker playground regression covering Worker A -> Worker B WebSocket proxying in dev mode.

Local validation:

  • node node_modules/vitest/vitest.mjs run src/__tests__/vite-proxy-worker.spec.ts
  • node node_modules/tsdown/dist/run.mjs
  • node node_modules/typescript/bin/tsc -b packages/vite-plugin-cloudflare/playground/multi-worker/tsconfig.json
  • I attempted to run the targeted playground e2e spec as well, but this Windows host does not currently have the required Playwright Chromium binary installed.

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: this is an internal Vite dev-mode service-binding fix with no public API or docs surface.

A picture of a cute animal (not mandatory, but encouraged)


Open with Devin

@raashish1601 raashish1601 requested a review from a team as a code owner March 27, 2026 19:02
@raashish1601 raashish1601 requested a review from vicb March 27, 2026 19:02
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 27, 2026

🦋 Changeset detected

Latest commit: 4842265

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@workers-devprod
Copy link
Copy Markdown
Contributor

workers-devprod commented Mar 27, 2026

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • packages/vite-plugin-cloudflare/src/workers/vite-proxy-worker/index.ts: [@cloudflare/wrangler]

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

devin-ai-integration[bot]

This comment was marked as resolved.

Upgrade requests (e.g. WebSocket) cannot be proxied through the Node.js
middleware binding because the upgrade event provides a raw Duplex socket
rather than an http.ServerResponse. Detect the Upgrade header in
ViteProxyWorker and send these requests directly to the entry worker.

Replaces the original unit test and playground regression with a
dev-registry fixture test that exercises the actual cross-process path
(vite dev <-> vite dev via the dev registry).
@penalosa penalosa force-pushed the contributor-01/workers-sdk-13054 branch from f1cb50c to 4842265 Compare March 30, 2026 16:49
@github-project-automation github-project-automation Bot moved this to Untriaged in workers-sdk Mar 30, 2026
@github-project-automation github-project-automation Bot moved this from Untriaged to Approved in workers-sdk Mar 30, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 30, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@13098

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@13098

miniflare

npm i https://pkg.pr.new/miniflare@13098

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@13098

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@13098

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@13098

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@13098

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@13098

wrangler

npm i https://pkg.pr.new/wrangler@13098

commit: 4842265

@jamesopstad
Copy link
Copy Markdown
Contributor

Hi @raashish1601. Thanks for the PR and for fixing the issue! There are just some formatting issues so please can you run pnpm prettify from the root of the repo and then push up the changes. Then we can get it merged.

@Jdruwe
Copy link
Copy Markdown

Jdruwe commented Apr 2, 2026

Just wondering if this PR will solve my usecase:

  • Turboreporo
  • 2 apps
    -- /apps/chat-agent
    -- /apps/web

web wrangler file:

...
  "services": [
    {
      "binding": "ChatAgent",
      "service": "chat-agent",
      "entrypoint": "ChatAgent"
    }
  ]

Running locally using turbo dev I am getting in my web app:

SyntaxError: Unexpected token '', ""... is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes

I have a feeling this is also related to the web app not being able to properly resolve the chat-agent in time?

@petebacondarwin petebacondarwin removed the request for review from vicb April 15, 2026 08:06
@apostolos-geyer
Copy link
Copy Markdown

any timeline on this getting shipped?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved

Development

Successfully merging this pull request may close these issues.

WebSocket upgrades proxied through a Worker service binding do not work in Vite dev mode

6 participants