const CACHE_NAME = '@AppName@';
const resourcesToCache = ['index.html', 'manifest.json', 'js/', 'css/', 'assets/'];

// install service worker and cache resources defined in resourcesToCache
self.addEventListener('install', (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME).then(cache => (
      cache.addAll(resourcesToCache)
    ))
  );
});

// return response from cache if there's a match
// otherwise fetch from server and add it to cache
self.addEventListener('fetch', (event) => {
  event.respondWith(
    caches.match(event.request).then((response) => {
      return response || fetch(event.request).then((response) => {
        return caches.open(CACHE_NAME).then((cache) => {
          cache.put(event.request, response.clone());
          return response;
        });
      });
    })
  );
});
