-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
66 lines (60 loc) · 1.82 KB
/
Copy pathsw.js
File metadata and controls
66 lines (60 loc) · 1.82 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
// Service worker for the NorCal Touge Spots 3D map.
// Strategy:
// - HTML / JS / CSS: stale-while-revalidate (always fast, refresh in background)
// - JSON data files (routes, tilden_holes, bay_buildings, bay_forest):
// same — cached aggressively
// - Cesium CDN tiles + image tiles: never cached by us; let the browser cache them
//
// Bumping CACHE_VERSION drops all old entries on next activation.
const CACHE_VERSION = "v1-2026-05-04";
const PRECACHE = [
"./",
"./3d.html",
"./index.html",
"./food.html",
"./app-3d.js",
"./app.js",
"./food.js",
"./styles.css",
"./routes.json",
"./food.json",
"./tilden_holes.json",
"./bay_buildings.json",
"./bay_forest.json",
"./manifest.webmanifest",
];
self.addEventListener("install", (e) => {
e.waitUntil(
caches.open(CACHE_VERSION)
.then((c) => c.addAll(PRECACHE).catch(() => {}))
.then(() => self.skipWaiting())
);
});
self.addEventListener("activate", (e) => {
e.waitUntil(
caches.keys()
.then((keys) => Promise.all(keys.filter((k) => k !== CACHE_VERSION).map((k) => caches.delete(k))))
.then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (e) => {
const url = new URL(e.request.url);
// Only handle same-origin GET requests
if (e.request.method !== "GET" || url.origin !== location.origin) return;
// Stale-while-revalidate
e.respondWith(
caches.open(CACHE_VERSION).then((cache) => {
return cache.match(e.request).then((cached) => {
const fetchPromise = fetch(e.request)
.then((response) => {
if (response && response.status === 200) {
cache.put(e.request, response.clone()).catch(() => {});
}
return response;
})
.catch(() => cached);
return cached || fetchPromise;
});
})
);
});