'use strict'; const CACHE_NAME = 'shelem-v2'; // ── Generate all 55 card paths ────────────────────────────────── const SUITS = ['CLUB', 'DIAMOND', 'HEART', 'SPADE']; const CARD_PATHS = [ '/cards/JOKER-1.svg', '/cards/JOKER-2.svg', '/cards/JOKER-3.svg', ...SUITS.flatMap(s => [ `/cards/${s}-1.svg`, `/cards/${s}-2.svg`, `/cards/${s}-3.svg`, `/cards/${s}-4.svg`, `/cards/${s}-5.svg`, `/cards/${s}-6.svg`, `/cards/${s}-7.svg`, `/cards/${s}-8.svg`, `/cards/${s}-9.svg`, `/cards/${s}-10.svg`, `/cards/${s}-11-JACK.svg`, `/cards/${s}-12-QUEEN.svg`, `/cards/${s}-13-KING.svg`, ]), ]; const PRECACHE = [ '/', '/index.html', '/style.css', '/app.js', '/manifest.json', '/icons/icon-192.png', '/icons/icon-512.png', '/icons/icon.svg', ...CARD_PATHS, ]; self.addEventListener('install', event => { event.waitUntil( caches.open(CACHE_NAME).then(cache => cache.addAll(PRECACHE)) ); self.skipWaiting(); }); self.addEventListener('activate', event => { event.waitUntil( caches.keys().then(keys => Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))) ) ); self.clients.claim(); }); self.addEventListener('fetch', event => { const url = event.request.url; if (event.request.method !== 'GET') return; if (url.includes('/socket.io/')) return; // Cards: always serve from cache (never change) if (url.includes('/cards/')) { event.respondWith( caches.match(event.request).then(cached => { if (cached) return cached; return fetch(event.request).then(res => { caches.open(CACHE_NAME).then(c => c.put(event.request, res.clone())); return res; }); }) ); return; } // Everything else: network-first, cache as offline fallback event.respondWith( fetch(event.request) .then(res => { caches.open(CACHE_NAME).then(c => c.put(event.request, res.clone())); return res; }) .catch(() => caches.match(event.request)) ); });