From f6069173c53ff0f9f05a416f9849d94e9c5bbebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Sat, 8 Feb 2020 21:45:13 +0100 Subject: sw.js must not be cached! --- sw.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/sw.js b/sw.js index 792cea2..d937883 100644 --- a/sw.js +++ b/sw.js @@ -1,6 +1,7 @@ // Change version to cause cache refresh const static_cache_name = "site-static-v1.0.8"; -// Got them with find . -not -path '*/\.*' | sed "s/.*/\"&\",/" +// Got them with find . -not -path '*/\.*' | sed "s/.*/\"&\",/" | grep -v sw.js +// sw.js NE SME BITI CACHAN, ker vsebuje verzijo! const assets = [ ".", "./login.html", @@ -28,7 +29,6 @@ const assets = [ "./fonts/fa-brands-400.eot", "./fonts/materialicons.woff2", "./index.html", -"./sw.js", "./img", "./img/avatars", "./img/avatars/asijanec.png", @@ -148,6 +148,30 @@ self.addEventListener("activate", evt => { ); }); +self.addEventListener('message', event => { + if (event.data) { + let data = JSON.parse(event.data); // parse the message back to JSON + if (data.action == "addtocache") { // check the action + event.waitUntil( + caches.open(static_cache_name).then(function(cache) { + try { + return cache.add([data.url]); + } + catch(error) { + console.error("[sw.js] error: "+error); + } + }) + ); + } else if (data.action == "deletecaches") { + caches.keys().then(function(names) { + for (let name of names) + console.log("[sw.js] deleting cache named "+name); + caches.delete(name); + }); + } + } +}); + self.addEventListener("fetch", (evt) => { evt.respondWith(caches.match(evt.request).then((cache_res) => { return cache_res || fetch(evt.request); -- cgit v1.2.3