76 lines
1.9 KiB
JavaScript
76 lines
1.9 KiB
JavaScript
const CACHE_NAME = "whopays-v1";
|
|
const urlsToCache = [
|
|
"/",
|
|
"/index.html",
|
|
"/style.css",
|
|
"/script.js",
|
|
"/img/a.png",
|
|
"/img/b.png",
|
|
"/img/bgA.jpg",
|
|
"/img/bgB.jpg",
|
|
"/manifest.json"
|
|
];
|
|
|
|
// Install event - cache assets
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
console.log("Cache geöffnet");
|
|
return cache.addAll(urlsToCache).catch(err => {
|
|
console.error("Cache fehler:", err);
|
|
});
|
|
})
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
// Activate event - clean old caches
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((cacheNames) => {
|
|
return Promise.all(
|
|
cacheNames.map((cacheName) => {
|
|
if (cacheName !== CACHE_NAME) {
|
|
console.log("Alte Cache löschen:", cacheName);
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
// Fetch event - serve from cache, fallback to network
|
|
self.addEventListener("fetch", (event) => {
|
|
// Network first for API calls
|
|
if (event.request.url.includes("/status") ||
|
|
event.request.url.includes("/toggle")) {
|
|
event.respondWith(
|
|
fetch(event.request).catch(() => {
|
|
return new Response(
|
|
JSON.stringify({ error: "Offline" }),
|
|
{ headers: { "Content-Type": "application/json" } }
|
|
);
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Cache first for static assets
|
|
event.respondWith(
|
|
caches.match(event.request).then((response) => {
|
|
return response || fetch(event.request).then((fetchResponse) => {
|
|
return caches.open(CACHE_NAME).then((cache) => {
|
|
cache.put(event.request, fetchResponse.clone());
|
|
return fetchResponse;
|
|
});
|
|
});
|
|
}).catch(() => {
|
|
// Fallback for navigation requests
|
|
if (event.request.mode === 'navigate') {
|
|
return caches.match('/index.html');
|
|
}
|
|
})
|
|
);
|
|
}); |