diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index b2a62f0dd98eca..0d0deb9347fb11 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -101,7 +101,7 @@ function setupWebSocket( } console.log(`[vite] server connection lost. polling for restart...`) - await waitForSuccessfulPing(hostAndPath) + await waitForSuccessfulPing(protocol, hostAndPath) location.reload() }) @@ -292,14 +292,22 @@ async function queueUpdate(p: Promise<(() => void) | undefined>) { } } -async function waitForSuccessfulPing(hostAndPath: string, ms = 1000) { +async function waitForSuccessfulPing( + socketProtocol: string, + hostAndPath: string, + ms = 1000 +) { + const pingHostProtocol = socketProtocol === 'wss' ? 'https' : 'http' + // eslint-disable-next-line no-constant-condition while (true) { try { // A fetch on a websocket URL will return a successful promise with status 400, // but will reject a networking error. // When running on middleware mode, it returns status 426, and an cors error happens if mode is not no-cors - await fetch(`${location.protocol}//${hostAndPath}`, { mode: 'no-cors' }) + await fetch(`${pingHostProtocol}://${hostAndPath}`, { + mode: 'no-cors' + }) break } catch (e) { // wait ms before attempting to ping again