-
Notifications
You must be signed in to change notification settings - Fork 6
/
service-worker.js
83 lines (77 loc) · 2.42 KB
/
service-worker.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Welcome to your Workbox-powered service worker!
*
* You'll need to register this file in your web app and you should
* disable HTTP caching for this file too.
* See https://goo.gl/nhQhGp
*
* The rest of the code is auto-generated. Please don't update this file
* directly; instead, make changes to your Workbox build configuration
* and re-run your build process.
* See https://goo.gl/2aRDsh
*/
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/3.6.3/workbox-sw.js'
);
importScripts(
'/' // Add precache
);
workbox.core.setCacheNameDetails({ prefix: 'd4' });
//Change this value every time before you build
const LATEST_VERSION = 'v1.0';
self.addEventListener('activate', event => {
console.log(`%c ${LATEST_VERSION} `, 'background: #ddd; color: #0000ff');
if (caches) {
caches.keys().then(arr => {
arr.forEach(key => {
if (key.indexOf('d4-precache') < -1) {
caches
.delete(key)
.then(() =>
console.log(
`%c Cleared ${key}`,
'background: #333; color: #ff0000'
)
);
} else {
caches.open(key).then(cache => {
cache.match('version').then(res => {
if (!res) {
cache.put(
'version',
new Response(LATEST_VERSION, {
status: 200,
statusText: LATEST_VERSION
})
);
} else if (res.statusText !== LATEST_VERSION) {
caches
.delete(key)
.then(() =>
console.log(
`%c Cleared Cache ${LATEST_VERSION}`,
'background: #333; color: #ff0000'
)
);
} else
console.log(
`%c Great you have the latest version ${LATEST_VERSION}`,
'background: #333; color: #00ff00'
);
});
});
}
});
});
}
});
workbox.skipWaiting();
workbox.clientsClaim();
/**
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
* requests for URLs in the manifest.
* See https://goo.gl/S9QRab
*/
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});