diff options
author | Andrew Dolgov <[email protected]> | 2017-02-26 20:00:48 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2017-02-26 20:00:48 +0300 |
commit | d647fdd547147596634ce712fb591119cb45bbb5 (patch) | |
tree | 10122355dd012712e1ccf80d739e3789336b3919 | |
parent | 32221ab0b4b1d10c7e72bd5de0f262efaf704d9c (diff) |
worker: only handle GET requests; update local cache with newer network files
-rw-r--r-- | worker.js | 29 |
1 files changed, 23 insertions, 6 deletions
@@ -33,10 +33,12 @@ self.addEventListener('install', function(event) { this.addEventListener('fetch', function(event) { var req = event.request.clone(); - if (!navigator.onLine) { - event.respondWith( - caches.match(req).then(function(resp) { + if (req.method != "GET") return; + event.respondWith( + caches.match(req).then(function(resp) { + + if (!navigator.onLine) { if (resp) return resp; if (req.url.match("read.html")) { @@ -46,7 +48,22 @@ this.addEventListener('fetch', function(event) { if (req.url.match("index.php")) { return caches.match("offline.html"); } - }) - ); - } + + return; + } + + //console.log("<<<", req.url); + + // refresh cached files + return caches.open(CACHE_NAME).then(function(cache) { + return fetch(req.url,{credentials: 'include'}).then(function(resp) { + return caches.match(req).then(function(match) { + if (match) cache.put(req.url, resp.clone()); + return resp; + }); + }); + }); + }) + ); + }); |