<commit_before>/* global location */

import Router from '../lib/router'
import fetch from 'unfetch'

export default () => {
  Router.ready(() => {
    Router.router.events.on('routeChangeComplete', ping)
  })

  async function ping () {
    try {
      const url = `/_next/on-demand-entries-ping?page=${Router.pathname}`
      const res = await fetch(url, {
        credentials: 'same-origin'
      })
      const payload = await res.json()
      if (payload.invalid) {
        // Payload can be invalid even if the page is not exists.
        // So, we need to make sure it's exists before reloading.
        const pageRes = await fetch(location.href, {
          credentials: 'same-origin'
        })
        if (pageRes.status === 200) {
          location.reload()
        }
      }
    } catch (err) {
      console.error(`Error with on-demand-entries-ping: ${err.message}`)
    }
  }

  async function runPinger () {
    while (true) {
      await new Promise((resolve) => setTimeout(resolve, 5000))
      await ping()
    }
  }

  runPinger()
    .catch((err) => {
      console.error(err)
    })
}<commit_msg>Tie page visibility api to on demand pinger (#2818)<commit_after>/* global location */

import Router from '../lib/router'
import fetch from 'unfetch'

export default () => {
  Router.ready(() => {
    Router.router.events.on('routeChangeComplete', ping)
  })

  async function ping () {
    try {
      const url = `/_next/on-demand-entries-ping?page=${Router.pathname}`
      const res = await fetch(url, {
        credentials: 'same-origin'
      })
      const payload = await res.json()
      if (payload.invalid) {
        // Payload can be invalid even if the page is not exists.
        // So, we need to make sure it's exists before reloading.
        const pageRes = await fetch(location.href, {
          credentials: 'same-origin'
        })
        if (pageRes.status === 200) {
          location.reload()
        }
      }
    } catch (err) {
      console.error(`Error with on-demand-entries-ping: ${err.message}`)
    }
  }

  let pingerTimeout
  async function runPinger () {
    // 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) {
      await ping()
      await new Promise((resolve) => {
        pingerTimeout = setTimeout(resolve, 5000)
      })
    }
  }

  document.addEventListener('visibilitychange', () => {
    if (!document.hidden) {
      runPinger()
    } else {
      clearTimeout(pingerTimeout)
    }
  }, false)

  setTimeout(() => {
    runPinger()
      .catch((err) => {
        console.error(err)
      })
  }, 10000)
}
{'fraction_non_alphanumeric': 0.13401684364701574, 'fraction_numerical': 0.008421823507872574, 'mean_word_length': 2.248513674197384, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5430313', 'n_tokens_mistral': 843, 'n_tokens_neox': 805, 'n_words': 221}