-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
40 lines (39 loc) · 1.07 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
// not used
const lastVersion = "v2.1";
const currentVersion = "v2.2";
self.addEventListener("install", (event) => {
event.waitUntil(caches.delete(lastVersion));
self.skipWaiting();
// event.waitUntil(
// caches.open(version).then((cache) => {
// for (const asset of coreAssets) {
// cache.add(new Request(asset));
// }
// return cache;
// })
// );
});
self.addEventListener("fetch", (event) => {
const request = event.request;
// Offline-first
if (
request.headers.get("Accept").includes("text/html") ||
request.headers.get("Accept").includes("text/css") ||
request.headers.get("Accept").includes("text/javascript") ||
request.headers.get("Accept").includes("image")
) {
event.respondWith(
caches.match(request).then((response) => {
return (
response ??
fetch(request).then((response) => {
caches.open(currentVersion).then((cache) => {
cache.put(request, response.clone());
});
return response;
})
);
})
);
}
});