VoiceStudio / sw.js
DillonLD's picture
Upload 11 files
a66f20a verified
Raw History Blame Contribute Delete
1.37 kB
const CACHE_NAME = "voice-studio-shell-v2";
const APP_SHELL = ["/", "/mobile", "/manifest.webmanifest", "/mobile-manifest.webmanifest", "/icon.svg"];
self.addEventListener("install", (event) => {
event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)));
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((names) => Promise.all(
names.filter((name) => name.startsWith("voice-studio-shell-") && name !== CACHE_NAME)
.map((name) => caches.delete(name))
))
);
self.clients.claim();
});
self.addEventListener("fetch", (event) => {
const request = event.request;
const url = new URL(request.url);
if (request.method !== "GET" || url.origin !== self.location.origin ||
url.pathname.startsWith("/api/") || url.pathname.startsWith("/files/")) return;
if (request.mode === "navigate") {
event.respondWith(fetch(request).then((response) => {
const copy = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
return response;
}).catch(async () => (await caches.match(request)) || caches.match(url.pathname.startsWith("/mobile") ? "/mobile" : "/")));
return;
}
event.respondWith(caches.match(request).then((cached) => cached || fetch(request)));
});