/* GOBAO app01111: no offline recache. Always prefer the latest server files. */
const CACHE_NAME = 'gobao-no-cache-app01111';
const ASSETS = [];

self.addEventListener('install', event => {
  event.waitUntil((async () => {
    await self.skipWaiting();
  })());
});

self.addEventListener('activate', event => {
  event.waitUntil((async () => {
    const names = await caches.keys();
    await Promise.all(names.filter(name => name.startsWith('gobao-')).map(name => caches.delete(name)));
    await self.clients.claim();
  })());
});

self.addEventListener('fetch', event => {
  const req = event.request;
  if (req.method !== 'GET') return;
  const url = new URL(req.url);
  if (url.origin !== self.location.origin || url.pathname.startsWith('/api/') || url.pathname.startsWith('/downloads/')) return;
  event.respondWith((async () => {
    try {
      const fresh = await fetch(req, {cache: 'no-store'});
      return fresh;
    } catch (e) {
      return new Response('GOBAO latest server file is temporarily unavailable.', {status:503});
    }
  })());
});
