// 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.6";
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
"/slike/icons-192.png", // proxy | vsake proxyjane html strani
"/manifest.webmanifest",// proxy | in vsebuje custom skripte,
"/desktop2.css", // proxy | kot na primer sw inštalacijo.
"/phone2.css", // proxy |
"/pin.svg", // proxy |
"/redfox.js", // moje |
"/slike/home100.svg", // proxy |
"/slike/prPage.svg", // proxy |
"/slike/prSubPage.svg", // proxy |
"/slike/neSubPage.svg", // proxy |
"/slike/nePage.svg", // proxy |
"/slike/keyboard.svg", // proxy |
"/slike/go.svg", // proxy |
"/favicon.png", // proxy |
"/offline.html" // moje |
];
self.addEventListener("install", (evt) => {
evt.waitUntil(
caches.open(static_cache_name).then((cache) => {
cache.addAll(assets);
})
);
});
// Delete old caches
self.addEventListener("activate", evt => {
evt.waitUntil(
caches.keys().then((keys) => {
return Promise.all(keys
.filter(key => key !== static_cache_name)
.map(key => caches.delete(key))
);
})
);
});
self.addEventListener("fetch", (event) => {
if (event.request.mode === 'navigate' || // tole ni povsod podprto, zato
( (event.request.method === 'GET' || // če je GET
event.request.method === 'POST') // ali POST zahteva
&& event.request.headers.get('accept').includes('text/html'))) {
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) => {
return cache_res || fetch(event.request);
}));
}
});