Files
twitter-lite/public/sw.js
T

48 lines
1.4 KiB
JavaScript

// Only this generic page is cached. Authenticated HTML, API responses, and
// mutations always use the network. Bump the version when offline.html changes.
const CACHE = "workspace-offline-v1";
const OFFLINE_URL = "/offline.html";
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE).then((cache) => cache.add(new Request(OFFLINE_URL, { cache: "reload" }))),
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
(async () => {
const keys = await caches.keys();
await Promise.all(
keys
.filter((key) => key.startsWith("workspace-offline-") && key !== CACHE)
.map((key) => caches.delete(key)),
);
await self.clients.claim();
})(),
);
});
self.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(request.url);
if (
request.method !== "GET" ||
request.mode !== "navigate" ||
url.origin !== self.location.origin ||
url.pathname.startsWith("/api/") ||
url.pathname.startsWith("/_serverFn/") ||
url.pathname.startsWith("/oauth/") ||
url.pathname === "/mcp" ||
url.pathname === "/health"
) {
return;
}
event.respondWith(
fetch(request).catch(async () => {
const cache = await caches.open(CACHE);
return (await cache.match(OFFLINE_URL)) ?? Response.error();
}),
);
});