fix service worker

This commit is contained in:
Rizky 2023-10-15 13:52:59 +07:00
parent 6418e5b879
commit c3af3cba39
8 changed files with 69 additions and 37 deletions

View File

@ -13,8 +13,10 @@ export const treePropEval = (
cprops: [string, FNCompDef][]
) => {
if (meta.item.type === "item" && meta.item.component) {
if (p.site.api_url) {
if (!p.script.db) p.script.db = createDB(p.site.api_url);
if (!p.script.api) p.script.api = createAPI(p.site.api_url);
}
const props = meta.item.component.props;

View File

@ -27,8 +27,10 @@ export const treeScopeEval = (
}
// prepare args
if (p.site.api_url) {
if (!p.script.db) p.script.db = createDB(p.site.api_url);
if (!p.script.api) p.script.api = createAPI(p.site.api_url);
}
const w = window as any;
const finalScope = mergeScopeUpwards(p, meta);

View File

@ -15,8 +15,10 @@ export const treePropEval = (
cprops: [string, FNCompDef][]
) => {
if (meta.item.type === "item" && meta.item.component) {
if (p.site.api_url) {
if (!p.script.db) p.script.db = createDB(p.site.api_url);
if (!p.script.api) p.script.api = createAPI(p.site.api_url);
}
const props = meta.item.component.props;

View File

@ -35,8 +35,10 @@ export const treeScopeEval = (
}
// prepare args
if (p.site.api_url) {
if (!p.script.db) p.script.db = createDB(p.site.api_url);
if (!p.script.api) p.script.api = createAPI(p.site.api_url);
}
const w = window as any;
const finalScope = mergeScopeUpwards(p, meta);

View File

@ -1,7 +1,12 @@
import { manifest, version } from "@parcel/service-worker";
const g = {
cache: null as null | Cache,
};
async function install() {
const cache = await caches.open(version);
g.cache = cache;
await cache.addAll(manifest);
}
addEventListener("install", (e) => (e as ExtendableEvent).waitUntil(install()));
@ -14,27 +19,41 @@ addEventListener("activate", (e) =>
(e as ExtendableEvent).waitUntil(activate())
);
addEventListener("fetch", async (evt) => {
const e = evt as FetchEvent;
e.respondWith(
(async () => {
const r = await caches.match(e.request);
if (r) {
return r;
}
return await fetch(e.request);
})()
);
});
addEventListener("message", async (e) => {
const type = e.data.type;
const cache = await caches.open(version);
// addEventListener("fetch", async (evt) => {
// const e = evt as FetchEvent;
switch (type) {
case "add-cache":
if (!(await cache.match(e.data.url))) {
await cache.add(e.data.url);
}
break;
}
});
// e.respondWith(
// (async () => {
// if (!g.cache) {
// g.cache = await caches.open(version);
// }
// const cache = g.cache;
// const r = await cache.match(e.request);
// if (r) {
// return r;
// }
// const url = e.request.url;
// if (url.includes("_api_frm")) {
// cache.add(e.request);
// }
// return await fetch(e.request);
// })()
// );
// });
// addEventListener("message", async (e) => {
// const type = e.data.type;
// if (!g.cache) {
// g.cache = await caches.open(version);
// }
// const cache = g.cache;
// switch (type) {
// case "add-cache":
// if (!(await cache.match(e.data.url))) {
// await cache.add(e.data.url);
// }
// break;
// }
// });

View File

@ -20,7 +20,7 @@ export const createAPI = (url: string) => {
w.prasiApi = {};
}
if (!url) {
throw new Error("s")
throw new Error("No URL provided");
}
return w.apiClient(w.prasiApi[url]?.apiEntry, url);
};

View File

@ -1,15 +1,13 @@
import { apiContext } from "../server/api-ctx";
import { execQuery } from "../utils/query";
import { DBArg, execQuery } from "../utils/query";
export const _ = {
url: "/_dbs/:dbName/:action",
async api(dbName: any, action?: string) {
const { req, res } = apiContext(this);
const body = req.params;
try {
const result = await execQuery(body, db);
const result = await execQuery(req.params, db);
res.send(result);
} catch (e: any) {
res.sendStatus(500);

View File

@ -37,12 +37,19 @@ export const apiContext = (ctx: any) => {
};
};
const replacer = (key: string, value: string) => {
if (typeof value === "string" && value.startsWith("BigInt::")) {
return BigInt(value.substring(8));
}
return value;
};
export const createResponse = (existingRes: any, body: any) => {
const status =
typeof existingRes._status === "number" ? existingRes._status : undefined;
let res = new Response(
typeof body === "string" ? body : JSON.stringify(body),
typeof body === "string" ? body : JSON.stringify(body, replacer),
status
? {
status,