-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
50 lines (46 loc) · 1.59 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
const PRECACHE = 'rle-sprite-editor-001';
const PRECACHE_URLS = [
'./',
'index.html'
];
let createCacheBustedRequest = url => {
let request = new Request(url, { cache: 'reload' });
if ('cache' in request) {
return request;
}
let bustedUrl = new URL(url, self.location.href);
bustedUrl.search += (bustedUrl.search ? '&' : '') + 'cachebust=' + Date.now();
return new Request(bustedUrl);
}
self.addEventListener('install', event => {
event.waitUntil(
caches.open(PRECACHE)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
);
});
self.addEventListener('activate', event => {
const currentCaches = [PRECACHE];
event.waitUntil(
caches.keys().then(cacheNames => {
return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
}).then(cachesToDelete => {
return Promise.all(cachesToDelete.map(cacheToDelete => {
return caches.delete(cacheToDelete);
}));
}).then(() => self.clients.claim())
);
});
self.addEventListener('fetch', event => {
if (
event.request.mode === 'navigate' ||
(event.request.method === 'GET' && event.request.headers.get('accept').indexOf('text/html') > -1)
) {
event.respondWith(fetch(createCacheBustedRequest(event.request.url)).then(response => {
caches.open(PRECACHE).then(cache => cache.put(event.request.url, response.clone()));
return response.clone();
}).catch(error => {
return caches.match(event.request.url);
}));
}
});