From 637d31bcc59d964e51f7969093cc369deee88ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Fri, 1 Nov 2024 18:48:41 +0900 Subject: [PATCH] fix(client): detect ws close correctly (#18548) --- packages/vite/src/client/client.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index d51dd73c6357f2..046110cafc09cc 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -91,9 +91,19 @@ function setupWebSocket( handleMessage(JSON.parse(data)) }) + let willUnload = false + window.addEventListener( + 'beforeunload', + () => { + willUnload = true + }, + { once: true }, + ) + // ping server - socket.addEventListener('close', async ({ wasClean }) => { - if (wasClean) return + socket.addEventListener('close', async () => { + // ignore close caused by top-level navigation + if (willUnload) return if (!isOpened && onCloseWithoutOpen) { onCloseWithoutOpen()