Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reload only once on socket reconnect #2340

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
use setTimeout chaining instead
  • Loading branch information
tjk committed Mar 16, 2021
commit 5725e90982e7a1a8e7771c414813486dea939332
25 changes: 14 additions & 11 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,23 @@ async function queueUpdate(p: Promise<(() => void) | undefined>) {
}
}

async function waitForSuccessfulPing(ms = 1000) {
while (true) {
try {
await fetch(`${base}__vite_ping`)
break
} catch (e) {
await new Promise(resolve => setTimeout(resolve, ms))
}
}
}

// ping server
socket.addEventListener('close', ({ wasClean }) => {
socket.addEventListener('close', async ({ wasClean }) => {
if (wasClean) return
console.log(`[vite] server connection lost. polling for restart...`)
const interval = setInterval(() => {
fetch(`${base}__vite_ping`)
.then(() => {
clearInterval(interval)
location.reload()
})
.catch((e) => {
/* ignore */
})
}, 1000)
await waitForSuccessfulPing()
location.reload()
})

// https://wicg.github.io/construct-stylesheets
Expand Down