-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
56 lines (49 loc) · 1.47 KB
/
worker.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
const CACHE_NAME = 'cache';
let fetchHandler;
self.addEventListener('fetch', fetchHandler = function (event) {
event.respondWith(
caches.match(event.request)
.then(function (response) {
if (response) {
return response;
}
return fetch(event.request).then(
function (response) {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(event.request, responseToCache);
});
return response;
}
);
})
);
});
fetchHandler({ request: '.', respondWith: function (response) {} });
async function loadSpritesheets(i=0) {
await fetch(`data/${i}.png`).then(function (response) {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
fetchHandler({ request: `data/${i}.png`, respondWith: function (response) {} });
loadSpritesheets(i + 1);
});
}
loadSpritesheets();
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.filter(function (cacheName) {
return cacheName === CACHE_NAME;
}).map(function (cacheName) {
return caches.delete(cacheName);
})
);
})
);
});