-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid unnecessary reload after HMR WebSocket connection lost #5675
Comments
I have same problem #6089 |
I want to flag an important reason I'd like to be able to deactivate the websocket. An active websocket blocks the bfcache on the browser. So the bfcaching caching behavior locally is different than the caching behavior in prod, where there is no websocket, at least for me. I therefore couldn't reproduce a certain cache-related bug locally until I realized the Vite websocket was affecting it. Because there was no way to shut off websockets completely, I had to strip Vite out to be able to reproduce the bug. Just leaving this comment here since I didn't see any reference to this caching issue in any of the websocket-related Vite issues, and I'm sure someone will have this problem someday. |
How this happens is:When the web socket connection was disconnected, Vite's HMR client starts to ping to vite/packages/vite/src/client/client.ts Lines 311 to 333 in 29abeea
When the Vite server starts, Vite will respond to that ping request. Then, Vite's HMR client reload the page. vite/packages/vite/src/client/client.ts Lines 103 to 105 in 29abeea
But this has some edge cases. For example, when WebSocket server starts after the HTTP Server (can happen in middleware mode). Where we need to change is:
I guess we need to use a different protocol( |
Does this mean we need to create our own WebSocket server and reference it in Vite |
No, Vite already has a WebSocket server and we can reuse that. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Would love to see this be configurable. For my situation (a very simple vite server setup w/ some proxies for socketio and our api, nothing else), this diff allowed me to retain HMR functionality but not reload the page: diff --git a/dist/client/client.mjs b/dist/client/client.mjs
index e944ed731f936d5a35fe8368ce55c0518ade537e..ba56411cce8960a5dd0c47bf9b3bb13a8b8498f6 100644
--- a/dist/client/client.mjs
+++ b/dist/client/client.mjs
@@ -483,7 +483,7 @@ function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {
notifyListeners('vite:ws:disconnect', { webSocket: socket });
console.log(`[vite] server connection lost. polling for restart...`);
await waitForSuccessfulPing(protocol, hostAndPath);
- location.reload();
+ setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen)
});
return socket;
} It's not clear if this would break other things but it seems to work just fine for me. |
any update on this? |
+1 on wondering if this will be fixed |
Clear and concise description of the problem
Currently Vite reloads the page after a successful ping after connection lost.
In a hybrid app, once the app launches another app, the connection lost. When the app switches back, the app reloads. To properly debug routines involve switching to another app, we have to either use production build or comment out
location.reload()
invite/dist/client/client.mjs
.Suggested solution
Reconnect WebSocket to check HMR status instead of reload unconditionally.
Alternative
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: