rsnext/packages/next/client/on-demand-entries-client.js
JJ Kasper a27c235260 Update to share HMR and ondemand SSE connection (#7084)
We also close the connection when the window is in the background and re-connect when it is brought to the foreground. This prevents us from using up too many connections.
2019-04-22 05:51:09 +09:00

24 lines
623 B
JavaScript

/* global window */
import Router from 'next/router'
import { setupPing, currentPage, closePing } from './on-demand-entries-utils'
export default async ({ assetPrefix }) => {
Router.ready(() => {
Router.events.on(
'routeChangeComplete',
setupPing.bind(this, assetPrefix, () => Router.pathname)
)
})
setupPing(assetPrefix, () => Router.pathname, currentPage)
document.addEventListener('visibilitychange', event => {
const state = document.visibilityState
if (state === 'visible') {
setupPing(assetPrefix, () => Router.pathname, true)
} else {
closePing()
}
})
}