-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
68 lines (57 loc) · 1.54 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
59
60
61
62
63
64
65
66
67
const cacheName = 'Einheitenrechner';
const staticAssets = [
'.',
'index.html',
'app.js',
'./style/app.css',
'manifest.json',
'icons/'
];
self.addEventListener('install',evt => {
console.log("[Service Worker] Install");
evt.waitUntil(
caches.open(cacheName)
.then((cache)=>{
console.log("[Service Worker] Pre-caching offline page");
return cache.addAll(staticAssets);
})
);
self.skipWaiting();
});
self.addEventListener('activate', (evt) => {
console.log("[Service Worker] Activate");
evt.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key !== cacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);;
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response;
}
return fetch(event.request).then(
function(response) {
if(!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
var responseToCache = response.clone();
caches.open(cacheName)
.then(function(cache) {
cache.put(event.request, responseToCache);
});
return response;
}
);
})
);
});