|
| 1 | +const CACHE_NAME = "opencode-v1" |
| 2 | +const STATIC_ASSETS = [ |
| 3 | + "/", |
| 4 | + "/favicon.svg", |
| 5 | + "/favicon-96x96.png", |
| 6 | + "/apple-touch-icon.png", |
| 7 | + "/web-app-manifest-192x192.png", |
| 8 | + "/web-app-manifest-512x512.png", |
| 9 | +] |
| 10 | + |
| 11 | +self.addEventListener("install", (event) => { |
| 12 | + event.waitUntil( |
| 13 | + caches.open(CACHE_NAME).then((cache) => { |
| 14 | + return cache.addAll(STATIC_ASSETS).catch((err) => { |
| 15 | + console.warn("Failed to cache some assets:", err) |
| 16 | + }) |
| 17 | + }), |
| 18 | + ) |
| 19 | + self.skipWaiting() |
| 20 | +}) |
| 21 | + |
| 22 | +self.addEventListener("activate", (event) => { |
| 23 | + event.waitUntil( |
| 24 | + caches.keys().then((keys) => { |
| 25 | + return Promise.all( |
| 26 | + keys.filter((key) => key !== CACHE_NAME && key.startsWith("opencode-")).map((key) => caches.delete(key)), |
| 27 | + ) |
| 28 | + }), |
| 29 | + ) |
| 30 | + self.clients.claim() |
| 31 | +}) |
| 32 | + |
| 33 | +self.addEventListener("fetch", (event) => { |
| 34 | + const url = new URL(event.request.url) |
| 35 | + |
| 36 | + // Skip non-GET requests |
| 37 | + if (event.request.method !== "GET") return |
| 38 | + |
| 39 | + // Skip API requests and SSE connections |
| 40 | + if (url.pathname.startsWith("/api/") || url.pathname.startsWith("/event")) return |
| 41 | + |
| 42 | + // Skip cross-origin requests |
| 43 | + if (url.origin !== self.location.origin) return |
| 44 | + |
| 45 | + // Network-first for HTML (app shell) |
| 46 | + if (event.request.mode === "navigate" || event.request.headers.get("accept")?.includes("text/html")) { |
| 47 | + event.respondWith( |
| 48 | + fetch(event.request) |
| 49 | + .then((response) => { |
| 50 | + const clone = response.clone() |
| 51 | + caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)) |
| 52 | + return response |
| 53 | + }) |
| 54 | + .catch(() => caches.match(event.request).then((cached) => cached || caches.match("/"))), |
| 55 | + ) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // Cache-first for hashed assets (Vite adds content hashes to /assets/*) |
| 60 | + if (url.pathname.startsWith("/assets/")) { |
| 61 | + event.respondWith( |
| 62 | + caches.match(event.request).then((cached) => { |
| 63 | + if (cached) return cached |
| 64 | + return fetch(event.request).then((response) => { |
| 65 | + if (response.ok) { |
| 66 | + const clone = response.clone() |
| 67 | + caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)) |
| 68 | + } |
| 69 | + return response |
| 70 | + }) |
| 71 | + }), |
| 72 | + ) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + // Stale-while-revalidate for unhashed static assets (favicon, icons, etc.) |
| 77 | + // Serves cached version immediately but updates cache in background |
| 78 | + if (url.pathname.match(/\.(js|css|png|jpg|jpeg|svg|gif|webp|woff|woff2|ttf|eot|ico|aac|mp3|wav)$/)) { |
| 79 | + event.respondWith( |
| 80 | + caches.match(event.request).then((cached) => { |
| 81 | + const fetchPromise = fetch(event.request).then((response) => { |
| 82 | + if (response.ok) { |
| 83 | + const clone = response.clone() |
| 84 | + caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)) |
| 85 | + } |
| 86 | + return response |
| 87 | + }) |
| 88 | + return cached || fetchPromise |
| 89 | + }), |
| 90 | + ) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + // Network-first for everything else |
| 95 | + event.respondWith( |
| 96 | + fetch(event.request) |
| 97 | + .then((response) => { |
| 98 | + if (response.ok) { |
| 99 | + const clone = response.clone() |
| 100 | + caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)) |
| 101 | + } |
| 102 | + return response |
| 103 | + }) |
| 104 | + .catch(() => caches.match(event.request)), |
| 105 | + ) |
| 106 | +}) |
0 commit comments