SPB Git

Repository Public

sw.js 1.3 KB 26 lines javascript

1/*2 * Sheet service worker — deliberately small.3 *  - Pages and API calls always go to the network: a reader never sees a stale sheet, and the studio never4 *    works on cached data (there is no offline editing).5 *  - Hashed static files (/_next/static, fonts, icons) are cached after first use.6 *  - When the network is unreachable, navigations get a small offline page.7 */8const CACHE = "sheet-static-v1";9const OFFLINE = "/offline.html";1011self.addEventListener("install", (e) => { e.waitUntil(caches.open(CACHE).then((c) => c.add(OFFLINE)).then(() => self.skipWaiting())); });12self.addEventListener("activate", (e) => {13  e.waitUntil(caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))).then(() => self.clients.claim()));14});15self.addEventListener("fetch", (e) => {16  const req = e.request;17  if (req.method !== "GET") return;18  const url = new URL(req.url);19  if (url.origin !== self.location.origin) return;20  if (url.pathname.startsWith("/_next/static/") || url.pathname.startsWith("/icons/")) {21    e.respondWith(caches.open(CACHE).then(async (c) => (await c.match(req)) || fetch(req).then((res) => { if (res.ok) c.put(req, res.clone()); return res; })));22    return;23  }24  if (req.mode === "navigate" && !url.pathname.startsWith("/admin")) e.respondWith(fetch(req).catch(() => caches.match(OFFLINE)));25});26
Files main
Menu

Theme