2023-07-23 15:06:57 +02:00
|
|
|
const sw_version = '0.1.0';
|
|
|
|
const cache_name = `sake-v${sw_version}`;
|
|
|
|
const cached_files = [
|
2023-07-23 17:39:49 +02:00
|
|
|
'/',
|
2023-07-23 15:06:57 +02:00
|
|
|
'/app.js',
|
|
|
|
'/index.html',
|
|
|
|
'/vendor/base32.min.js',
|
|
|
|
'/vendor/base32.min.js.map',
|
|
|
|
'/vendor/sha256.min.js',
|
|
|
|
];
|
|
|
|
|
|
|
|
function log_message(msg) {
|
|
|
|
console.log(`[Service Worker] v${sw_version}: ${msg}`);
|
|
|
|
}
|
|
|
|
|
2023-07-23 17:55:42 +02:00
|
|
|
self.addEventListener('install', (event) => {
|
2023-07-23 17:41:19 +02:00
|
|
|
log_message('Installed');
|
|
|
|
self.skipWaiting();
|
2023-07-23 17:55:42 +02:00
|
|
|
event.waitUntil(caches.open(cache_name).then((cache) => {
|
2023-07-23 15:06:57 +02:00
|
|
|
log_message('Caching all');
|
2023-07-23 17:41:19 +02:00
|
|
|
return cache.addAll(cached_files);
|
|
|
|
}));
|
2023-07-23 15:06:57 +02:00
|
|
|
});
|
|
|
|
|
2023-07-23 17:55:42 +02:00
|
|
|
self.addEventListener('fetch', (event) => {
|
|
|
|
if (!(event.request.url.startsWith('https:') || event.request.url.startsWith('http:'))) {
|
|
|
|
log_message(`Fetching resource failed: invalid protocol: ${event.request.url}`);
|
2023-07-23 15:06:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-23 18:04:57 +02:00
|
|
|
event.respondWith(caches.open(cache_name).then((cache) => {
|
2023-07-23 17:55:42 +02:00
|
|
|
log_message(`Fetching resource: ${event.request.url}`);
|
2023-07-23 18:04:57 +02:00
|
|
|
return cache.match(event.request).then((cached_response) => {
|
|
|
|
const fetched_response = fetch(event.request).then((network_response) => {
|
|
|
|
log_message(`Caching resource: ${event.request.url}`);
|
|
|
|
cache.put(event.request, network_response.clone());
|
|
|
|
return network_response;
|
|
|
|
});
|
|
|
|
return cached_response || fetched_response;
|
|
|
|
});
|
|
|
|
}));
|
2023-07-23 15:06:57 +02:00
|
|
|
});
|