const CACHE_NAME = 'vms-cache-v1';
const urlsToCache = [
  '/',
  '/offline.html',
  '/build/manifest.json'
];

self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(function(cache) {
        return cache.addAll(urlsToCache);
      })
  );
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request)
      .then(function(response) {
        if (response) {
          return response;
        }
        return fetch(event.request).catch(function() {
            // Offline fallback
            return caches.match('/offline.html');
        });
      }
    )
  );
});
