From 296d347fe04602e2798e072de21c222c7f95b51c Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 17 Nov 2019 15:46:56 +0300 Subject: 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 --- worker.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'worker.js') diff --git a/worker.js b/worker.js index f567447..7ae7724 100644 --- a/worker.js +++ b/worker.js @@ -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); } }); }) -- cgit v1.2.3