const CACHE_NAME = "szam-v1";

const CORE_CACHE = [
  "/szam/",
  "/szam/index.html",
  "/szam/manifest.json",

  "/szam/autozap/",
  "/szam/autozap/index.php"
];

self.addEventListener("install", event => {
  event.waitUntil(
    caches.open(CACHE_NAME).then(async cache => {
      // Cache one-by-one so a single 404 doesn't kill the whole install
      for (const url of CORE_CACHE) {
        try { await cache.add(url); } catch (e) {}
      }
    })
  );
});
self.addEventListener("activate", event => {
  event.waitUntil(
    caches.keys().then(keys =>
      Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
    )
  );
});
self.addEventListener("fetch", event => {
  if (event.request.method !== "GET") return;
  event.respondWith(caches.match(event.request).then(res => res || fetch(event.request)));
});