-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
151 lines (140 loc) · 5.31 KB
/
Copy pathsw.js
File metadata and controls
151 lines (140 loc) · 5.31 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const SHELL_CACHE = "proxaid-shell-v10";
const DATA_CACHE = "proxaid-data-v10";
const DB_NAME = "proxaid-offline-v1";
const SHELL_FILES = [
"./",
"./index.html",
"./styles.css",
"./app.js",
"./manifest.webmanifest",
"./assets/vendor/leaflet.css",
"./assets/vendor/leaflet.js",
"./assets/vendor/qrcode.mjs",
"./assets/vendor/qrcode_utf8.mjs",
"./assets/icon.svg",
"./assets/icon-180.png",
"./assets/icon-192.png",
"./assets/icon-512.png",
"./404.html",
"./data/catalog.json",
"./data/core.json",
"./data/packs/hu-west.json",
"./data/packs/hu-west-osm.json",
"./data/packs/hu-west-osm-events.json",
"./data/maps/hu-zala-south.geojson",
"./data/first-aid.json",
"./data/regions.json",
"./data/taxonomy.json",
"./data/sources.json",
"./README.md",
"./README_hu.md",
"./user-guide.md",
"./sources.md",
"./user-guide_hu.md",
"./sources_hu.md",
"./changelog.md",
"./changelog_hu.md",
"./third-party-notices.md",
"./assets/audio/cpr_hands_only_hu.mp3"
];
const scoped = (path) => new URL(path, self.registration.scope).toString();
self.addEventListener("install", (event) => {
event.waitUntil(caches.open(SHELL_CACHE).then((cache) => cache.addAll(SHELL_FILES.map(scoped))).then(() => self.skipWaiting()));
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys()
.then((keys) => Promise.all(keys.filter((key) => ![SHELL_CACHE, DATA_CACHE].includes(key)).map((key) => caches.delete(key))))
.then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (event) => {
if (event.request.method !== "GET") return;
const url = new URL(event.request.url);
if (url.origin !== self.location.origin) return;
if (event.request.mode === "navigate") {
event.respondWith(networkFirst(event.request, SHELL_CACHE, scoped("./index.html")));
return;
}
if (url.pathname.includes("/data/")) {
event.respondWith(networkFirst(event.request, DATA_CACHE));
return;
}
event.respondWith(cacheFirst(event.request, SHELL_CACHE));
});
self.addEventListener("message", (event) => {
if (event.data?.type === "SYNC_NOW") event.waitUntil(refreshDataCache());
if (event.data?.type === "SKIP_WAITING") self.skipWaiting();
});
self.addEventListener("periodicsync", (event) => {
if (event.tag === "proxaid-monthly-sync") event.waitUntil(refreshDataCache());
});
self.addEventListener("sync", (event) => {
if (event.tag === "proxaid-reconnect-sync") event.waitUntil(refreshDataCache());
});
async function cacheFirst(request, cacheName) {
const cached = await caches.match(request);
if (cached) return cached;
try {
const response = await fetch(request);
if (response.ok) (await caches.open(cacheName)).put(request, response.clone());
return response;
} catch {
return new Response("Offline", { status: 503, headers: { "Content-Type": "text/plain; charset=utf-8" } });
}
}
async function networkFirst(request, cacheName, fallbackUrl) {
try {
const response = await fetch(request);
if (response.ok) (await caches.open(cacheName)).put(request, response.clone());
return response;
} catch {
return (await caches.match(request)) || (fallbackUrl ? await caches.match(fallbackUrl) : null) || new Response("Offline", { status: 503 });
}
}
async function refreshDataCache() {
const cache = await caches.open(DATA_CACHE);
const installedPackIds = await readInstalledPackIds();
const catalogRequest = new Request(scoped("./data/catalog.json"), { cache: "no-store" });
const catalogResponse = await fetch(catalogRequest);
if (!catalogResponse.ok) throw new Error("Catalog refresh failed");
await cache.put(scoped("./data/catalog.json"), catalogResponse.clone());
const catalog = await catalogResponse.json();
for (const pack of catalog.packs ?? []) {
if (!(pack.required || pack.defaultInstall || installedPackIds.has(pack.id)) || Number(pack.estimatedBytes ?? 0) > 5 * 1024 * 1024) continue;
const request = new Request(new URL(pack.url, self.registration.scope), { cache: "no-store" });
const response = await fetch(request);
if (response.ok) await cache.put(request, response);
}
for (const descriptor of [...(catalog.maps ?? []), ...(catalog.guides ?? []), ...(catalog.audio ?? [])]) {
if (!descriptor.defaultInstall && descriptor.id !== "cpr-hands-only-hu") continue;
const request = new Request(new URL(descriptor.url, self.registration.scope), { cache: "no-store" });
const response = await fetch(request);
if (response.ok) await cache.put(request, response);
}
}
async function readInstalledPackIds() {
if (!("indexedDB" in self)) return new Set();
return new Promise((resolve) => {
const request = indexedDB.open(DB_NAME);
request.onupgradeneeded = () => request.transaction?.abort();
request.onerror = () => resolve(new Set());
request.onsuccess = () => {
const db = request.result;
if (!db.objectStoreNames.contains("meta")) {
db.close();
resolve(new Set());
return;
}
const getRequest = db.transaction("meta", "readonly").objectStore("meta").get("installedPackIds");
getRequest.onsuccess = () => {
db.close();
resolve(new Set(Array.isArray(getRequest.result?.value) ? getRequest.result.value : []));
};
getRequest.onerror = () => {
db.close();
resolve(new Set());
};
};
});
}