forked from tjy-gitnub/win12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
51 lines (50 loc) · 1.21 KB
/
sw.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
let version = 'v3.4.1';
this.addEventListener('fetch', function (event) {
event.respondWith(
caches.match(event.request).then(res => {
return res ||
fetch(event.request)
.then(responese => {
const responeseClone = responese.clone();
caches.open(version).then(cache => {
cache.put(event.request, responeseClone);
})
return responese;
})
.catch(err => {
console.log(err);
});
})
)
});
const cacheNames = [version];
let flag = false;
this.addEventListener('activate', function (event) {
event.waitUntil(
caches.keys().then(keys => {
return Promise.all[keys.map(key => {
if (!cacheNames.includes(key)) {
flag = true;
return caches.delete(key);
}
})]
})
);
event.waitUntil(
caches.open(version).then(function (cache) {
return cache.addAll([
'apps/icons/explorer/disk.png',
'apps/icons/explorer/diskwin.png',
'apps/icons/explorer/folder.png'
]);
})
);
});
this.addEventListener('message', function (e) {
if (e.data == 'update?') {
if (flag) {
e.source.postMessage('update');
flag = false;
}
}
});