-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
42 lines (37 loc) · 851 Bytes
/
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
/*global caches, fetch, Promise */
(function (worker) {
"use strict";
var PREFIX = 'fall',
VERSION = '2.3',
FILES = [
'app.js',
'index.html',
'scream.ogg',
'xkcd_phone.png'
];
worker.addEventListener('install', function (e) {
e.waitUntil(
caches.open(PREFIX + ':' + VERSION).then(function (cache) {
return cache.addAll(FILES);
})
);
});
worker.addEventListener('activate', function (e) {
e.waitUntil(
caches.keys().then(function (keys) {
return Promise.all(keys.map(function (key) {
if (key.indexOf(PREFIX + ':') === 0 && key !== PREFIX + ':' + VERSION) {
return caches.delete(key);
}
}));
})
);
});
worker.addEventListener('fetch', function (e) {
e.respondWith(caches.match(e.request, {ignoreSearch: true})
.then(function (response) {
return response || fetch(e.request);
})
);
});
})(this);