fix: Prevent Vite reload from canceling logout redirect (#23375) (CP: 24.9)#23392
Merged
fix: Prevent Vite reload from canceling logout redirect (#23375) (CP: 24.9)#23392
Conversation
8c2ded1 to
79f636c
Compare
When a user logs out and the session is invalidated, the server closes WebSocket connections with close code 1008 (VIOLATED_POLICY). Vite detects this disconnection and starts polling for reconnection, then reloads the page - canceling any server-initiated redirect. The fix uses a Promise-based synchronization between Vite's HMR events: 1. On vite:ws:connect, we register a close listener and store a Promise on the WebSocket that resolves with the close code when triggered. 2. On vite:ws:disconnect, we await this Promise to get the close code. Since Vite's notifyListeners awaits our async handler, this creates a synchronization point before Vite checks its willUnload flag. 3. When close code is 1008, we dispatch a beforeunload event to set Vite's internal willUnload flag, preventing the reload. This approach works because Vite's close handler does not await the Promise returned by onMessage/handleMessage, so our async disconnect handler can complete (setting willUnload) before Vite's willUnload check runs. Fixes #20819
79f636c to
6ebdc0d
Compare
|
caalador
approved these changes
Feb 2, 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.



When a user logs out and the session is invalidated, the server closes WebSocket connections with close code 1008 (VIOLATED_POLICY). Vite detects this disconnection and starts polling for reconnection, then reloads the page - canceling any server-initiated redirect.
The fix uses a Promise-based synchronization between Vite's HMR events:
On vite:ws:connect, we register a close listener and store a Promise on the WebSocket that resolves with the close code when triggered.
On vite:ws:disconnect, we await this Promise to get the close code. Since Vite's notifyListeners awaits our async handler, this creates a synchronization point before Vite checks its willUnload flag.
When close code is 1008, we dispatch a beforeunload event to set Vite's internal willUnload flag, preventing the reload.
This approach works because Vite's close handler does not await the Promise returned by onMessage/handleMessage, so our async disconnect handler can complete (setting willUnload) before Vite's willUnload check runs.
Fixes #20819