owlboard-svelte/src/service-worker.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-06-21 14:56:12 +01:00
/// <reference types="@sveltejs/kit" />
import { build, files, version } from '$service-worker';
const cacheName = `ob-${version}`;
const assets = [
...build,
...files,
"/service-worker.js"
2023-06-21 14:56:12 +01:00
];
self.addEventListener('install', (event) => {
async function addToCache() {
const cache = await caches.open(cacheName);
await cache.addAll(assets);
}
event.waitUntil(addToCache());
})
self.addEventListener('activate', (event) => {
async function deleteOldCache() {
for (const key of await caches.keys()) {
if (key !== cacheName) {
await caches.delete(key);
}
}
}
event.waitUntil(deleteOldCache());
})
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') {return}
async function respond() {
const cacheRes = await caches.match(event.request, {ignoreSearch: true});
if (cacheRes) {
return cacheRes;
}
try {
return await fetch(event.request)
} catch (err) {
return {"error": "OFFLINE", "errorMsg": "You are not online"};
}
}
event.respondWith(respond());
})