-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
125 lines (118 loc) · 3.42 KB
/
sw.js
File metadata and controls
125 lines (118 loc) · 3.42 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
// Service Worker: offline caching and versioned updates
const SITE_VERSION = "20260214-01";
const CACHE_NAME = `open-ar-${SITE_VERSION}`;
const CORE_ASSETS = [
"index.html",
"ar-scene.html",
"landing.html",
"manifest.json",
"assets/style.css?v=20260214-01",
"assets/reset-button.css?v=20260214-01",
"assets/img/Icon/app-icon.svg",
"version.json",
];
async function purgeLegacyCaches() {
const keys = await caches.keys();
await Promise.all(
keys.map(async (k) => {
if (k.startsWith("open-ar-")) return;
try {
await caches.delete(k);
console.log("SW: Purged legacy cache", k);
} catch (e) {}
}),
);
}
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(CORE_ASSETS)),
);
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
(async () => {
const cacheNames = await caches.keys();
await Promise.all(
cacheNames.map((cn) => {
if (cn.startsWith("open-ar-") && cn !== CACHE_NAME) {
return caches.delete(cn);
}
}),
);
await purgeLegacyCaches();
})(),
);
self.clients.claim();
self.clients.matchAll({ includeUncontrolled: true }).then((clients) => {
clients.forEach((c) =>
c.postMessage({ type: "SW_VERSION", version: SITE_VERSION }),
);
});
});
self.addEventListener("fetch", (event) => {
if (event.request.method !== "GET") return;
const req = event.request;
const url = new URL(req.url);
if (url.protocol !== "http:" && url.protocol !== "https:") return;
if (url.origin !== self.location.origin) return;
if (url.pathname.endsWith(".glb") || url.pathname.endsWith(".mind")) {
event.respondWith(
(async () => {
const runtimeCache = await caches.open("open-ar-runtime");
const cached = await runtimeCache.match(req);
const networkPromise = fetch(req)
.then((res) => {
if (res && res.ok) {
runtimeCache.put(req, res.clone()).catch(() => {});
}
return res;
})
.catch((err) => {
if (!cached) console.log("SW: GLB/MIND fetch fail", url.href, err);
return cached || Response.error();
});
if (cached) {
networkPromise.catch(() => {});
return cached;
}
return networkPromise;
})(),
);
return;
}
event.respondWith(
caches.match(req).then((cached) => {
const fetchPromise = fetch(req)
.then((networkResp) => {
if (
networkResp &&
networkResp.status === 200 &&
networkResp.type === "basic" &&
!req.url.startsWith("chrome-extension://")
) {
const clone = networkResp.clone();
caches
.open(CACHE_NAME)
.then((cache) => cache.put(req, clone))
.catch(() => {});
}
return networkResp;
})
.catch((err) => {
if (!cached)
console.log("SW: Network fail & no cache", err, url.href);
return (
cached ||
(req.mode === "navigate" ? caches.match("index.html") : undefined)
);
});
return cached || fetchPromise;
}),
);
});
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});