@jon49/sw 0.14.6 → 0.14.7
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/lib/new-app-notifier.ts +54 -41
- package/package.json +1 -1
package/lib/new-app-notifier.ts
CHANGED
|
@@ -3,9 +3,9 @@ let refreshing = false
|
|
|
3
3
|
// The event listener that is fired when the service worker updates
|
|
4
4
|
// Here we reload the page
|
|
5
5
|
navigator.serviceWorker.addEventListener('controllerchange', function () {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
if (refreshing) return
|
|
7
|
+
refreshing = true
|
|
8
|
+
window.location.reload()
|
|
9
9
|
})
|
|
10
10
|
|
|
11
11
|
/// Checks if there is a new version of the app available
|
|
@@ -13,54 +13,67 @@ navigator.serviceWorker.addEventListener('controllerchange', function () {
|
|
|
13
13
|
/// @returns void
|
|
14
14
|
/// @example
|
|
15
15
|
/// notifier((state, worker) => {
|
|
16
|
-
///
|
|
17
|
-
///
|
|
18
|
-
///
|
|
19
|
-
///
|
|
20
|
-
///
|
|
21
|
-
///
|
|
16
|
+
/// if (state !== "waiting") {
|
|
17
|
+
/// // Show notification
|
|
18
|
+
/// }
|
|
19
|
+
/// if (/* user confirms update */) {
|
|
20
|
+
/// worker.postMessage("skipWaiting")
|
|
21
|
+
/// }
|
|
22
22
|
/// })
|
|
23
23
|
function notifier(fn: (state: "" | "waiting", worker: ServiceWorker) => void) {
|
|
24
|
-
|
|
24
|
+
let newWorker: ServiceWorker | undefined | null
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
26
|
+
if ('serviceWorker' in navigator) {
|
|
27
|
+
// Register the service worker
|
|
28
|
+
navigator.serviceWorker.register('/web/sw.js').then(reg => {
|
|
29
|
+
if ((newWorker = reg.waiting)?.state === 'installed') {
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
fn("waiting", newWorker)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
reg.addEventListener('updatefound', () => {
|
|
35
|
+
// An updated service worker has appeared in reg.installing!
|
|
36
|
+
newWorker = reg.installing
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
newWorker?.addEventListener('statechange', () => {
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
// There is a new service worker available, show the notification
|
|
41
|
+
if (newWorker?.state === "installed" && navigator.serviceWorker.controller) {
|
|
42
|
+
navigator.serviceWorker.controller.postMessage("installed")
|
|
43
|
+
fn("", newWorker)
|
|
44
|
+
}
|
|
45
45
|
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
46
|
})
|
|
49
|
-
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
notifier(notifyUserAboutNewVersion)
|
|
53
53
|
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
window.app = window.app || {}
|
|
54
|
+
function skipWaiting(id: string) {
|
|
55
|
+
let el = document.getElementById(id)
|
|
56
|
+
el?.addEventListener("click", (e: MouseEvent) => {
|
|
57
|
+
e.preventDefault()
|
|
59
58
|
// @ts-ignore
|
|
60
|
-
window.app.sw
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
59
|
+
window.app.sw.postMessage({ action: 'skipWaiting' })
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function notifyUserAboutNewVersion(state = "", worker: ServiceWorker) {
|
|
64
|
+
let nav = document.getElementById("sw-message")
|
|
65
|
+
nav?.insertAdjacentHTML("afterbegin", `<div class=inline><a id=skipWaiting href="#">Click here to update your app.</a></div>`)
|
|
66
|
+
// @ts-ignore
|
|
67
|
+
skipWaiting("skipWaiting")
|
|
68
|
+
// @ts-ignore
|
|
69
|
+
window.app = window.app || {}
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
window.app.sw = worker
|
|
72
|
+
if (state === "waiting") return
|
|
73
|
+
// Publish custom event for "user-messages" to display a toast.
|
|
74
|
+
document.dispatchEvent(new CustomEvent("user-messages", {
|
|
75
|
+
detail: { html: `A new version of the app is available. <a id=skipWaiting1 href="#">Click to update the app.</a>` }
|
|
76
|
+
}))
|
|
77
|
+
// @ts-ignore
|
|
78
|
+
skipWaiting("skipWaiting1")
|
|
66
79
|
}
|