2017-02-26 19:45:16 +00:00
|
|
|
/* global location */
|
|
|
|
|
|
|
|
import Router from '../lib/router'
|
|
|
|
import fetch from 'unfetch'
|
|
|
|
|
2017-03-13 01:06:50 +00:00
|
|
|
Router.ready(() => {
|
2017-03-13 04:34:30 +00:00
|
|
|
Router.router.events.on('routeChangeComplete', ping)
|
2017-03-13 01:06:50 +00:00
|
|
|
})
|
2017-03-12 03:51:49 +00:00
|
|
|
|
2017-02-26 19:45:16 +00:00
|
|
|
async function ping () {
|
|
|
|
try {
|
2017-02-27 20:04:01 +00:00
|
|
|
const url = `/_next/on-demand-entries-ping?page=${Router.pathname}`
|
2017-02-26 19:45:16 +00:00
|
|
|
const res = await fetch(url)
|
|
|
|
const payload = await res.json()
|
|
|
|
if (payload.invalid) {
|
|
|
|
location.reload()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Error with on-demand-entries-ping: ${err.message}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runPinger () {
|
|
|
|
while (true) {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 5000))
|
|
|
|
await ping()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runPinger()
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
})
|