Skip to content

fix(vite): only skip vite-* WebSocket upgrades destined for Vite's HMR base - #4291

Merged
pi0 merged 1 commit into
mainfrom
fix/vite-ws
Oct 22, 2026
Merged

pi0 merged 1 commit into
mainfrom
fix/vite-ws

Conversation

@pi0x

@pi0x pi0x commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In dev mode, Nitro's Vite integration skipped every WebSocket upgrade whose Sec-WebSocket-Protocol started with vite-, so Vite could handle it as its own HMR socket. That check was too broad: it ignored the request path.

So a vite-hmr upgrade on any other path never got an answer. One example is a Nitro route or devProxy that forwards the HMR socket of an upstream Vite. Nitro skipped it, Vite ignored it (wrong path), and the socket stayed pending forever.

Fix

Nitro now uses the same check Vite uses (createWebSocketServer) and only defers when Vite will actually take the upgrade:

  • the subprotocol is vite-hmr or vite-ping and the path equals Vite's HMR base (base + ws.path)
  • and Vite's WebSocket server listens on this httpServer. It doesn't when server.ws === false or when it runs on its own server or port. Options are read from server.ws (Vite 8.3), with a fallback to server.hmr for older Vite 8.

server.hmr: false does 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 (fixture test/vite/websocket-hmr-fixture) runs a real Vite + Nitro dev server with the default config, with server.hmr: false, and with a custom server.ws.path:

  • a vite-hmr upgrade on a Nitro route reaches Nitro. It times out on main.
  • a vite-hmr upgrade on the HMR base still goes to Vite and closes cleanly. If Nitro stops deferring when hmr: false, this check fails.

🤖 Generated with AI assistant

@vercel

vercel Bot commented May 29, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nitro.build Ready Ready Preview Oct 22, 2026 4:32am UTC

Request Review

@coderabbitai

coderabbitai Bot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: edb975e7-410e-4679-a402-23eb6bb7381c
📥 Commits

Reviewing files that changed from the base of the PR and between a383c8f and c01a26b.

📒 Files selected for processing: 4
  • src/build/vite/dev.ts
  • test/vite/websocket-hmr-fixture/routes/ws.ts
  • test/vite/websocket-hmr-fixture/vite.config.ts
  • test/vite/websocket-hmr.test.ts
+----------------------------------------------------------+
| Nice abstraction. Shame about the reality underneath it. |
+----------------------------------------------------------+
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented May 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4291

commit: c01a26b

@pi0x pi0x added bug Something isn't working (PRs; issues use the Bug type) v3 labels Sep 2, 2026
@pi0x

pi0x commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author
  • I know this is a draft; I left the draft status alone.
  • Rebased onto main. One small conflict: the ufo import line, since main added withoutBase. Nothing else moved. Push is blocked in my environment, so nothing was pushed.
  • I checked the new condition against Vite 8's own code and it matches: ["vite-hmr","vite-ping"].includes(protocol) && pathname === hmrBase.
  • The test is a real regression test: it fails on main and passes with the fix.
  • One edge case: if a user sets server.hmr.server to the same http server, Vite does listen here, but we would return undefined and take the upgrade away from it. Rare, but worth a guard.
  • Small nit: test/vite/ already holds vite dev-server tests with checked-in *-fixture dirs. This test would fit better there than in test/unit/ with a mkdtemp inside the repo (stray .tmp-vite-ws-* dirs are left behind if it crashes, and they are not gitignored).

This comment was written by an AI assistant on behalf of the Nitro maintainers. Please double-check anything that looks wrong.

…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
pi0 merged commit bd72d90 into main Oct 22, 2026
13 of 14 checks passed
@pi0
pi0 deleted the fix/vite-ws branch October 22, 2026 04:33
@pi0x pi0x removed the v3 label Nov 10, 2026

This branch was successfully deployed

1 active deployment
Preview — c01a26be Deployed Oct 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working (PRs; issues use the Bug type)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants