-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
sw-runner.js.html
45 lines (39 loc) · 1.12 KB
/
sw-runner.js.html
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
/* jshint esversion: 6 */
const cacheName = '{{version}}-v1';
const scripts = [
'/runner',
'{{static}}/js/prod/runner-{{version}}.min.js',
];
self.addEventListener('install', e => {
// once the SW is installed, go ahead and fetch the resources to make this
// work offline
e.waitUntil(
caches.open(cacheName).then(cache => {
const fetches = scripts.map(req => {
return fetch(req, { mode: 'no-cors' }).then(res => {
return cache.put(req, res);
});
});
return Promise.all(fetches).then(() => self.skipWaiting());
})
);
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(names => {
// remove anything that isn't our bins or active static cache
const keep = [cacheName];
const trash = names.filter(
name => !keep.includes(name)
).map(
name => caches.delete(name)
);
return Promise.all(trash).then(() => self.clients.claim());
})
);
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(res => res || fetch(event.request))
);
});