looper/web/serviceWorker.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-05-02 14:52:11 -07:00
const staticLooper = "looper-app-v1";
const assets = [
"/",
"/index.html",
"/shell.js",
"/icon.png",
"/icon.svg",
"/index.wasm",
"/index.js"
];
const devAssets = [
"/index.html.symbols",
"/index.wasm.map"
];
self.addEventListener("message", (event) => {
if (event.data) {
switch (event.data.type) {
case "UNREGISTER": {
self.unregister();
} break;
case "UPDATE": {
caches.open(staticLooper).then(async cache => {
await cache.addAll(assets);
try {
await cache.addAll(devAssets);
} catch (e) {
console.warn("Could not add developer assets!");
}
})
} break;
}
}
})
self.addEventListener("install", installEvent => {
installEvent.waitUntil(
self.postMessage({
type: "UPDATE"
})
)
});
self.addEventListener("fetch", fetchEvent => {
fetchEvent.respondWith(
caches.match(fetchEvent.request).then(res => {
return res || fetch(fetchEvent.request);
})
)
});