generated from 11ty/eleventy-base-blog
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceworker.js
58 lines (54 loc) · 1.45 KB
/
serviceworker.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
const version = "V3.04",
staticCacheName = `${version}staticfiles`;
addEventListener("install", (installEvent) => {
skipWaiting();
installEvent.waitUntil(
caches.open(staticCacheName).then((staticCache) => {
// Nice to Cache
staticCache.addAll([
"404.html",
"index.html",
"about/index.html",
"portfolio/index.html",
"blog/index.html",
"contact/index.html",
"/assets/images/global/favicon.ico",
"/assets/images/global/headshot-2.jpg",
"/assets/images/icons/icon-192.png",
"/assets/images/icons/icon-512.png",
]);
// Need to Cache
return staticCache.addAll([
"/assets/css/main.css",
"/assets/javascript/main.js",
]);
})
);
});
// For clearing out old caches
addEventListener("activate", (activateEvent) => {
activateEvent.waitUntil(
// Clean the old caches up
caches
.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== staticCacheName) {
// Time to kill!
return caches.delete(cacheName);
}
})
);
})
.then(() => clients.claim())
);
});
addEventListener("fetch", (fetchEvent) => {
const request = fetchEvent.request;
fetchEvent.respondWith(
caches.match(request).then((responseFromCache) => {
return responseFromCache || fetch(request);
})
);
});