flare-gamet2 / sw.js
HelloSun's picture
Manual stat points by default (auto-spend is now an opt-in toggle), bigger + buttons
63a5f47 verified
Raw History Blame Contribute Delete
7.11 kB
// Flare web: Service Worker cache.
// The Space serves assets with "Cache-Control: no-store", so the browser HTTP cache never keeps them.
// Cache Storage (used here) is not affected by that header, so images are downloaded once per build.
// The two placeholders below (build id and asset list) are filled in by tools/inline_build.py.
const BUILD = '95ac9b4b1443';
const SHELL = 'flare-shell-' + BUILD; // index.html (network first, cache fallback)
const ASSETS = 'flare-assets-' + BUILD; // sprites, tiles, icons (cache-first)
const KEEP = [SHELL, ASSETS];
// every asset file, filled in by tools/inline_build.py; downloaded once when the worker installs
const PRECACHE = ["./assets/icons_items.webp", "./assets/fx/arrow_stuck_0.webp", "./assets/fx/arrows_0.webp", "./assets/fx/bear_trap_0.webp", "./assets/fx/bear_trap_trigger_0.webp", "./assets/fx/blast_0.webp", "./assets/fx/caltrops_0.webp", "./assets/fx/channel_0.webp", "./assets/fx/cleave_0.webp", "./assets/fx/dark_mist_0.webp", "./assets/fx/ember_0.webp", "./assets/fx/fireball_0.webp", "./assets/fx/freeze_0.webp", "./assets/fx/freeze_2_0.webp", "./assets/fx/heal_0.webp", "./assets/fx/icicle_0.webp", "./assets/fx/immunity_0.webp", "./assets/fx/lightning_0.webp", "./assets/fx/plasmaball_0.webp", "./assets/fx/quake_0.webp", "./assets/fx/runes_0.webp", "./assets/fx/runes_blue_0.webp", "./assets/fx/runes_orange_0.webp", "./assets/fx/rupture_0.webp", "./assets/fx/shield_0.webp", "./assets/fx/shield_orange_0.webp", "./assets/fx/spark_blue_0.webp", "./assets/fx/spark_green_0.webp", "./assets/fx/spark_green_loop_0.webp", "./assets/fx/spark_orange_0.webp", "./assets/fx/spark_red_0.webp", "./assets/fx/spark_red_loop_0.webp", "./assets/fx/spear_0.webp", "./assets/fx/spit_0.webp", "./assets/fx/status_freeze_0.webp", "./assets/fx/stone_0.webp", "./assets/fx/throw_axe_0.webp", "./assets/fx/throw_knife_0.webp", "./assets/fx/thunderstrike_0.webp", "./assets/icons/0.png", "./assets/icons/1.png", "./assets/icons/10.png", "./assets/icons/11.png", "./assets/icons/12.png", "./assets/icons/14.png", "./assets/icons/15.png", "./assets/icons/16.png", "./assets/icons/17.png", "./assets/icons/18.png", "./assets/icons/2.png", "./assets/icons/20.png", "./assets/icons/21.png", "./assets/icons/22.png", "./assets/icons/23.png", "./assets/icons/25.png", "./assets/icons/3.png", "./assets/icons/32.png", "./assets/icons/33.png", "./assets/icons/34.png", "./assets/icons/35.png", "./assets/icons/36.png", "./assets/icons/37.png", "./assets/icons/38.png", "./assets/icons/39.png", "./assets/icons/4.png", "./assets/icons/40.png", "./assets/icons/41.png", "./assets/icons/42.png", "./assets/icons/43.png", "./assets/icons/44.png", "./assets/icons/45.png", "./assets/icons/46.png", "./assets/icons/5.png", "./assets/icons/515.png", "./assets/icons/6.png", "./assets/icons/64.png", "./assets/icons/65.png", "./assets/icons/7.png", "./assets/icons/72.png", "./assets/icons/73.png", "./assets/icons/74.png", "./assets/icons/75.png", "./assets/icons/76.png", "./assets/icons/77.png", "./assets/icons/78.png", "./assets/icons/79.png", "./assets/icons/8.png", "./assets/icons/80.png", "./assets/icons/81.png", "./assets/icons/82.png", "./assets/icons/83.png", "./assets/icons/84.png", "./assets/icons/85.png", "./assets/icons/86.png", "./assets/icons/87.png", "./assets/icons/9.png", "./assets/loot/belt_0.webp", "./assets/loot/book_0.webp", "./assets/loot/boots_0.webp", "./assets/loot/buckler_0.webp", "./assets/loot/clothes_0.webp", "./assets/loot/club_0.webp", "./assets/loot/coins100_0.webp", "./assets/loot/coins25_0.webp", "./assets/loot/coins5_0.webp", "./assets/loot/dagger_0.webp", "./assets/loot/gem_0.webp", "./assets/loot/greatbow_0.webp", "./assets/loot/greatstaff_0.webp", "./assets/loot/greatsword_0.webp", "./assets/loot/hp_potion_0.webp", "./assets/loot/infantry_axe_0.webp", "./assets/loot/leather_armor_0.webp", "./assets/loot/longbow_0.webp", "./assets/loot/longsword_0.webp", "./assets/loot/mp_potion_0.webp", "./assets/loot/pouch_0.webp", "./assets/loot/reinforced_club_0.webp", "./assets/loot/ring_0.webp", "./assets/loot/rod_0.webp", "./assets/loot/scroll_0.webp", "./assets/loot/shield_0.webp", "./assets/loot/shortbow_0.webp", "./assets/loot/shortsword_0.webp", "./assets/loot/slingshot_0.webp", "./assets/loot/smith_hammer_0.webp", "./assets/loot/staff_0.webp", "./assets/loot/steel_armor_0.webp", "./assets/loot/wand_0.webp", "./assets/loot/zweihander_0.webp", "./assets/hero/default_chest_0.webp", "./assets/hero/default_feet_0.webp", "./assets/hero/default_hands_0.webp", "./assets/hero/default_legs_0.webp", "./assets/hero/head_short_0.webp", "./assets/npcs/wandering_trader_0.webp", "./assets/sprites/enemies/goblin.webp", "./assets/sprites/enemies/skeleton_weak.webp", "./assets/sprites/enemies/zombie.webp", "./assets/tilesets/tileset_grassland.webp", "./assets/tilesets/tileset_grassland_water.webp", "./data/maps/goblin_camp.json"];
self.addEventListener('install', (e) => {
self.skipWaiting();
e.waitUntil((async () => {
const cache = await caches.open(ASSETS);
await Promise.all(PRECACHE.map(async (u) => {
if (await cache.match(u)) return;
try {
const res = await fetch(u);
if (res.ok || res.type === 'opaque') await cache.put(u, res.clone());
} catch (_) { /* a failed file is simply fetched on demand later */ }
}));
})());
});
self.addEventListener('activate', (e) => {
e.waitUntil((async () => {
for (const name of await caches.keys()) {
if (name.startsWith('flare-') && !KEEP.includes(name)) await caches.delete(name);
}
await self.clients.claim();
})());
});
self.addEventListener('fetch', (e) => {
const req = e.request;
if (req.method !== 'GET') return;
const url = new URL(req.url);
if (url.origin !== self.location.origin) return;
if (url.pathname.includes('/assets/') || url.pathname.includes('/data/')) {
e.respondWith(cacheFirst(req));
} else if (req.mode === 'navigate' || url.pathname.endsWith('/') || url.pathname.endsWith('/index.html')) {
e.respondWith(networkFirst(req));
}
});
async function cacheFirst(req) {
const cache = await caches.open(ASSETS);
const hit = await cache.match(req);
if (hit) return hit;
const res = await fetch(req);
// no-cors image requests can come back "opaque" (status 0) after the CDN redirect; those are cacheable too
if (res.ok || res.type === 'opaque') cache.put(req, res.clone()).catch(() => {});
return res;
}
// Page: network first so a new version is used immediately; falls back to the cached copy when offline / slow (>4s)
async function networkFirst(req) {
const cache = await caches.open(SHELL);
const net = fetch(req).then((res) => {
if (res.ok) cache.put(req, res.clone()).catch(() => {});
return res;
});
const timeout = new Promise((resolve) => setTimeout(() => resolve(null), 4000));
try {
const res = await Promise.race([net, timeout]);
if (res) return res;
} catch (_) { /* offline: fall through to cache */ }
const hit = await cache.match(req);
if (hit) { net.catch(() => {}); return hit; }
return net;
}