Files
Bike-Tinder/app/static/sw.js
Torres 57fae0df49 app: no-build PWA frontend (fidelity port) + image mirror
- app/static/index.html: single-file app — DOM/CSS/stack engine ported
  verbatim from reference/mockup-001-fidelity.html (DEPTH/SC/TY/DIM, drag/
  fly/undo, one .topcard, no-lock commits, stamps, dossier sheet,
  pull-to-close, empty state); production deltas only (UI-SPEC §7): deck
  buffer + cursor refill (40/page), optimistic swipe persistence with
  2s/5s/10s/15s retry queue + toast, badge-once-per-session, real list
  filters + search (server-side), real Matches screen, /img/{id} everywhere
  with placeholder fallback, SW registration, safe areas, desktop centering
- manifest.json + sw.js (precache shell, network-first API, cache-first imgs)
- icons: 192/512/maskable/apple-touch-180, dark tile with the brand
  accent-gradient mark (Pillow)
- app/imgfetch.py: local mirror, hunt's Chrome-120 UA, ~1s pacing,
  skip-existing, .part writes atomic, per-item failure isolation,
  fetched/skipped/failed summary; verified: --limit 15 -> 15 fetched, full
  run 5 more, re-run 0 fetched 20 skipped (20/20 mirrored)
2026-11-10 21:21:27 -04:00

68 lines
1.9 KiB
JavaScript

/* Bike-Tinder service worker.
- Precache the app shell at install; index.html + sw.js are served with
Cache-Control: no-cache so updates are picked up.
- /api/* : network-first; offline -> 503 JSON (the page toasts "Offline").
- /img/* : cache-first with network fill (mirrored thumbnails stay usable
offline once seen).
- Everything else (shell/navigation): network-first, fall back to cache.
*/
const VERSION = 'bike-tinder-v1';
const SHELL = [
'/',
'/manifest.json',
'/apple-touch-icon.png',
'/icons/icon-192.png',
'/icons/icon-512.png',
'/icons/icon-512-maskable.png'
];
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(VERSION)
.then((c) => c.addAll(SHELL))
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys()
.then((keys) => Promise.all(keys.filter((k) => k !== VERSION).map((k) => caches.delete(k))))
.then(() => self.clients.claim())
);
});
self.addEventListener('fetch', (e) => {
const req = e.request;
if (req.method !== 'GET') return;
const url = new URL(req.url);
if (url.pathname.startsWith('/api/')) {
e.respondWith(
fetch(req).catch(() => new Response(JSON.stringify({ error: 'offline' }),
{ status: 503, headers: { 'Content-Type': 'application/json' } }))
);
return;
}
if (url.pathname.startsWith('/img/')) {
e.respondWith(
caches.match(req).then((hit) => hit || fetch(req).then((res) => {
if (res.ok) {
const clone = res.clone();
caches.open(VERSION).then((c) => c.put(req, clone));
}
return res;
}).catch(() => new Response('', { status: 404 })))
);
return;
}
// shell + navigation: network first, cache fallback (offline launch)
e.respondWith(
fetch(req).then((res) => res).catch(() =>
caches.match(req).then((hit) => hit || caches.match('/'))
)
);
});