Files
lucioandClaude Fable 5 472c983287 Commit build outputs via LFS; right-click markers, inline edit box, panel pin
Make a fresh clone servable after `git lfs pull` alone, and rebuildable with
`tools/build.py` without re-rendering unchanged PSDs:

- Track build/ (rendered base PNGs, previews, world composites), web/tiles/,
  web/tiles-trimmed/, web/data/world.json and web/data/markers/. Large PNGs
  and the tile pyramids go through Git LFS (.gitattributes); JSON stays plain.
- build.py: decide PSD re-export from build/<id>/base.stamp.json (PSD size,
  mtime, SHA-256 recorded at export time) instead of file mtimes, which are
  meaningless after an LFS checkout. Same size + new mtime hashes once and
  refreshes the stamp; --stamp-only writes stamps for existing base.pngs.
- Ignore .DS_Store and .env files; track sources/**/*.jpg through LFS.

Viewer:
- Right-click (long-press) the map for a category menu that drops a draft
  marker under the pointer and enters marker edit mode for that map.
- Marker editing moves out of the side panel into a box anchored next to the
  marker: Enter or a left click anywhere else saves, Esc cancels, Delete
  inside the box; drags save position plus the box contents.
- Maps panel expands on hover, header click pins/unpins (status shown in the
  header), unpinned collapses 2 s after the pointer leaves; Overview is the
  first list entry instead of a separate button. Only the panel scrolls; the
  page never grows past the viewport. Hover tooltips render above markers.

Also includes the pending source additions (overall square base, skirt
textures, AI regeneration tooling and notes), service worker, Caddyfile and
map config updates.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-22 09:46:02 -07:00

112 lines
4.4 KiB
JavaScript

/* Tarkov Maps service worker -- offline and instant start for the viewer.
*
* Strategy by path:
* tiles/, tiles-trimmed/, vendor/ cache-first. Tile URLs carry ?v=<build>
* 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);
}
})());
});