Skip to content

fix: support WebSocket upgrades in dev server#3823

Merged
bartlomieju merged 1 commit into
mainfrom
fix/vite-websocket-upgrade
May 27, 2026
Merged

fix: support WebSocket upgrades in dev server#3823
bartlomieju merged 1 commit into
mainfrom
fix/vite-websocket-upgrade

Conversation

@bartlomieju
Copy link
Copy Markdown
Contributor

Vite's Connect-based dev server dispatches WebSocket upgrades as
'upgrade' events on the underlying http.Server rather than routing them
through middleware, so Deno.upgradeWebSocket() was never reached and
the client hung in CONNECTING. The plugin now attaches an upgrade
listener that hands the raw node socket off to Fresh's existing
ctx.upgrade() path.

The plumbing leans on denoland/deno#33342, which taught
Deno.upgradeWebSocket to accept a { socket, head } option for
requests that didn't come in through Deno.serve. A
WeakMap<Request, { socket, head }> kept on globalThis acts as the
side channel: the plugin's upgrade listener populates it,
ctx.upgrade() reads and clears it before calling Deno. The map lives
on globalThis because Vite's SSR runner evaluates context.ts
separately from Deno's own load of the plugin, so each side would
otherwise hold its own module-local instance.

Fixes #3350

Vite's Connect-based dev server dispatches WebSocket upgrades as
'upgrade' events on the underlying http.Server rather than routing them
through middleware, so Deno.upgradeWebSocket() was never called and the
client hung in CONNECTING.

Now that deno#33342 lets Deno.upgradeWebSocket accept a raw node socket
via the `socket` option, the plugin can hand the socket off to Fresh's
existing ctx.upgrade() path. A WeakMap<Request, {socket, head}> kept on
globalThis acts as the side channel — populated by the plugin's upgrade
listener, consumed by ctx.upgrade() before it calls
Deno.upgradeWebSocket. The globalThis singleton is needed because
Vite's SSR runner evaluates context.ts separately from Deno's own load
of the plugin, leaving each with its own module instance.

Fixes #3350
@bartlomieju bartlomieju changed the title fix(plugin-vite): support WebSocket upgrades in dev server fix: support WebSocket upgrades in dev server May 27, 2026
@bartlomieju bartlomieju merged commit 1fcf2d9 into main May 27, 2026
8 of 9 checks passed
@bartlomieju bartlomieju deleted the fix/vite-websocket-upgrade branch May 27, 2026 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fresh V2 Beta WebSocket upgrade request never reaches server behind vite development environment

1 participant