mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Tie page visibility api to on demand pinger (#2818)
Avoid making requests to the on demand reloader if the page is not visible.
This commit is contained in:
parent
c0d031d90e
commit
808c662e3b
|
@ -30,15 +30,31 @@ export default () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let pingerTimeout
|
||||||
async function runPinger () {
|
async function runPinger () {
|
||||||
while (true) {
|
// Will restart on the visibilitychange API below. For older browsers, this
|
||||||
await new Promise((resolve) => setTimeout(resolve, 5000))
|
// will always be true and will always run, but support is fairly prevalent
|
||||||
|
// at this point.
|
||||||
|
while (!document.hidden) {
|
||||||
await ping()
|
await ping()
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
pingerTimeout = setTimeout(resolve, 5000)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runPinger()
|
document.addEventListener('visibilitychange', () => {
|
||||||
.catch((err) => {
|
if (!document.hidden) {
|
||||||
console.error(err)
|
runPinger()
|
||||||
})
|
} else {
|
||||||
|
clearTimeout(pingerTimeout)
|
||||||
|
}
|
||||||
|
}, false)
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
runPinger()
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err)
|
||||||
|
})
|
||||||
|
}, 10000)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue