forked from NeverDecaf/discord-PWA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord-pwa-sw.js
35 lines (33 loc) · 947 Bytes
/
discord-pwa-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
var cacheName = 'discord-pwa';
var filesToCache = [
'./',
'./index.html',
'./css/style.css',
'./css/client.css',
'./js/main.js',
'./PWA_Install_Button.png'
];
/* Start the service worker and cache all of the app's content */
self.addEventListener('install', function (e) {
e.waitUntil(
caches.open(cacheName).then(function (cache) {
return cache.addAll(filesToCache);
})
);
});
/* Use cached file and update cache if possible */
self.addEventListener('fetch', event => {
event.respondWith(
caches.open(cacheName).then(cache => {
return cache.match(event.request).then(response => {
const fetchPromise = fetch(event.request)
.then(networkResponse => {
if (event.request.url.indexOf('http') === 0)
cache.put(event.request, networkResponse.clone());
return networkResponse;
}).catch(err => undefined)
return response || fetchPromise;
})
})
);
});