-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.worker.js
62 lines (56 loc) · 2.19 KB
/
service.worker.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
59
60
61
62
// Version 1.2.2 - update version to ensure service worker is bitwise different than previous one and it gets reinstalled leading to cache update in installation script
const cacheName = "CurriculumVitaTorstenKnauf";
const pathnamesToCache = [
"/app.webmanifest",
"/",
"/index.html",
"/assets/applicationPhoto.jpg",
"/assets/favicon.ico",
"/assets/QRCodeToWebPage.svg",
"/assets/splashScreen.png",
"/css/browserReset.css",
"/css/styles.css",
"/css/styles.desktop.css",
"/css/styles.print.css",
"/css/theme.css",
"/css/theme-switcher.css",
"/src/applicationPhoto.component.js",
"/src/contentEntry.component.js",
"/src/contentSection.component.js",
];
const basePath = location.host.includes("github.io") ? "/curriculum-vitae" : "";
const servedPathnamesToCache = pathnamesToCache.map(path => basePath + path);
self.addEventListener("install", event => {
fillCache(event);
// Force update to immediately present latest version.
// See https://whatwebcando.today/articles/handling-service-worker-updates for more sophisticated approaches
self.skipWaiting();
function fillCache(event) {
event.waitUntil(caches.delete(cacheName)
.then(() => caches.open(cacheName))
.then(cache => cache.addAll(servedPathnamesToCache))
);
}
});
self.addEventListener('activate', async () => {
const openedTabs = await self.clients.matchAll({type: 'window'})
openedTabs.forEach((tab) => {
tab.navigate(tab.url) // reload to ensure cached files weren't already served before activation
})
})
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
if (url.origin === location.origin && servedPathnamesToCache.includes(url.pathname)) {
return event.respondWith(replyFromCache(url.pathname, event.request));
}
async function replyFromCache(pathname, request) {
const cachedResponse = await caches.match(pathname);
if (cachedResponse !== undefined) {
return cachedResponse;
}
const response = await fetch(request);
const clonedResponse = response.clone();
caches.open(cacheName).then(cache => cache.put(pathname, clonedResponse)); // don't await to response as fast as possible to the user
return response;
}
});