/* Tarkov Maps service worker -- offline and instant start for the viewer. * * Strategy by path: * tiles/, tiles-trimmed/, vendor/ cache-first. Tile URLs carry ?v= * and are immutable; once seen, never * fetched again. The page posts the live * build versions so stale ones get dropped. * index.html, app.js, style.css stale-while-revalidate: paint from cache * immediately, refresh in the background. * The next load gets the new shell. * data/*.json network-first, cache fallback. world.json * must be fresh when the network is there; * offline, the last known copy still works. * data/capabilities.json, api/ network only (server state). * * The authoring server unregisters this worker -- see app.js. */ const VERSION = 'v1'; const SHELL_CACHE = `tarkov-shell-${VERSION}`; const DATA_CACHE = `tarkov-data-${VERSION}`; const TILE_CACHE = 'tarkov-tiles'; // tiles are versioned by URL, not by worker const KEEP = new Set([SHELL_CACHE, DATA_CACHE, TILE_CACHE]); const SHELL = ['./', 'index.html', 'app.js', 'style.css', 'vendor/leaflet.js', 'vendor/leaflet.css']; self.addEventListener('install', (event) => { event.waitUntil((async () => { const cache = await caches.open(SHELL_CACHE); // One at a time with individual failures tolerated: a missing optional // file must not block installation. await Promise.all(SHELL.map((url) => cache.add(url).catch(() => {}))); await self.skipWaiting(); })()); }); self.addEventListener('activate', (event) => { event.waitUntil((async () => { const names = await caches.keys(); await Promise.all(names.filter((n) => !KEEP.has(n)).map((n) => caches.delete(n))); await self.clients.claim(); })()); }); const isTile = (path) => path.includes('/tiles/') || path.includes('/tiles-trimmed/') || path.includes('/vendor/'); const isShell = (path) => /\/(index\.html|app\.js|style\.css)?$/.test(path); const isServerState = (path) => path.endsWith('/data/capabilities.json') || path.includes('/api/'); const isData = (path) => path.includes('/data/'); const cacheFirst = async (request) => { const cache = await caches.open(TILE_CACHE); const hit = await cache.match(request); if (hit) return hit; const response = await fetch(request); if (response.ok) cache.put(request, response.clone()).catch(() => {}); return response; }; const staleWhileRevalidate = async (request) => { const cache = await caches.open(SHELL_CACHE); const hit = await cache.match(request, { ignoreSearch: true }); const refresh = fetch(request).then((response) => { if (response.ok) cache.put(request, response.clone()).catch(() => {}); return response; }); if (hit) { refresh.catch(() => {}); return hit; } return refresh; }; const networkFirst = async (request) => { const cache = await caches.open(DATA_CACHE); try { const response = await fetch(request); if (response.ok) cache.put(request, response.clone()).catch(() => {}); return response; } catch (error) { const hit = await cache.match(request, { ignoreSearch: true }); if (hit) return hit; throw error; } }; self.addEventListener('fetch', (event) => { const { request } = event; if (request.method !== 'GET') return; const url = new URL(request.url); if (url.origin !== self.location.origin) return; const path = url.pathname; if (isServerState(path)) return; // network only if (isTile(path)) event.respondWith(cacheFirst(request)); else if (isData(path)) event.respondWith(networkFirst(request)); else if (isShell(path) || request.mode === 'navigate') event.respondWith(staleWhileRevalidate(request)); }); // The page tells us which build versions are live; tiles from older builds // are dead weight (their URLs will never be requested again). self.addEventListener('message', (event) => { const data = event.data; if (!data || data.type !== 'tile-versions' || !Array.isArray(data.versions)) return; const live = new Set(data.versions); event.waitUntil((async () => { const cache = await caches.open(TILE_CACHE); for (const request of await cache.keys()) { const version = new URL(request.url).searchParams.get('v'); if (version && !live.has(version)) await cache.delete(request); } })()); });