Files
projax/web/static/sw.js
mAi 1d5db0fe7b feat(phase 3j pwa): manifest + service worker + icons → installable PWA
- web/static/manifest.webmanifest: name/short_name/start_url=/dashboard/
  display=standalone/theme_color/background_color + three icons (192, 512,
  512-maskable with ~12% safe-zone padding)
- web/static/sw.js: minimal SW — install caches /static/* shell assets,
  fetch is network-first with cache fallback on GETs only, skips /mcp/
  and non-GETs entirely. CACHE_NAME versioned for clean activate-time
  prune.
- cmd/icongen: stdlib-only generator that produces the three PNG icons
  from a stylised "p" monogram. Run once at brand-change, commit output.
- web.init() registers .webmanifest → application/manifest+json with
  mime.AddExtensionType so Chrome accepts the manifest at all
- layout.tmpl + login.tmpl: manifest link, apple-touch-icon, theme-color,
  apple-mobile-web-app-* metas, inline SW-register on load (silent on
  failure — older browsers still work)
- design.md gets §"PWA install (Phase 3j)"; CLAUDE.md "Out of scope"
  drops the Phase-3j line and adds push/background-sync as the
  remaining Otto-PWA territory
- 4 new tests cover manifest MIME, sw.js delivery, all 3 icons, layout
  meta tags
2026-05-15 19:32:48 +02:00

60 lines
1.9 KiB
JavaScript

// projax service worker — Phase 3j install affordance only.
//
// Strategy:
// * On install: pre-cache /static/style.css + manifest + icons so the
// standalone shell renders even when offline.
// * On fetch (GET only): network-first. On network failure, fall back to
// cache. POSTs, the /mcp/ surface, and everything else go straight to the
// network so writeback semantics are preserved.
// * On activate: prune stale caches from earlier versions.
//
// This is NOT full offline mode. It's enough to make the icon work as a
// real PWA + keep static assets warm. Mutations (CalDAV / Gitea writeback)
// still require connectivity.
const CACHE_NAME = 'projax-shell-v1';
const SHELL_ASSETS = [
'/static/style.css',
'/static/manifest.webmanifest',
'/static/icon-192.png',
'/static/icon-512.png',
'/static/icon-maskable.png',
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(SHELL_ASSETS))
);
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 req = event.request;
// Only GET — never cache POST/PATCH/DELETE/PUT (writeback + MCP RPC).
if (req.method !== 'GET') return;
// Skip MCP RPC entirely — it's stateful JSON-RPC.
if (new URL(req.url).pathname.startsWith('/mcp/')) return;
event.respondWith(
fetch(req)
.then((res) => {
// Stash successful GETs into the cache for offline fallback.
if (res.ok && req.url.startsWith(self.location.origin)) {
const copy = res.clone();
caches.open(CACHE_NAME).then((c) => c.put(req, copy));
}
return res;
})
.catch(() => caches.match(req))
);
});