Skip to content

Commit

Permalink
fix: allow ping to http from https website (#9561)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelAlev authored Aug 13, 2022
1 parent 85e387a commit f4b4405
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function setupWebSocket(
}

console.log(`[vite] server connection lost. polling for restart...`)
await waitForSuccessfulPing(hostAndPath)
await waitForSuccessfulPing(protocol, hostAndPath)
location.reload()
})

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f4b4405

Please sign in to comment.