@3cr/viewer-browser 0.0.151 → 0.0.153
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/playground/index.html +40 -0
- package/playground/sw.js +15 -0
package/package.json
CHANGED
package/playground/index.html
CHANGED
|
@@ -7,6 +7,12 @@
|
|
|
7
7
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
|
8
8
|
<script src="https://cdn.jsdelivr.net/npm/@3cr/viewer-browser@{{version}}/dist/Viewer3CR.umd.js"> </script>
|
|
9
9
|
<script type="module">
|
|
10
|
+
function invokeServiceWorkerUpdateFlow(registration) {
|
|
11
|
+
if (registration.waiting) {
|
|
12
|
+
// let waiting Service Worker know it should became active
|
|
13
|
+
registration.waiting.postMessage('SKIP_WAITING')
|
|
14
|
+
}
|
|
15
|
+
}
|
|
10
16
|
function initServiceWorker() {
|
|
11
17
|
// Only enable service worker in production
|
|
12
18
|
if ("serviceWorker" in navigator) {
|
|
@@ -17,10 +23,44 @@
|
|
|
17
23
|
"[CHECK]: Service_Worker_Registered_With_Scope",
|
|
18
24
|
registration.scope
|
|
19
25
|
);
|
|
26
|
+
// ensure the case when the updatefound event was missed is also handled
|
|
27
|
+
// by re-invoking the prompt when there's a waiting Service Worker
|
|
28
|
+
if (registration.waiting) {
|
|
29
|
+
invokeServiceWorkerUpdateFlow(registration)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// detect Service Worker update available and wait for it to become installed
|
|
33
|
+
registration.addEventListener('updatefound', () => {
|
|
34
|
+
if (registration.installing) {
|
|
35
|
+
// wait until the new Service worker is actually installed (ready to take over)
|
|
36
|
+
registration.installing.addEventListener('statechange', () => {
|
|
37
|
+
if (registration.waiting) {
|
|
38
|
+
// if there's an existing controller (previous Service Worker), show the prompt
|
|
39
|
+
if (navigator.serviceWorker.controller) {
|
|
40
|
+
invokeServiceWorkerUpdateFlow(registration)
|
|
41
|
+
} else {
|
|
42
|
+
// otherwise it's the first install, nothing to do
|
|
43
|
+
console.log('Service Worker initialized for the first time')
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
let refreshing = false;
|
|
51
|
+
|
|
52
|
+
// detect controller change and refresh the page
|
|
53
|
+
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
|
54
|
+
if (!refreshing) {
|
|
55
|
+
window.location.reload()
|
|
56
|
+
refreshing = true
|
|
57
|
+
}
|
|
58
|
+
})
|
|
20
59
|
})
|
|
21
60
|
.catch((error) => {
|
|
22
61
|
console.error("[ERR]: Service_Worker_Registration_Failed:", error);
|
|
23
62
|
});
|
|
63
|
+
|
|
24
64
|
}
|
|
25
65
|
}
|
|
26
66
|
|
package/playground/sw.js
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
// Service Worker-based solution
|
|
2
|
+
self.addEventListener("activate", async () => {
|
|
3
|
+
// after we've taken over, iterate over all the current clients (windows)
|
|
4
|
+
const tabs = await self.clients.matchAll({ type: "window" });
|
|
5
|
+
tabs.forEach((tab) => {
|
|
6
|
+
// ...and refresh each one of them
|
|
7
|
+
tab.navigate(tab.url);
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
self.addEventListener("message", (event) => {
|
|
12
|
+
if (event.data === "SKIP_WAITING") {
|
|
13
|
+
self.skipWaiting().then((r) => {});
|
|
14
|
+
}
|
|
15
|
+
});
|
|
1
16
|
self.addEventListener("fetch", (event) => {
|
|
2
17
|
event.respondWith(handleServerRequest(event.request));
|
|
3
18
|
});
|