-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
118 lines (105 loc) · 3.09 KB
/
sw.js
File metadata and controls
118 lines (105 loc) · 3.09 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Y-Sight service worker
- Cache-first for core shell
- Network-first for everything else (with cache fallback)
Bump CACHE_VERSION when you change any core asset.
*/
// Bump this on every deploy that changes *any* cached asset.
// Consider setting it to a date stamp like "2026-02-15-1".
const CACHE_VERSION = "v2";
const CORE_CACHE = `y-sight-core-${CACHE_VERSION}`;
// Keep this list small and stable.
const CORE_ASSETS = [
"./",
"./index.html",
"./manifest.webmanifest",
"./sw.js",
"./404.html",
"./robots.txt",
"./icons/icon-192.png",
"./icons/icon-512.png",
"./icons/maskable-192.png",
"./icons/maskable-512.png"
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CORE_CACHE).then((cache) => cache.addAll(CORE_ASSETS))
);
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys
.filter((k) => k.startsWith("y-sight-") && k !== CORE_CACHE)
.map((k) => caches.delete(k))
)
)
);
self.clients.claim();
});
self.addEventListener("fetch", (event) => {
const req = event.request;
// Only handle GET.
if (req.method !== "GET") return;
const url = new URL(req.url);
// Only same-origin.
if (url.origin !== self.location.origin) return;
const accept = req.headers.get("accept") || "";
const isHTML = req.mode === "navigate" || accept.includes("text/html");
// 1) Navigations / HTML: NETWORK-FIRST to prevent stale app shell winning.
// Fallback to cached index.html when offline.
if (isHTML) {
event.respondWith(
(async () => {
const cache = await caches.open(CORE_CACHE);
try {
const fresh = await fetch(req, { cache: "no-store" });
if (fresh && fresh.ok) {
// Cache the latest HTML so offline still works.
cache.put("./index.html", fresh.clone());
}
return fresh;
} catch (e) {
return (
(await cache.match("./index.html")) ||
(await caches.match("./index.html"))
);
}
})()
);
return;
}
// 2) Cache-first for core static assets (icons, manifest, etc.)
// If not cached yet, fetch and populate cache.
const isCoreAsset = CORE_ASSETS.some((p) => url.pathname.endsWith(p.replace("./", "")));
if (isCoreAsset) {
event.respondWith(
(async () => {
const cached = await caches.match(req);
if (cached) return cached;
const res = await fetch(req);
if (res && res.ok) {
const cache = await caches.open(CORE_CACHE);
cache.put(req, res.clone());
}
return res;
})()
);
return;
}
// 3) Everything else: NETWORK-FIRST with cache fallback.
event.respondWith(
(async () => {
const cache = await caches.open(CORE_CACHE);
try {
const res = await fetch(req);
if (res && res.ok) cache.put(req, res.clone());
return res;
} catch (e) {
return (await cache.match(req)) || (await caches.match(req));
}
})()
);
});