-
Notifications
You must be signed in to change notification settings - Fork 6
/
sw.js
38 lines (34 loc) · 1016 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
const CACHE_NAME = "ruffle-v1";
const APP_SHELL_FILES = [
"./",
"./main.js",
"./elements.js",
"./icons/favicon-32.png",
"./icons/favicon-64.png",
"./icons/favicon-180.png"
];
const CONTENT_TO_CACHE = APP_SHELL_FILES;
self.addEventListener("install", (e) => {
e.waitUntil((async () => {
let cache = await caches.open(CACHE_NAME);
await cache.addAll(CONTENT_TO_CACHE);
})());
});
self.addEventListener('fetch', (e) => {
e.respondWith((async () => {
let r = await caches.match(e.request);
if (r) return r;
let response = await fetch(e.request);
let cache = await caches.open(CACHE_NAME);
cache.put(e.request, response.clone());
return response;
})());
});
self.addEventListener('activate', (e) => {
e.waitUntil(caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key === CACHE_NAME) { return; }
return caches.delete(key);
}))
}));
});