summaryrefslogtreecommitdiff
path: root/worker.js
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2019-11-19 14:49:13 +0300
committerAndrew Dolgov <[email protected]>2019-11-19 14:49:13 +0300
commit6c037531be3d0e5e4aeb0b1836a960941d1062a2 (patch)
tree11ff005d0a9772a6d772a75bd3d56ea489f74876 /worker.js
parent11bcabe3bf074c922c95aa945504f427c15dde7c (diff)
use map + Promise.all instead of plain iteration while fetching cached urls
Diffstat (limited to 'worker.js')
-rw-r--r--worker.js31
1 files changed, 8 insertions, 23 deletions
diff --git a/worker.js b/worker.js
index 7ae7724..65e12f4 100644
--- a/worker.js
+++ b/worker.js
@@ -75,40 +75,25 @@ self.addEventListener('message', function(event){
send_broadcast('refresh-started');
- caches.open(CACHE_NAME).then(function(cache) {
- const promises = [];
+ return caches.open(CACHE_NAME).then(function(cache) {
- for (let i = 0; i < CACHE_URLS.length; i++) {
+ Promise.all(CACHE_URLS.map((url) => {
+ return fetch(url + "?ts=" + Date.now()).then((resp) => {
+ console.log('got', resp.url, resp);
- if (CACHE_URLS[i].match("backend.php"))
- continue;
-
- const fetch_url = CACHE_URLS[i] + "?ts=" + Date.now();
-
- const promise = fetch(fetch_url).then(function(resp) {
- const url = new URL(resp.url);
- url.searchParams.delete("ts");
-
- console.log('got', url);
-
- send_broadcast('refreshed:' + url);
+ send_broadcast('refreshed:' + resp.url);
if (resp.status == 200) {
- cache.put(url, resp);
+ return cache.put(url, resp);
} else if (resp.status == 404) {
- cache.delete(url);
+ return cache.delete(url);
}
});
- promises.push(promise);
-
- }
-
- Promise.all(promises).then(function() {
+ })).then(function() {
console.log('all done');
send_broadcast('client-reload');
});
-
});
}
});