Repository navigation
fix(vite): only skip vite-* WebSocket upgrades destined for Vite's HMR base - #4291
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing: 4
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
4 of 8 tasks
Contributor
Author
This comment was written by an AI assistant on behalf of the Nitro maintainers. Please double-check anything that looks wrong. |
pi0
marked this pull request as ready for review
October 22, 2026 04:31
…R base Mirror Vite's own claim condition (subprotocol + HMR base path) and where its WebSocket server listens (`server.ws`, or `server.hmr` on older Vite 8), so proxied upstream HMR sockets on other paths reach Nitro.
pi0
approved these changes
Oct 22, 2026
This branch was successfully deployed
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.
Summary
In dev mode, Nitro's Vite integration skipped every WebSocket upgrade whose
Sec-WebSocket-Protocolstarted withvite-, so Vite could handle it as its own HMR socket. That check was too broad: it ignored the request path.So a
vite-hmrupgrade on any other path never got an answer. One example is a Nitro route ordevProxythat forwards the HMR socket of an upstream Vite. Nitro skipped it, Vite ignored it (wrong path), and the socket stayedpendingforever.Fix
Nitro now uses the same check Vite uses (
createWebSocketServer) and only defers when Vite will actually take the upgrade:vite-hmrorvite-pingand the path equals Vite's HMR base (base+ws.path)httpServer. It doesn't whenserver.ws === falseor when it runs on its ownserverorport. Options are read fromserver.ws(Vite 8.3), with a fallback toserver.hmrfor older Vite 8.server.hmr: falsedoes not stop Vite's listener, so Nitro still defers in that case. If Nitro handled those upgrades too, both would answer on the same socket and break the HMR connection.Tests
test/vite/websocket-hmr.test.ts(fixturetest/vite/websocket-hmr-fixture) runs a real Vite + Nitro dev server with the default config, withserver.hmr: false, and with a customserver.ws.path:vite-hmrupgrade on a Nitro route reaches Nitro. It times out onmain.vite-hmrupgrade on the HMR base still goes to Vite and closes cleanly. If Nitro stops deferring whenhmr: false, this check fails.🤖 Generated with AI assistant