diff options
Diffstat (limited to 'sw.js')
-rw-r--r-- | sw.js | 43 |
1 files changed, 36 insertions, 7 deletions
@@ -1,8 +1,11 @@ // Change version to cause cache refresh, sw.js SE NE SME CACHAT aka ne ga dat v assets! -const static_cache_name = "site-static-v0.0.4"; +const static_cache_name = "site-static-v0.0.5"; const offlineUrl = "/offline.html"; const assets = [ "/manifest.json", // proxy | + "/900/404/", // moje | + "/900/404/index.html", // moje | + "/900/404", // moje | "/", // proxy | proxyjan je tudi sw.js "/index.html", // proxy | "/slike/icons-512.png", // proxy | redfox.js je poklican iz @@ -22,7 +25,6 @@ const assets = [ "/favicon.png", // proxy | "/offline.html" // moje | ]; - self.addEventListener("install", (evt) => { evt.waitUntil( caches.open(static_cache_name).then((cache) => { @@ -48,13 +50,40 @@ self.addEventListener("fetch", (event) => { ( (event.request.method === 'GET' || // če je GET event.request.method === 'POST') // ali POST zahteva && event.request.headers.get('accept').includes('text/html'))) { - - event.respondWith(fetch(event.request.url).catch(error => { - return caches.match(offlineUrl); + + event.respondWith(caches.match(event.request) + .then((cache_res) => { + if (cache_res) { + return cache_res; + } else { + +///// start of fetch thingies ///// + + return fetch(event.request.url) + .then(response => { + response.redirected = false; + console.log(response); + if (response.redirected) { +return new Response("<meta http-equiv=refresh content=0;/?404 />", { + status: 200, + statusText: "OK", + headers: response.headers +}); + } + return response; + }) + .catch(error => { + return caches.match(offlineUrl); + }); + +///// end of fetch thingys ///// + + } })); } else { // zahteva je za nek resource (css/js/img), ne za stran - event.respondWith(caches.match(event.request).then((cache_res) => { + event.respondWith(caches.match(event.request) + .then((cache_res) => { return cache_res || fetch(event.request); - })) + })); } }); |