summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2017-02-26 20:00:48 +0300
committerAndrew Dolgov <[email protected]>2017-02-26 20:00:48 +0300
commitd647fdd547147596634ce712fb591119cb45bbb5 (patch)
tree10122355dd012712e1ccf80d739e3789336b3919
parent32221ab0b4b1d10c7e72bd5de0f262efaf704d9c (diff)
worker: only handle GET requests; update local cache with newer network files
-rw-r--r--worker.js29
1 files changed, 23 insertions, 6 deletions
diff --git a/worker.js b/worker.js
index 219947f..88a97c4 100644
--- a/worker.js
+++ b/worker.js
@@ -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;
+ });
+ });
+ });
+ })
+ );
+
});