-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
70 lines (67 loc) · 1.8 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*globals self, caches, fetch, console */
import { MusicDB } from "./musicdb.js";
import { DatabaseLoadingMessages } from "./actions.js";
import {
APP_CACHE_NAME,
IMAGES_CACHE_NAME,
FLAC_WORKER_CACHE_NAME,
urlsToCache
} from "./swConstants.js";
self.addEventListener("activate", event => {
var cacheWhitelist = [
APP_CACHE_NAME,
IMAGES_CACHE_NAME,
FLAC_WORKER_CACHE_NAME
];
event.waitUntil(
Promise.all([
event.waitUntil(self.clients.claim()),
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
])
);
});
self.addEventListener("install", event => {
const cachesPromises = [];
self.skipWaiting();
Object.entries(urlsToCache).forEach(([cacheName, urls]) => {
cachesPromises.push(
event.waitUntil(
caches.open(cacheName).then(cache => {
return cache.addAll(urls);
})
)
);
});
return Promise.all(cachesPromises);
});
self.addEventListener("fetch", event => {
event.respondWith(
caches.match(event.request).then(response => {
if (response) {
// console.log("SW: fetch serving from cache", event, response);
return response;
}
if (/\/art$/.test(event.request.url)) {
return fetch(event.request).then(response => {
if (!response.ok) {
return response;
}
return caches.open(IMAGES_CACHE_NAME).then(cache => {
cache.put(event.request, response.clone());
return response;
});
});
}
// console.log("SW: fetch cache miss, forwarding", event);
return fetch(event.request);
})
);
});