This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sw.js
61 lines (54 loc) · 1.49 KB
/
sw.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
let log = console.log.bind(console);
let err = console.error.bind(console);
let version = '1';
let cacheName = 'pwa-client-v' + version;
let dataCacheName = 'pwa-client-data-v' + version;
let appShellFilesToCache = [
'./',
'./index.html',
// './inline.bundle.js',
// './polyfills.bundle.js',
// './styles.bundle.js',
// './vendor.bundle.js',
// './main.bundle.js',
'./manifest.json'
];
self.addEventListener('install', (ev) => {
ev.waitUntil(self.skipWaiting());
log('Service Worker: Installed');
ev.waitUntil(
caches.open(cacheName).then((cache) => {
log('Service Worker: Caching App Shell');
return cache.addAll(appShellFilesToCache);
})
);
});
self.addEventListener('activate', (ev) => {
ev.waitUntil(self.clients.claim());
log('Service Worker: Active');
ev.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key !== cacheName) {
log('Service Worker: Removing old cache', key);
return caches.delete(key);
}
}));
})
);
});
self.addEventListener('fetch', (e) => {
log('Service Worker: Fetch URL ', e.request.url);
// Match requests for data and handle them separately
e.respondWith(
caches.match(e.request.clone()).then((response) => {
return response || fetch(e.request.clone()).then((r2) => {
return caches.open(dataCacheName).then((cache) => {
console.log('Service Worker: Fetched & Cached URL ', e.request.url);
cache.put(e.request.url, r2.clone());
return r2.clone();
});
});
})
);
});