From ff67f09deb48a388e79640c364b7f85613dc2ebf Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Tue, 8 Feb 2022 23:06:32 -0500 Subject: [PATCH] fix(client): if the websocket fails to connect, wait on the socket host, not the ping host The problem with checking the ping host is that the ping host can pass, even if the socket host fails, leading to infinite reloads. Infinite reloads are a bad way to deal with this failure. This makes the failure less bad. For examples, see: https://github.com/vitejs/vite/issues/6814 https://github.com/vitejs/vite/issues/3093 --- packages/vite/src/client/client.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 48917204d8c291..6a7f9b27c3a700 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -201,12 +201,10 @@ async function waitForSuccessfulPing(ms = 1000) { // eslint-disable-next-line no-constant-condition while (true) { try { - const pingResponse = await fetch(`${base}__vite_ping`) - - // success - 2xx status code - if (pingResponse.ok) break - // failure - non-2xx status code - else throw new Error() + // A fetch on a websocket URL will return a successful promise with status 400, + // but will reject a networking error. + await fetch(`${location.protocol}//${socketHost}`) + break } catch (e) { // wait ms before attempting to ping again await new Promise((resolve) => setTimeout(resolve, ms))