Skip to content

Commit

Permalink
fix(client): if the websocket fails to connect, wait on the socket ho…
Browse files Browse the repository at this point in the history
…st, 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:
#6814
#3093
  • Loading branch information
nicks committed May 10, 2022
1 parent a709440 commit ff67f09
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions packages/vite/src/client/client.ts
Expand Up @@ -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))
Expand Down

0 comments on commit ff67f09

Please sign in to comment.