Files
famapp/public/sw.js
T
ginnoirandClaude Sonnet 4.6 8aab25e0ac Implement PWA shell (task 50)
Add web manifest, placeholder icons, app-shell service worker, and
install prompt so famapp is installable on iOS and Android home screens.

- public/manifest.webmanifest — name, short_name, standalone display,
  start_url /, all four icon sizes (192/384/512 + maskable)
- public/icon-{180,192,384,512,512-maskable}.png — indigo placeholder
  squares generated by scripts/generate-icons.mjs (pnpm gen:icons)
- public/icon.svg — house-icon SVG source for future branded export
- public/sw.js — hand-rolled app-shell SW: precaches offline.html,
  cache-first for /_next/static/, network-first navigation with offline
  fallback, network-only for /api/*
- public/offline.html — branded offline fallback page
- src/components/pwa-register.tsx — client SW registration
- src/components/install-prompt.tsx — dismissible install banner:
  beforeinstallprompt on Android, "Add to Home Screen" hint on iOS
- Root layout: viewport themeColor, manifest/appleWebApp metadata,
  apple-touch-icon, mounts PwaRegister + InstallPrompt

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-06 16:00:02 -05:00

70 lines
1.7 KiB
JavaScript

// famapp service worker — app-shell pattern
// Caches a minimal offline fallback on install.
// Navigation requests: network-first with offline fallback.
// Next.js static chunks: cache-first.
// API routes: network-only (pass through).
const CACHE = "famapp-shell-v1";
const OFFLINE = "/offline.html";
// --- install: precache offline shell ---
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE).then((cache) => cache.add(OFFLINE))
);
self.skipWaiting();
});
// --- activate: evict old caches ---
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
)
);
self.clients.claim();
});
// --- fetch ---
self.addEventListener("fetch", (event) => {
const { request } = event;
if (request.method !== "GET") return;
const url = new URL(request.url);
// API routes — always go to network, never cache.
if (url.pathname.startsWith("/api/")) return;
// NextAuth — network-only.
if (url.pathname.startsWith("/api/auth/")) return;
// Next.js immutable static assets — cache-first.
if (url.pathname.startsWith("/_next/static/")) {
event.respondWith(
caches.match(request).then(
(cached) =>
cached ??
fetch(request).then((res) => {
const clone = res.clone();
caches.open(CACHE).then((c) => c.put(request, clone));
return res;
})
)
);
return;
}
// Navigation requests — network-first, serve offline shell if unreachable.
if (request.mode === "navigate") {
event.respondWith(
fetch(request).catch(() => caches.match(OFFLINE))
);
return;
}
});