-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
54 lines (47 loc) · 1.46 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
!(function startExpressService (root) {
'use strict'
if (!root.navigator) {
console.error('Missing navigator')
return
}
if (!root.navigator.serviceWorker) {
console.error('Sorry, not ServiceWorker feature, maybe enable it?')
console.error('http://jakearchibald.com/2014/using-serviceworker-today/')
return
}
function getCurrentScriptFolder () {
var scriptEls = document.getElementsByTagName('script')
var thisScriptEl = scriptEls[scriptEls.length - 1]
var scriptPath = thisScriptEl.src
return scriptPath.substr(0, scriptPath.lastIndexOf('/') + 1)
}
var serviceScriptUrl = getCurrentScriptFolder() + 'express-service.js'
var scope = './'
/**
*
* @param registration {ServiceWorkerRegistration}
*/
function registeredWorker (registration) {
console.log('express-service registered...')
const sw = registration.installing || registration.waiting || registration.active;
if (sw) {
sw.addEventListener('statechange', function(e) {
if( e.target.state === "activated" ) {
// let the Express take over, even the index page
setTimeout( () => window.location.reload(), 2000 );
}
})
}
}
function onError (err) {
if (err.message.indexOf('missing active') !== -1) {
// the service worker is installed
window.location.reload()
} else {
console.error('express service worker error', err)
}
}
root.navigator.serviceWorker.register(serviceScriptUrl, { scope: scope })
.then(registeredWorker)
.catch(onError)
}(window));