-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
48 lines (43 loc) · 958 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
43
44
45
46
47
48
/*global caches, fetch, Promise */
(function (worker) {
"use strict";
var PREFIX = 'tts',
VERSION = '1.3',
FILES = [
'app.css',
'app.js',
'index.html',
'speaker.js',
'img/icon512.png',
'img/pause.svg',
'img/play.svg',
'img/stop.svg',
'pdf/pdf.js',
'pdf/pdf.worker.js'
];
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);