-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
53 lines (46 loc) · 1.25 KB
/
sw.js
File metadata and controls
53 lines (46 loc) · 1.25 KB
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
const CACHE_VERSION = 'v2.1.1';
const CACHE_NAME = `demucsstems-${CACHE_VERSION}`;
const FILES_TO_CACHE = [
'./',
'./index.html',
'./manifest.json',
'./player-192x192.PNG',
'./player-512x512.PNG'
];
self.addEventListener('install', (event) => {
console.log('[ServiceWorker] Install');
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('[ServiceWorker] Caching app shell');
return cache.addAll(FILES_TO_CACHE);
})
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
console.log('[ServiceWorker] Activate');
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key !== CACHE_NAME) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
.then(() => self.clients.claim())
);
});
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
if (url.hostname.endsWith('.ts.net') || event.request.method === 'POST') {
return;
}
event.respondWith(
caches.match(event.request)
.then((response) => {
return response || fetch(event.request);
})
);
});