-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
62 lines (59 loc) · 1.83 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
var staticCacheName = "pwa-v" + new Date().getTime();
var filesToCache = [
'/offline',
'/css/app.css',
'/js/app.js',
'/images/icons/icon-72x72.png',
'/images/icons/icon-96x96.png',
'/images/icons/icon-128x128.png',
'/images/icons/icon-144x144.png',
'/images/icons/icon-152x152.png',
'/images/icons/icon-192x192.png',
'/images/icons/icon-384x384.png',
'/images/icons/icon-512x512.png',
'/images/icons/splash-640x1136.png',
'/images/icons/splash-750x1334.png',
'/images/icons/splash-1242x2208.png',
'/images/icons/splash-1125x2436.png',
'/images/icons/splash-828x1792.png',
'/images/icons/splash-1242x2688.png',
'/images/icons/splash-1536x2048.png',
'/images/icons/splash-1668x2224.png',
'/images/icons/splash-1668x2388.png',
'/images/icons/splash-2048x2732.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');
})
)
});