-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserviceworker.js
72 lines (67 loc) · 1.8 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
68
69
70
71
72
//########################################
// serviceworker for Simple App Shell
// Version: 10.11.2024 15:50
//########################################
const cacheName = 'simple_appshell_24_11_10_15_50';
const urlsToCache = [
'/',
'/index.htm',
'/apple-touch-icon.png',
'/favicon.ico',
'/icon_144.png',
'/icon_192.png',
'/icon_196.png',
'/icon_512.png',
'/icon_maskable.png',
'/screenshot1.png',
'/screenshot2.png',
'/manifest.json'
];
//########################################
//delete old cache
self.addEventListener('activate', function(event) {
//console.log('SW activate');
event.waitUntil(
caches.keys().then(function(keys) {
return Promise.all(keys.map(function(key) {
if (key !== cacheName) {
//console.log('Delete cache: ', key);
return caches.delete(key);
}
}));
}).then(function() {
//console.log('SW claim', cacheName);
return self.clients.claim();
})
);
});
//########################################
//setup the cache
self.addEventListener('install', function(event) {
//console.log('SW install');
event.waitUntil(
caches.open(cacheName).then(function(cache) {
//console.log('Open cache');
return cache.addAll(urlsToCache);
}).then(function() {
//console.log('Skip waiting');
return self.skipWaiting();
})
);
});
//########################################
//fetch the cache
self.addEventListener('fetch', function(event) {
//console.log('SW fetch: ', event.request.url);
event.respondWith(
caches.match(event.request).then(function(pResponse) {
if (pResponse) {
//console.log('Load from cache: ', event.request.url);
return pResponse;
} else {
//console.log('Load from network: ', event.request.url);
return fetch(event.request);
}
})
);
});