fix: support WebSocket upgrades in dev server#3823
Merged
Conversation
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
This was referenced May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 andthe 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.upgradeWebSocketto accept a{ socket, head }option forrequests that didn't come in through
Deno.serve. AWeakMap<Request, { socket, head }>kept onglobalThisacts as theside channel: the plugin's upgrade listener populates it,
ctx.upgrade()reads and clears it before calling Deno. The map liveson
globalThisbecause Vite's SSR runner evaluatescontext.tsseparately from Deno's own load of the plugin, so each side would
otherwise hold its own module-local instance.
Fixes #3350