sverweij/mscgen_js

View on GitHub
src/script/interpreter/register-service-worker.js

Summary

Maintainability
A
0 mins
Test Coverage
if ('serviceWorker' in navigator) {
  // Delay registration until after the page has loaded, to ensure that our
  // precaching requests don't degrade the first visit experience.
  // See https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/registration
  window.addEventListener('load', function () {
    navigator.serviceWorker.register('service-worker.js').then(function (pRegistration) {
      // updatefound is fired if service-worker.js changes.
      pRegistration.onupdatefound = function () {
        // The updatefound event implies that pRegistration.installing is set; see
        // https://w3c.github.io/ServiceWorker/#service-worker-registration-updatefound-event
        var installingWorker = pRegistration.installing

        installingWorker.onstatechange = function () {
          switch (installingWorker.state) {
            case 'installed':
              if (navigator.serviceWorker.controller) {
                console.log('New or updated content is available.')
              } else {
                console.log('Content is now available offline!')
              }
              break

            case 'redundant':
              console.error('The installing service worker became redundant.')
              break
          }
        }
      }
    }).catch(function (e) {
      console.error('Error during service worker registration:', e)
    })
  })
}