function registerValidSW()

in src/registerServiceWorker.tsx [65:124]


function registerValidSW (swUrl: string, toasterSupplier: () => (Toaster | null)) {
  navigator.serviceWorker
    .register(swUrl)
    .then((registration) => {
      registration.onupdatefound = () => {
        const installingWorker = registration.installing
        if (installingWorker) {
          installingWorker.onstatechange = () => {
            if (installingWorker.state === 'installed') {
              if (navigator.serviceWorker.controller) {
                // At this point, the old content will have been purged and
                // the fresh content will have been added to the cache.
                // It's the perfect time to display a "New content is
                // available; please refresh." message in your web app.
                const toaster = toasterSupplier()
                if (toaster) {
                  toaster.show({
                    action: {
                      icon: 'refresh',
                      onClick: () => window.location.reload(),
                      text: <strong>Reload</strong>
                    },
                    icon: 'outdated',
                    intent: Intent.PRIMARY,
                    message: (
                      <React.Fragment>
                        The page has been updated in the background and new content is available.
                      </React.Fragment>
                    )
                  })
                }
              } else {
                // At this point, everything has been precached.
                // It's the perfect time to display a
                // "Content is cached for offline use." message.
                const toaster = toasterSupplier()
                if (toaster) {
                  toaster.show({
                    icon: 'info-sign',
                    intent: Intent.PRIMARY,
                    message: <React.Fragment>Content is cached for offline use.</React.Fragment>
                  })
                }
              }
            }
          }
        }
      }
    })
    .catch((error) => {
      const toaster = toasterSupplier()
      if (toaster) {
        toaster.show({
          icon: 'error',
          intent: Intent.DANGER,
          message: <React.Fragment>Could not register service worker:{error}</React.Fragment>
        })
      }
    })
}