allerta-vvf/server/resources/src/sw.js

59 lines
2.8 KiB
JavaScript
Raw Normal View History

let cacheVersion = 1
let cacheName = "static-"+cacheVersion
2020-10-11 00:33:42 +02:00
2020-11-06 15:14:59 +01:00
const urls = ['offline.php', 'resources/dist/main.js', 'resources/dist/maps.js', 'manifest.webmanifest', 'resources/images/favicon.ico', 'resources/dist/marker-icon.png', 'resources/dist/layers.png', 'resources/dist/layers-2x.png', 'resources/images/android-chrome-192x192.png', 'resources/images/android-chrome-384x384.png', 'resources/images/black_helmet.png', 'resources/images/red_helmet.png', 'resources/images/wheel.png', 'resources/images/logo.png', 'resources/images/owner.png', 'resources/dist/fonts/fontawesome-webfont.ttf', 'resources/dist/fonts/fontawesome-webfont.svg', 'resources/dist/fonts/fontawesome-webfont.woff', 'resources/dist/fonts/fontawesome-webfont.woff2'];
2020-10-11 00:33:42 +02:00
function fetchHandler(event, content_type, not_found_message){
event.respondWith(
fetch(event.request).then(function (response) {
return response;
}).catch(function (error) {
if(content_type == null){ // if content_type is null, load a file from cache as not found response
var not_found_response = caches.match(not_found_message).then(function(response) {
return response;
});
} else {
var not_found_response = new Response(new Blob([not_found_message]), {
headers: {'Content-Type': content_type}
});
}
return caches.match(event.request).then(function(response) {
return response || not_found_response;
});
})
);
}
self.addEventListener('fetch', function (event) {
2020-11-13 15:22:26 +01:00
//console.log(event.request);
var request = event.request;
2020-10-11 00:33:42 +02:00
// https://stackoverflow.com/a/49719964
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') return;
if (request.headers.get('Accept').includes('text/html')) {
2020-11-06 15:14:59 +01:00
fetchHandler(event, null, "offline.php");
} else if (request.destination == "script") {
fetchHandler(event, "application/javascript", "console.error('Script "+event.request.url+" not found');");
} else if (request.destination == "image") {
2020-11-06 15:14:59 +01:00
fetchHandler(event, null, "resources/images/logo.png");
2020-10-25 21:22:32 +01:00
} else if (request.destination == "font") {
fetchHandler(event, null, null);
} else if (request.destination == "manifest" || request.url.includes("manifest")) {
2020-11-06 15:14:59 +01:00
fetchHandler(event, null, "manifest.webmanifest");
} else {
event.respondWith(fetch(request));
}
});
self.addEventListener('install', event => {
self.skipWaiting();
console.log("Service worker installed");
})
self.addEventListener('activate', event => {
event.waitUntil(caches.open(cacheName)
.then((openCache) => {
return openCache.addAll(urls);
})
.catch(err => console.error(err)))
2020-10-22 15:30:11 +02:00
});