summaryrefslogtreecommitdiff
path: root/worker.js
diff options
context:
space:
mode:
Diffstat (limited to 'worker.js')
-rw-r--r--worker.js114
1 files changed, 51 insertions, 63 deletions
diff --git a/worker.js b/worker.js
index 73024fd..e40d731 100644
--- a/worker.js
+++ b/worker.js
@@ -1,5 +1,3 @@
-//importScripts('lib/localforage.min.js');
-
const CACHE_PREFIX = 'epube';
const CACHE_NAME = CACHE_PREFIX + '-v3';
@@ -17,90 +15,80 @@ self.addEventListener('activate', function(event) {
});
function send_message(client, msg) {
- client.postMessage(msg);
+ return client.postMessage(msg);
}
function send_broadcast(msg) {
- self.clients.matchAll().then((clients) => {
+ return self.clients.matchAll().then((clients) => {
clients.forEach((client) => {
send_message(client, msg);
})
})
}
-self.addEventListener('message', function(event){
- console.log('got message', event.data);
+self.addEventListener('message', async (event) => {
+ console.log('[worker] got message', event.data);
if (event.data.msg == 'refresh-cache') {
- console.log("refreshing cache...", event);
+ console.log("[worker] refreshing cache...");
- send_broadcast('refresh-started');
+ await send_broadcast('refresh-started');
- return caches.open(CACHE_NAME).then(function(cache) {
- Promise.all(event.data.urls.map(async function(url) {
- const resp = await fetch(url + "?ts=" + Date.now());
- console.log('got', resp.url, resp);
- send_broadcast('refreshed:' + resp.url);
- if (resp.status == 200) {
- return cache.put(url, resp);
- } else if (resp.status == 404) {
- return cache.delete(url);
- }
+ const cache = await caches.open(CACHE_NAME);
+
+ await Promise.all(event.data.urls.map(async (url) => {
+ const resp = await fetch(url + "?ts=" + Date.now());
+ send_broadcast('refreshed:' + resp.url);
+ if (resp.ok) {
+ console.log('[worker] refresh complete for', resp.url);
+ return cache.put(url, resp);
+ } else if (resp.status == 404) {
+ console.log('[worker] removing obsolete file', resp.url);
+ return cache.delete(url);
+ }
+ }));
+
+ console.log('[worker] refresh finished');
- })).then(function() {
- console.log('all done');
- send_broadcast('client-reload');
- });
- });
+ await send_broadcast('refresh-finished');
}
});
-this.addEventListener('fetch', function(event) {
- const req = event.request.clone();
-
- if (req.url.match('/assets/'))
+this.addEventListener('fetch', (event) => {
+ if (event.request.url.match('/assets/'))
return;
- event.respondWith(
- caches.match(req).then(function(resp) {
-
+ event.respondWith(caches.match(event.request).then((resp) => {
if (resp) {
+ console.log('[worker] cache hit for', event.request.url);
return resp;
- }
-
- /*if (!navigator.onLine) {
- if (req.url.match("read.html")) {
- return caches.match("read.html");
- }
-
- if (req.url.match("offline.html")) {
- return caches.match("offline.html");
- }
- }*/
-
- console.log('cache miss for', req.url, 'OL:', navigator.onLine);
-
- return fetch(req).then(function(resp) {
-
- if (resp.status == 200) {
- if (resp.url.match("backend.php\\?op=(cover|getinfo)")) {
- return caches.open(CACHE_NAME).then(function(cache) {
- cache.put(resp.url, resp.clone());
+ } else {
+ console.log('[worker] cache miss for', event.request.url);
+ return fetch(event.request.clone())
+ .then((resp) => {
+ if (resp.ok && resp.url.match("backend.php\\?op=(cover|getinfo)")) {
+ return caches.open(CACHE_NAME).then((cache) => {
+ console.log('[worker] caching response', resp.url);
+ cache.put(resp.url, resp.clone());
+ return resp;
+ });
+ } else {
return resp;
- });
+ }
+ })
+ .catch((err) => {
+ console.warn('[worker] fetch request failed', event.request.url, err);
+
+ if (event.request.url[event.request.url.length-1] == "/" || event.request.url.match("index.php|offline.html")) {
+ return caches.match("offline.html");
+ } else if (event.request.url.match("read.html")) {
+ return caches.match("read.html");
+ } else {
+ console.error('[worker] no fetch fallback for ', event.request.url);
+ }
}
- }
-
- return resp;
- }).catch(function() {
- if (req.url[req.url.length-1] == "/" || req.url.match("index.php") || req.url.match("offline.html")) {
- return caches.match("offline.html");
- } else if (req.url.match("read.html")) {
- return caches.match("read.html");
- } else {
- return caches.match(req.url);
- }
- });
+ );
+ }
})
);
});