2017-02-26 19:45:16 +00:00
|
|
|
/* global location */
|
|
|
|
|
|
|
|
import Router from '../lib/router'
|
|
|
|
import fetch from 'unfetch'
|
|
|
|
|
2017-04-06 06:11:13 +00:00
|
|
|
export default () => {
|
|
|
|
Router.ready(() => {
|
|
|
|
Router.router.events.on('routeChangeComplete', ping)
|
|
|
|
})
|
2017-03-12 03:51:49 +00:00
|
|
|
|
2017-04-06 06:11:13 +00:00
|
|
|
async function ping () {
|
|
|
|
try {
|
|
|
|
const url = `/_next/on-demand-entries-ping?page=${Router.pathname}`
|
|
|
|
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}`)
|
2017-02-26 19:45:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 06:11:13 +00:00
|
|
|
async function runPinger () {
|
|
|
|
while (true) {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 5000))
|
|
|
|
await ping()
|
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 06:11:13 +00:00
|
|
|
runPinger()
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
})
|
|
|
|
}
|