-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Avoid unnecessary reload after HMR WebSocket connection lost #5675
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
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
+1 |
It seems like first part of the change ("Change the ping to use WebSocket instead of HTTP") is already here But I’m failing to understand where we should handle WebSocket connection in |
@niksy What I explained at #5675 (comment) was implemented by #17891 |
@sapphi-red I’m referencing this situation; it doesn’t seem to work. What happens is Nodemon restarts whole application (including Vite dev server) and client keeps pinging but after some time it decides to reload whole page instead of using new WebSocket connection. |
If the connection is lost and the ping successes later on, Vite will reload the page. This is an intended behavior. Vite cannot just connect the WebSocket, because the Vite cannot know whether the page content that is already loaded is stale or not. I don't remember why I said "For that case, I think implementing #5675 would work." at #9007 (comment). I should have said "For #9007 (comment)" instead of "For that case". |
So give us some options.
|
Is this anyhow possible? Like try to create fresh connection and use it for subsequent interactions. |
Dunno. Clearly it's reconnecting if it's forcing a refresh, whether or not that can technically be used for subsequent interactions I'm not sure. Maybe there's some kind of state that's kept in memory and it's flushed when Vite dies so it's not possible. I'm really not sure how the internals work. But if it's not possible, I'd still prefer (3) over (1) |
I'm debugging in webview on iOS and this problem is driving me crazy... |
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: