clientEntry.js 1.6 KB
Newer Older
U
ULIVZ 已提交
1
/* global BASE_URL, GA_ID, ga, SW_ENABLED, VUEPRESS_VERSION, LAST_COMMIT_HASH*/
E
Evan You 已提交
2

E
wip  
Evan You 已提交
3
import { createApp } from './app'
4
import SWUpdateEvent from './SWUpdateEvent'
E
Evan You 已提交
5
import { register } from 'register-service-worker'
E
wip  
Evan You 已提交
6 7

const { app, router } = createApp()
E
init  
Evan You 已提交
8

U
ULIVZ 已提交
9 10 11 12 13
window.__VUEPRESS_VERSION__ = {
  version: VUEPRESS_VERSION,
  hash: LAST_COMMIT_HASH
}

E
init  
Evan You 已提交
14 15
router.onReady(() => {
  app.$mount('#app')
E
Evan You 已提交
16

17
  // TODO Separate it into a plugin ('vuepress-plugin-pwa')
E
Evan You 已提交
18
  // Register service worker
E
Evan You 已提交
19 20 21
  if (process.env.NODE_ENV === 'production' &&
    SW_ENABLED &&
    window.location.protocol === 'https:') {
E
Evan You 已提交
22 23
    register(`${BASE_URL}service-worker.js`, {
      ready () {
E
Evan You 已提交
24
        console.log('[vuepress:sw] Service worker is active.')
E
Evan You 已提交
25 26
        app.$refs.layout.$emit('sw-ready')
      },
27
      cached (registration) {
E
Evan You 已提交
28
        console.log('[vuepress:sw] Content has been cached for offline use.')
29
        app.$refs.layout.$emit('sw-cached', new SWUpdateEvent(registration))
E
Evan You 已提交
30
      },
31
      updated (registration) {
E
Evan You 已提交
32
        console.log('[vuepress:sw] Content updated.')
33
        app.$refs.layout.$emit('sw-updated', new SWUpdateEvent(registration))
E
Evan You 已提交
34 35
      },
      offline () {
E
Evan You 已提交
36
        console.log('[vuepress:sw] No internet connection found. App is running in offline mode.')
E
Evan You 已提交
37 38 39
        app.$refs.layout.$emit('sw-offline')
      },
      error (err) {
E
Evan You 已提交
40
        console.error('[vuepress:sw] Error during service worker registration:', err)
E
Evan You 已提交
41 42 43 44 45 46 47 48 49 50
        app.$refs.layout.$emit('sw-error', err)
        if (GA_ID) {
          ga('send', 'exception', {
            exDescription: err.message,
            exFatal: false
          })
        }
      }
    })
  }
E
init  
Evan You 已提交
51
})