Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
71 lines (66 loc) · 2.25 KB
/
Copy pathsw.js
File metadata and controls
71 lines (66 loc) · 2.25 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
/* ТЕЛЕМАСТЕР — service worker: сайт ставится как приложение и живёт офлайн.
ВАЖНО: при смене ?v= у ассетов обновить список ASSETS и CACHE ниже. */
const CACHE = "tm-v92";
const ASSETS = [
"./",
"./index.html",
"./manifest.json",
"./css/style.css?v=62",
"./js/main.js?v=71",
"./js/constellation.js?v=39",
"./js/pcb-field.js?v=1",
"./js/data.js?v=2",
"./js/config.js",
"./js/circuit-bg.js?v=6",
"./img/icon-192.png",
"./img/icon-512.png",
];
self.addEventListener("install", (e) => {
e.waitUntil(
// cache:"reload" — не берём index.html и ассеты из HTTP-кэша браузера,
// иначе в новый CACHE попадает старый HTML и проверка версий циклит
caches.open(CACHE).then((c) =>
c.addAll(ASSETS.map((u) => new Request(u, { cache: "reload" })))
).then(() => self.skipWaiting())
);
});
self.addEventListener("activate", (e) => {
e.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
)
.then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (e) => {
const req = e.request;
if (req.method !== "GET" || !req.url.startsWith(self.location.origin)) return;
// навигация/HTML — всегда сеть (иначе обновления не доходят никогда),
// офлайн-фолбэк на закэшированную оболочку
if (req.mode === "navigate" || (req.headers.get("accept") || "").includes("text/html")) {
e.respondWith(
fetch(req)
.then((res) => {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(req, copy));
return res;
})
.catch(() => caches.match("./index.html"))
);
return;
}
// ассеты с ?v= не меняют содержимое — cache-first безопасен
e.respondWith(
caches.match(req).then(
(hit) =>
hit ||
fetch(req).then((res) => {
const copy = res.clone();
caches.open(CACHE).then((c) => c.put(req, copy));
return res;
})
)
);
});