2018-12-14 11:25:59 +00:00
|
|
|
/* global location, WebSocket */
|
2017-02-26 19:45:16 +00:00
|
|
|
|
2018-11-28 14:03:02 +00:00
|
|
|
import Router from 'next/router'
|
2017-02-26 19:45:16 +00:00
|
|
|
import fetch from 'unfetch'
|
|
|
|
|
2019-01-19 12:39:09 +00:00
|
|
|
const { hostname, protocol } = location
|
|
|
|
const wsProtocol = protocol.includes('https') ? 'wss' : 'ws'
|
2018-12-14 11:25:59 +00:00
|
|
|
const retryTime = 5000
|
|
|
|
let ws = null
|
|
|
|
let lastHref = null
|
|
|
|
|
|
|
|
export default async ({ assetPrefix }) => {
|
2017-04-18 04:18:43 +00:00
|
|
|
Router.ready(() => {
|
2018-07-31 19:04:14 +00:00
|
|
|
Router.events.on('routeChangeComplete', ping)
|
2017-04-18 04:18:43 +00:00
|
|
|
})
|
2017-03-12 03:51:49 +00:00
|
|
|
|
2018-12-14 11:25:59 +00:00
|
|
|
const setup = async (reconnect) => {
|
|
|
|
if (ws && ws.readyState === ws.OPEN) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
2019-01-19 12:39:09 +00:00
|
|
|
ws = new WebSocket(`${wsProtocol}://${hostname}:${process.env.NEXT_WS_PORT}${process.env.NEXT_WS_PROXY_PATH}`)
|
2018-12-14 11:25:59 +00:00
|
|
|
ws.onopen = () => resolve()
|
|
|
|
ws.onclose = () => {
|
|
|
|
setTimeout(async () => {
|
|
|
|
// check if next restarted and we have to reload to get new port
|
|
|
|
await fetch(`${assetPrefix}/_next/on-demand-entries-ping`)
|
|
|
|
.then(res => res.status === 200 && location.reload())
|
|
|
|
.catch(() => {})
|
|
|
|
await setup(true)
|
|
|
|
resolve()
|
|
|
|
}, retryTime)
|
|
|
|
}
|
|
|
|
ws.onmessage = async ({ data }) => {
|
|
|
|
const payload = JSON.parse(data)
|
|
|
|
if (payload.invalid && lastHref !== location.href) {
|
|
|
|
// Payload can be invalid even if the page does not exist.
|
|
|
|
// So, we need to make sure it exists before reloading.
|
|
|
|
const pageRes = await fetch(location.href, {
|
|
|
|
credentials: 'omit'
|
|
|
|
})
|
|
|
|
if (pageRes.status === 200) {
|
|
|
|
location.reload()
|
|
|
|
} else {
|
|
|
|
lastHref = location.href
|
|
|
|
}
|
2017-07-02 07:29:08 +00:00
|
|
|
}
|
2017-04-18 04:18:43 +00:00
|
|
|
}
|
2018-12-14 11:25:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
await setup()
|
|
|
|
|
|
|
|
async function ping () {
|
|
|
|
if (ws.readyState === ws.OPEN) {
|
|
|
|
ws.send(Router.pathname)
|
2017-02-26 19:45:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 21:26:13 +00:00
|
|
|
let pingerTimeout
|
2017-04-18 04:18:43 +00:00
|
|
|
async function runPinger () {
|
2017-09-08 21:26:13 +00:00
|
|
|
// Will restart on the visibilitychange API below. For older browsers, this
|
|
|
|
// will always be true and will always run, but support is fairly prevalent
|
|
|
|
// at this point.
|
|
|
|
while (!document.hidden) {
|
2017-04-18 04:18:43 +00:00
|
|
|
await ping()
|
2018-12-14 11:25:59 +00:00
|
|
|
await new Promise(resolve => {
|
2017-09-08 21:26:13 +00:00
|
|
|
pingerTimeout = setTimeout(resolve, 5000)
|
|
|
|
})
|
2017-04-18 04:18:43 +00:00
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:25:59 +00:00
|
|
|
document.addEventListener(
|
|
|
|
'visibilitychange',
|
|
|
|
() => {
|
|
|
|
if (!document.hidden) {
|
|
|
|
runPinger()
|
|
|
|
} else {
|
|
|
|
clearTimeout(pingerTimeout)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
false
|
|
|
|
)
|
2017-09-08 21:26:13 +00:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2018-12-14 11:25:59 +00:00
|
|
|
runPinger().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
})
|
2017-09-08 21:26:13 +00:00
|
|
|
}, 10000)
|
2017-04-18 04:18:43 +00:00
|
|
|
}
|