-
Notifications
You must be signed in to change notification settings - Fork 31
/
registerServiceWorker.js
52 lines (47 loc) · 1.67 KB
/
registerServiceWorker.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
/* eslint-disable no-var */
'use strict'
var noop = function () {}
var SW_URL = '/service-worker.js'
/* eslint-disable no-console */
exports.register = function (options) {
options = options || {}
var first = options.first || noop
var renovate = options.renovate || noop
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker
.register(SW_URL)
.then(function (registration) {
registration.onupdatefound = function () {
var installingWorker = registration.installing
installingWorker.onstatechange = function () {
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.
renovate()
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
first()
}
}
}
}
})
.catch(function (error) {
console.error('Error during service worker registration:', error)
})
})
}
}
exports.unregister = function () {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(function (registration) {
registration.unregister()
})
}
}