Skip to content
Snippets Groups Projects
registerServiceWorker.js 1.51 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* eslint-disable no-console */
    
    import { register } from 'register-service-worker'
    
    import store from './store'
    
    if (process.env.NODE_ENV === 'production') {
      register(`${process.env.BASE_URL}service-worker.js`, {
    
        registrationOptions: { scope: '/' },
    
            'App is being served from cache by a service worker.'
    
        },
        registered (registration) {
          console.log('Service worker has been registered.')
    
          // check for updates every 2 hours
          var checkInterval = 1000 * 60 * 60 * 2
          // var checkInterval = 1000 * 5
    
          registration.update();
    
          setInterval(() => {
            console.log('Checking for service worker update…')
            registration.update();
          }, checkInterval);
    
          store.commit('ui/serviceWorker', {registration: registration})
          if (registration.active) {
            registration.active.postMessage({command: 'serverChosen', serverUrl: store.state.instance.instanceUrl})
          }
        },
    
        cached () {
          console.log('Content has been cached for offline use.')
        },
        updatefound () {
          console.log('New content is downloading.')
        },
        updated (registration) {
          console.log('New content is available; please refresh!')
          store.commit('ui/serviceWorker', {updateAvailable: true, registration: registration})
        },
        offline () {
          console.log('No internet connection found. App is running in offline mode.')
        },
        error (error) {
          console.error('Error during service worker registration:', error)
        }
      })
    }