-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
51 lines (48 loc) · 1.49 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
var staticCacheName = "pwa-v" + new Date().getTime();
var filesToCache = [
'public/frontend/css/app.css',
'public/frontend/js/app.js',
'public/images/ico/favicon-72x72.png',
'public/images/ico/favicon-96x96.png',
'public/images/ico/favicon-128x128.png',
'public/images/ico/favicon-144x144.png',
'public/images/ico/favicon-152x152.png',
'public/images/ico/favicon-192x192.png',
'public/images/ico/favicon-384x384.png',
'public/images/ico/favicon-512x512.png',
];
// Cache on install
self.addEventListener("install", event => {
this.skipWaiting();
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
)
});
// Clear cache on activate
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames
.filter(cacheName => (cacheName.startsWith("pwa-")))
.filter(cacheName => (cacheName !== staticCacheName))
.map(cacheName => caches.delete(cacheName))
);
})
);
});
// Serve from Cache
self.addEventListener("fetch", event => {
event.respondWith(
caches.match(event.request)
.then(response => {
return response || fetch(event.request);
})
.catch(() => {
return caches.match('offline');
})
)
});