Spaces:
Build error
Build error
Commit ·
0d2f09e
1
Parent(s): f53ed9e
fix(BUG-12/23): sw.js never caches API responses — static-only cache-first, API always network
Browse files- frontend/sw.js +22 -10
frontend/sw.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
self.addEventListener("install", e => {
|
| 7 |
e.waitUntil(caches.open(CACHE).then(c => c.addAll(STATIC)));
|
|
@@ -17,14 +24,19 @@ self.addEventListener("activate", e => {
|
|
| 17 |
});
|
| 18 |
|
| 19 |
self.addEventListener("fetch", e => {
|
| 20 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if (isApi) {
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
);
|
| 27 |
} else {
|
|
|
|
| 28 |
e.respondWith(caches.match(e.request).then(r => r || fetch(e.request)));
|
| 29 |
}
|
| 30 |
});
|
|
|
|
| 1 |
+
// BUG-12/23 FIX: was caching API responses (health, search, stats) with cache-then-network.
|
| 2 |
+
// Cold-start 503 responses were being served from cache for minutes.
|
| 3 |
+
// Fix: static assets only (cache-first). API calls always network-first, no caching.
|
| 4 |
+
// Never cache health, search, stats, investigate — these must always be fresh.
|
| 5 |
+
const CACHE = "bharatgraph-v4";
|
| 6 |
+
const STATIC = [
|
| 7 |
+
"/","/index.html",
|
| 8 |
+
"/css/design-system.css","/css/components.css",
|
| 9 |
+
"/js/router.js","/js/api.js","/js/components.js",
|
| 10 |
+
"/js/graph.js","/js/timeline.js","/js/evidence_panel.js","/js/app.js"
|
| 11 |
+
];
|
| 12 |
|
| 13 |
self.addEventListener("install", e => {
|
| 14 |
e.waitUntil(caches.open(CACHE).then(c => c.addAll(STATIC)));
|
|
|
|
| 24 |
});
|
| 25 |
|
| 26 |
self.addEventListener("fetch", e => {
|
| 27 |
+
const url = e.request.url;
|
| 28 |
+
// BUG-12/23 FIX: API calls NEVER cached — always fetch live from network
|
| 29 |
+
const isApi = url.includes("hf.space") || url.includes("/api/") ||
|
| 30 |
+
url.includes("/health") || url.includes("/stats") ||
|
| 31 |
+
url.includes("/search") || url.includes("/investigate");
|
| 32 |
if (isApi) {
|
| 33 |
+
// Network only — no cache fallback for API
|
| 34 |
+
e.respondWith(fetch(e.request).catch(() =>
|
| 35 |
+
new Response(JSON.stringify({error:"API temporarily unavailable"}),
|
| 36 |
+
{status:503, headers:{"Content-Type":"application/json"}})
|
| 37 |
+
));
|
| 38 |
} else {
|
| 39 |
+
// Static assets: cache-first
|
| 40 |
e.respondWith(caches.match(e.request).then(r => r || fetch(e.request)));
|
| 41 |
}
|
| 42 |
});
|