diff options
author | Andrew Dolgov <[email protected]> | 2019-11-17 15:46:56 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2019-11-17 15:46:56 +0300 |
commit | 296d347fe04602e2798e072de21c222c7f95b51c (patch) | |
tree | 0a3ac16e42554e600b513c0f2e85f79aab4703b2 | |
parent | 10734c622fb6d9ca19f8580242ff951ab9775045 (diff) |
service worker: only hardcode return of read.html etc while offline, try
to return cached data if online request failed
index: notify to reload the page initially because of SW bullshit
-rw-r--r-- | js/index.js | 7 | ||||
-rw-r--r-- | worker.js | 16 |
2 files changed, 14 insertions, 9 deletions
diff --git a/js/index.js b/js/index.js index 9aadc00..2ec8508 100644 --- a/js/index.js +++ b/js/index.js @@ -28,9 +28,10 @@ function cache_refresh(force) { .html("Could not communicate with service worker. Try reloading the page."); } else { localforage.setItem("epube.initial-load-done", true).then(function() { - window.setTimeout(function() { - window.location.reload(); - }, 1000); + $(".dl-progress") + .show() + .addClass("alert-info") + .html("Please reload the page once to activate service worker and populate offline cache."); }); } @@ -123,15 +123,17 @@ this.addEventListener('fetch', function(event) { return resp; } - if (req.url.match("read.html")) { - return caches.match("read.html"); - } + 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"); + if (req.url.match("offline.html")) { + return caches.match("offline.html"); + } } - console.log('cache miss for', req.url); + console.log('cache miss for', req.url, 'OL:', navigator.onLine); return fetch(req).then(function(resp) { @@ -148,6 +150,8 @@ this.addEventListener('fetch', function(event) { }).catch(function() { if (req.url[req.url.length-1] == "/" || req.url.match("index.php")) { return caches.match("offline.html"); + } else { + return caches.match(req.url); } }); }) |