-
Notifications
You must be signed in to change notification settings - Fork 419
/
sw.caches.js
39 lines (37 loc) · 995 Bytes
/
sw.caches.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
self.addEventListener("activate", function (event) {
const current = ["{{ theme_alias }}"];
event.waitUntil(
caches.keys().then(function (keyList) {
return Promise.all(
keyList.map(function (key) {
if (current.indexOf(key) === -1) {
return caches.delete(key);
}
})
);
})
);
});
self.addEventListener("fetch", function (e) {
if (e.request.url.match("{{ theme_alias }}")) {
e.respondWith(
caches.match(e.request).then(function (resp) {
if (resp !== undefined) {
return resp;
} else {
return fetch(e.request, {
cache: "no-store",
})
.then(function (resp) {
let clone = resp.clone();
caches.open("{{ theme_alias }}").then(function (cache) {
cache.put(e.request, clone);
});
return resp;
})
.catch(console.log);
}
})
);
}
});