-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyWorker.js
58 lines (53 loc) · 1.63 KB
/
myWorker.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
var cachepages = [
"/",
"/index",
"/index.html",
"/bus",
"/bus.html"
];
var version = '2.1.2';
this.addEventListener('install', function(event) {
event.waitUntil(
caches.open(version).then(function(cache) {
return cache.addAll(cachepages);
})
);
});
this.addEventListener('fetch', function(event) {
var cacheWhitelist = [version];
event.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (cacheWhitelist.indexOf(key) === -1) {
return caches.delete(key)
}
}))
})
)
event.respondWith(
caches.match(event.request)
.then(
function (response) {
if (response) {
this.registration.update();
return response;
}
let fetchRequest = event.request.clone();
return fetch(fetchRequest)
.then((response) => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
let responseToCache = response.clone();
caches.open(version)
.then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
});
})
);
});
this.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim());
});