Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
66 lines (61 loc) · 2.11 KB
/
Copy pathsw.js
File metadata and controls
66 lines (61 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* THE COLD FRONT — service worker (v1.167.0)
Conservative by design: pages stay network-first (a fan never sees a
stale score because of this file), versioned static assets ride
stale-while-revalidate, and every cross-origin request (live scores,
weather, market APIs) bypasses the worker entirely — the app's own
snapshot and localStorage fallbacks own those failure modes. */
const CACHE = "cf-shell-1.167.0";
const SHELL = [
"./",
"./index.html",
"./manifest.webmanifest",
"./img/favicon.svg",
"./img/icon-192.png",
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE).then((cache) => cache.addAll(SHELL)).then(() => self.skipWaiting())
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys()
.then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
.then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (event) => {
const req = event.request;
if (req.method !== "GET") return;
const url = new URL(req.url);
if (url.origin !== self.location.origin) return; // live APIs: network only
// Pages + data: network-first, cache is the offline fallback only.
if (req.mode === "navigate" || url.pathname.endsWith(".html") || url.pathname.includes("/data/")) {
event.respondWith(
fetch(req)
.then((res) => {
const copy = res.clone();
caches.open(CACHE).then((cache) => cache.put(req, copy));
return res;
})
.catch(() => caches.match(req).then((hit) => hit || caches.match("./index.html")))
);
return;
}
// Same-origin static (CSS/JS/images — URLs are ?v= versioned):
// serve the cache instantly, refresh it in the background.
event.respondWith(
caches.match(req).then((hit) => {
const network = fetch(req)
.then((res) => {
if (res && res.ok) {
const copy = res.clone();
caches.open(CACHE).then((cache) => cache.put(req, copy));
}
return res;
})
.catch(() => hit);
return hit || network;
})
);
});