-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
49 lines (46 loc) · 1.5 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
var staticCacheName = "the-shop-pwa-" + new Date().getTime();
var filesToCache = [
'/public/web-assets/img/icons/icon-72x72.png',
'/public/web-assets/img/icons/icon-96x96.png',
'/public/web-assets/img/icons/icon-128x128.png',
'/public/web-assets/img/icons/icon-144x144.png',
'/public/web-assets/img/icons/icon-152x152.png',
'/public/web-assets/img/icons/icon-192x192.png',
'/public/web-assets/img/icons/icon-384x384.png',
'/public/web-assets/img/icons/icon-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("the-shop-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');
})
)
});