multica/apps/web/public/sw.js
Naiyuan Qing ba1a90cd8d feat(web): add PWA support for installability and offline fallback
Enable the web app to be installed as a standalone PWA on mobile and
desktop. Uses Next.js built-in Metadata API for manifest generation,
a lightweight service worker for navigation offline fallback, and
proper Apple Web App metadata for iOS support.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 14:43:58 +08:00

40 lines
974 B
JavaScript

const CACHE_NAME = "multica-v1";
// App shell resources to precache
const PRECACHE_URLS = ["/~offline"];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(PRECACHE_URLS))
);
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
// Clean up old caches
event.waitUntil(
caches.keys().then((names) =>
Promise.all(
names
.filter((name) => name !== CACHE_NAME)
.map((name) => caches.delete(name))
)
)
);
self.clients.claim();
});
self.addEventListener("fetch", (event) => {
// Only handle navigation requests (HTML pages)
if (event.request.mode === "navigate") {
event.respondWith(
fetch(event.request).catch(() =>
caches.match("/~offline").then(
(response) =>
response ||
new Response("Offline", { status: 503, statusText: "Offline" })
)
)
);
}
});