From 9f4927825bb5efeefdff9a2aac05c5b3200f5ef6 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 28 Jun 2017 12:32:48 +0300 Subject: move to internal user management because it's impossible to implement proper transparent offline mode with http auth (worker is incapable of authenticating properly) MIGRATION: 1. disable HTTP authentication (this is important!) 2. add two new tables to db/scratch.db (sessions & users) 3. create users via useradm.php (same names and passwords, previous data is kept) --- worker.js | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) (limited to 'worker.js') diff --git a/worker.js b/worker.js index d2ed2f7..268aee2 100644 --- a/worker.js +++ b/worker.js @@ -44,7 +44,7 @@ self.addEventListener('message', function(event){ cache.keys().then(function(keys) { for (var i = 0; i < keys.length; i++) { - fetch(keys[i],{credentials:'same-origin'}).then(function(resp) { + fetch(keys[i]).then(function(resp) { if (resp.status == 200) { cache.put(resp.url, resp); } @@ -59,24 +59,38 @@ self.addEventListener('message', function(event){ this.addEventListener('fetch', function(event) { var req = event.request.clone(); - if (!navigator.onLine) { - event.respondWith( - caches.match(req).then(function(resp) { + event.respondWith( + caches.match(req).then(function(resp) { - if (resp) return resp; + if (resp) { + return resp; + } - if (req.url.match("read.html")) { - return caches.match("read.html"); - } + 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"); + } + + return fetch(req).then(function(resp) { + + if (resp.status == 200) { + if (resp.url.match("backend.php\\?op=cover")) { + return caches.open(CACHE_NAME).then(function(cache) { + cache.put(resp.url, resp.clone()); + return resp; + }); + } } - if (req.url.match("index.php")) { + return resp; + }).catch(function() { + if (req.url[req.url.length-1] == "/" || req.url.match("index.php")) { return caches.match("offline.html"); } - }) - ); - } + }); + }) + ); }); -- cgit v1.2.3