a27c235260
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.
24 lines
623 B
JavaScript
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()
|
|
}
|
|
})
|
|
}
|