@jon49/sw 0.12.13 → 0.13.0
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 +1 -3
- package/lib/routes.ts +24 -11
- package/package.json +1 -1
package/lib/new-app-notifier.ts
CHANGED
|
@@ -4,10 +4,8 @@ let refreshing = false
|
|
|
4
4
|
// Here we reload the page
|
|
5
5
|
navigator.serviceWorker.addEventListener('controllerchange', function () {
|
|
6
6
|
if (refreshing) return
|
|
7
|
-
let url = new URL(window.location.href)
|
|
8
|
-
url.searchParams.set("refresh", "hard")
|
|
9
|
-
window.location.href = url.href
|
|
10
7
|
refreshing = true
|
|
8
|
+
window.location.reload()
|
|
11
9
|
})
|
|
12
10
|
|
|
13
11
|
/// Checks if there is a new version of the app available
|
package/lib/routes.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
let { html, links } =
|
|
1
|
+
let { html, links, db, globalDb } =
|
|
2
2
|
// @ts-ignore
|
|
3
|
-
self.app as { links: { file: string, url: string }[], html: Function }
|
|
3
|
+
self.app as { links: { file: string, url: string }[], html: Function, db: any, globalDb: any }
|
|
4
4
|
|
|
5
5
|
if (!links) {
|
|
6
6
|
console.error("Expecting links defined with `self.app.links`, but found none.")
|
|
@@ -99,6 +99,7 @@ export async function findRoute(url: URL, method: unknown) {
|
|
|
99
99
|
}
|
|
100
100
|
// @ts-ignore
|
|
101
101
|
for (const r of self.app.routes) {
|
|
102
|
+
// @ts-ignore
|
|
102
103
|
if (r.file
|
|
103
104
|
&& (r.route instanceof RegExp && r.route.test(url.pathname)
|
|
104
105
|
|| (r.route instanceof Function && r.route(url)))) {
|
|
@@ -144,7 +145,25 @@ interface ExectuteHandlerOptions {
|
|
|
144
145
|
async function executeHandler({ url, req, event }: ExectuteHandlerOptions) : Promise<Response> {
|
|
145
146
|
let method = req.method.toLowerCase()
|
|
146
147
|
let isPost = method === "post"
|
|
147
|
-
if (!isPost
|
|
148
|
+
if (!isPost) {
|
|
149
|
+
if (!url.pathname.endsWith("/")) {
|
|
150
|
+
return cacheResponse(url.pathname, event)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (url.searchParams.get("login") === "success") {
|
|
154
|
+
await globalDb.setLoggedIn(true)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let lastUrl: string | undefined
|
|
158
|
+
if (url.pathname === "/web/" && (lastUrl = (await db.get("last-url"))?.url)) {
|
|
159
|
+
url = new URL(lastUrl)
|
|
160
|
+
} else if (url.searchParams.has("pushUrl") || url.searchParams.has("hz")) {
|
|
161
|
+
let saveUrl = new URL(url.href)
|
|
162
|
+
saveUrl.searchParams.delete("pushUrl")
|
|
163
|
+
saveUrl.searchParams.delete("hz")
|
|
164
|
+
await db.set("last-url", { url: saveUrl.href }, { sync: false })
|
|
165
|
+
}
|
|
166
|
+
}
|
|
148
167
|
|
|
149
168
|
let handlers =
|
|
150
169
|
<RouteHandler<RouteGetArgs | RoutePostArgs> | null>
|
|
@@ -175,13 +194,8 @@ async function executeHandler({ url, req, event }: ExectuteHandlerOptions) : Pro
|
|
|
175
194
|
}
|
|
176
195
|
|
|
177
196
|
if (isHtml(result)) {
|
|
178
|
-
if (
|
|
179
|
-
|
|
180
|
-
if (referrer.pathname.startsWith("/web")
|
|
181
|
-
&& url.searchParams.get("refresh") !== "hard"
|
|
182
|
-
&& req.headers.get("HF-Request") !== "true") {
|
|
183
|
-
result = html`<template>${result}</template>`
|
|
184
|
-
}
|
|
197
|
+
if (url.searchParams.has("hz")) {
|
|
198
|
+
result = html`<template>${result}</template>`
|
|
185
199
|
}
|
|
186
200
|
|
|
187
201
|
return streamResponse({
|
|
@@ -373,4 +387,3 @@ type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
|
|
|
373
387
|
export type Route = RequireAtLeastOne<Route_, "file" | "get" | "post">
|
|
374
388
|
export type RoutePage = Pick<Route_, "get" | "post">
|
|
375
389
|
|
|
376
|
-
|