forked from bogas04/SikhJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
49 lines (41 loc) · 1.33 KB
/
service-worker.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
const VERSION = 'v20';
const RUNTIME = 'runtime_19';
const currentCaches = [ RUNTIME, VERSION ];
const CACHE_URLS = [
'./',
'./index.html',
'./assets/fonts/gurmukhi_heavy.ttf',
'./assets/js/main.js',
'./assets/js/vendor.js',
];
const deleteCache = c => caches.delete(c);
const deleteCaches = cs => Promise.all(cs.map(deleteCache));
const findCachesToDelete = cs => cs.filter(c => !currentCaches.includes(c));
const isSameOrigin = event => event.request.url.startsWith(self.location.origin);
const updateCacheFromNetwork = event => cachedResponse => cachedResponse || caches
.open(RUNTIME)
.then(cache => fetch(event.request)
.then(response => cache
.put(event.request, response.clone())
.then(() => response)
)
);
const onInstall = event => event.waitUntil(
caches
.open(VERSION)
.then(cache => cache.addAll(CACHE_URLS))
.then(self.skipWaiting())
);
const onActivate = event => event.waitUntil(caches
.keys()
.then(findCachesToDelete)
.then(deleteCaches)
.then(() => self.clients.claim())
);
const onFetch = event => isSameOrigin(event)
? event.respondWith(caches.match(event.request).then(updateCacheFromNetwork(event)))
: null // TODO: Cache khajana API
;
self.addEventListener('activate', onActivate);
self.addEventListener('fetch', onFetch);
self.addEventListener('install', onInstall);