From 9d9f3754e7edb6ede492a722ee55435732bb9f70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=20Emir=20=C5=9Een?= Date: Wed, 8 May 2024 23:47:56 +0300 Subject: [PATCH] fix(devtools-shared): delay unestablished ws connections from closing --- packages/devtools-shared/src/context.tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/devtools-shared/src/context.tsx b/packages/devtools-shared/src/context.tsx index 8e3b14c01467..e98649f6dfdf 100644 --- a/packages/devtools-shared/src/context.tsx +++ b/packages/devtools-shared/src/context.tsx @@ -69,7 +69,16 @@ export const DevToolsContextProvider: React.FC = ({ return () => { unsubscribe(); - wsInstance.close(1000, window.location.origin); + // In strict mode, the WebSocket instance might not be connected yet + // so we need to wait for it to connect before closing it + // otherwise it will log an unnecessary error in the console + if (wsInstance.readyState === WebSocket.CONNECTING) { + wsInstance.addEventListener("open", () => { + wsInstance.close(1000, window.location.origin); + }); + } else { + wsInstance.close(1000, window.location.origin); + } }; }, []);