-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
53 lines (53 loc) · 1.2 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
52
53
self.addEventListener('message', function (e) {
var s = e.data;
if (!s || s.updateSw) {
self.skipWaiting();
return;
}
var type = s.type;
s = s.s;
if (type === "install") {
caches.open(s.cacheName).then(function (c) {
for (var nm in s.cacheFiles) {
s.cacheFiles[nm].forEach(function (f) {
c.match(nm + "/" + f).then(function (cR) {
if (!cR)
c.add(nm + "/" + f);
});
});
}
});
return;
}
if (type === "update") {
caches.open(s.cacheName).then(function (c) {
var cf = [];
for (var nm in s.cacheFiles) {
s.cacheFiles[nm].forEach(function (f) {
cf.push(c.add(nm + "/" + f));
});
}
return Promise.all(cf);
}).then(function () {
e.source.postMessage({
type: "reload"
});
});
return;
}
});
var fn = function (e) {
console.info('[ServiceWorker] Fetch', e.request.url);
e.respondWith(
caches.match("@:precacheJS_cacheName_").then(function (r) {
return r.text();
}).then(function (t) {
return caches.open(t).then(function (c) {
return c.match(e.request).then(function (response) {
return response || fetch(e.request);
});
})
}));
};
self.removeEventListener('fetch', fn);
self.addEventListener('fetch', fn);