7e7f2c0a6d
* Move client-side dev JS to dev folder * Move eventsource polyfill * Move source-map-support * Move error boundary * Deprecate Container in _app * Make initialRender check better * Remove unused code * Only support one subscription as there is only one * Don’t spread object * Shorten property name * Add container in development too * Simplify query update logic
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
/* global window, location */
|
|
|
|
import fetch from 'unfetch'
|
|
import { getEventSourceWrapper } from './error-overlay/eventsource'
|
|
|
|
let evtSource
|
|
export let currentPage
|
|
|
|
export function closePing () {
|
|
if (evtSource) evtSource.close()
|
|
evtSource = null
|
|
}
|
|
|
|
export function setupPing (assetPrefix, pathnameFn, retry) {
|
|
const pathname = pathnameFn()
|
|
|
|
// Make sure to only create new EventSource request if page has changed
|
|
if (pathname === currentPage && !retry) return
|
|
currentPage = pathname
|
|
// close current EventSource connection
|
|
closePing()
|
|
|
|
const url = `${assetPrefix}/_next/webpack-hmr?page=${currentPage}`
|
|
evtSource = getEventSourceWrapper({ path: url, timeout: 5000, ondemand: 1 })
|
|
|
|
evtSource.addMessageListener(event => {
|
|
if (event.data.indexOf('{') === -1) return
|
|
try {
|
|
const payload = JSON.parse(event.data)
|
|
if (payload.invalid) {
|
|
// Payload can be invalid even if the page does not exist.
|
|
// So, we need to make sure it exists before reloading.
|
|
fetch(location.href, {
|
|
credentials: 'same-origin'
|
|
}).then(pageRes => {
|
|
if (pageRes.status === 200) {
|
|
location.reload()
|
|
}
|
|
})
|
|
}
|
|
} catch (err) {
|
|
console.error('on-demand-entries failed to parse response', err)
|
|
}
|
|
})
|
|
}
|