// Installing service worker
const CACHE_NAME  = 'WinterBeach-v20250815';

/* Add relative URL of all the static content you want to store in
 * cache storage (this will help us use our app offline)*/
let resourcesToCache = ["./",
"./index.html", 
"./play.html",
"./interpreter/WinterBeach.gblorb.js", 
"./interpreter/glkote.min.js", 
"./interpreter/jquery-1.12.4.min.js", 
"./interpreter/quixe.min.js", 
"./interpreter/resourcemap.js", 
"./interpreter/dialog.css", 
"./interpreter/glkote.css", 
"./interpreter/background.jpg", 
"./interpreter/pxiKyp0ihIEF2isfFJU.woff2",
"./style.css",
"./cover512.png",
"./cover192.png",
"./cover180.png",
"./favicon-32x32.png",
"./favicon-16x16.png",
"./favicon.ico",
];

self.addEventListener("install", e=>{
    e.waitUntil(
        caches.open(CACHE_NAME).then(cache =>{
            return cache.addAll(resourcesToCache);
        })
    );
});

// Cache and return requests
self.addEventListener("fetch", e=>{
    e.respondWith(
        caches.match(e.request).then(response=>{
            return response || fetch(e.request);
        })
    );
});

// Update a service worker
const cacheWhitelist = [ CACHE_NAME ];
self.addEventListener('activate', event => {
    event.waitUntil(
      caches.keys().then(cacheNames => {
        return Promise.all(
          cacheNames.map(cacheName => {
            if (cacheWhitelist.indexOf(cacheName) === -1) {
              return caches.delete(cacheName);
            }
          })
        );
      })
    );
  });
