-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
49 lines (46 loc) · 1.32 KB
/
serviceworker.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
const version = "0.0.3",
preCache = "PRECACHE-" + version,
cacheList = [
"/",
"style/default.css",
"style/cards.css",
"js/application.js",
"js/github.js",
"js/storage.js",
"js/templates.js",
"js/serviceworker-register.js"
];
/* Service Worker Event Handlers */
self.addEventListener("install", function(event) {
console.log("Installing the service worker!");
self.skipWaiting();
caches.open(preCache)
.then(cache => {
cache.addAll(cacheList);
});
});
self.addEventListener("activate", function(event) {
event.waitUntil(
caches.keys()
.then(cacheNames => {
cacheNames.forEach(value => {
if(value.indexOf(version) < 0 ) {
caches.delete(value);
}
});
console.log("service worker activated");
return;
})
);
});
self.addEventListener("fetch", function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response;
}
return fetch(event.request);
})
);
});