@jon49/sw 0.12.14 → 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/routes.ts +23 -5
- package/package.json +1 -1
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,7 +194,7 @@ async function executeHandler({ url, req, event }: ExectuteHandlerOptions) : Pro
|
|
|
175
194
|
}
|
|
176
195
|
|
|
177
196
|
if (isHtml(result)) {
|
|
178
|
-
if (
|
|
197
|
+
if (url.searchParams.has("hz")) {
|
|
179
198
|
result = html`<template>${result}</template>`
|
|
180
199
|
}
|
|
181
200
|
|
|
@@ -368,4 +387,3 @@ type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
|
|
|
368
387
|
export type Route = RequireAtLeastOne<Route_, "file" | "get" | "post">
|
|
369
388
|
export type RoutePage = Pick<Route_, "get" | "post">
|
|
370
389
|
|
|
371
|
-
|