@jon49/sw 0.14.5 → 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/lib/routes.middleware.ts +20 -7
- 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
|
}
|
package/lib/routes.middleware.ts
CHANGED
|
@@ -138,12 +138,25 @@ async function executeHandler({ url, req, ctx }: ExectuteHandlerOptions): Promis
|
|
|
138
138
|
const data = await getData(req)
|
|
139
139
|
let query = searchParams<{ handler?: string }>(url)
|
|
140
140
|
let args = { req, data, query }
|
|
141
|
-
let result
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
141
|
+
let result
|
|
142
|
+
try {
|
|
143
|
+
result = await (
|
|
144
|
+
handlers instanceof Function
|
|
145
|
+
? handlers(args)
|
|
146
|
+
: (call(handlers[query.handler ?? ""], args)
|
|
147
|
+
|| call(handlers[method], args)
|
|
148
|
+
|| Promise.reject("I'm sorry, I didn't understand where to route your request.")))
|
|
149
|
+
} catch (e: any) {
|
|
150
|
+
if (e.reasons) {
|
|
151
|
+
ctx.messages =
|
|
152
|
+
e.reasons.map((x: any) => x.reason.reasons).flat().map((x: any) => x.reason).flat()
|
|
153
|
+
return {
|
|
154
|
+
status: 204,
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
throw e
|
|
158
|
+
}
|
|
159
|
+
}
|
|
147
160
|
|
|
148
161
|
if (!result) {
|
|
149
162
|
return isPost
|
|
@@ -309,4 +322,4 @@ type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
|
|
|
309
322
|
}[Keys]
|
|
310
323
|
|
|
311
324
|
export type Route = RequireAtLeastOne<Route_, "file" | "get" | "post">
|
|
312
|
-
export type RoutePage = Pick<Route_, "get" | "post">
|
|
325
|
+
export type RoutePage = Pick<Route_, "get" | "post">
|