From f4b44054127e82c9ee4118fc157a157467ae3216 Mon Sep 17 00:00:00 2001 From: Samuel Alev Date: Sat, 13 Aug 2022 09:16:31 +0200 Subject: [PATCH] fix: allow ping to http from https website (#9561) --- packages/vite/src/client/client.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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