From d28fa4c14153764ca1838c6e6306114ab099a876 Mon Sep 17 00:00:00 2001 From: Rizky Date: Fri, 9 Feb 2024 18:33:52 +0700 Subject: [PATCH] wip fix prod --- app/srv/api/prod.ts | 10 ++- app/srv/ws/sync/actions/code_action.ts | 16 +++- app/srv/ws/sync/editor/code/build-code.ts | 13 +--- app/srv/ws/sync/editor/code/server-main.ts | 1 - app/web/src/nova/prod/base/cache.ts | 3 + app/web/src/nova/prod/base/page.tsx | 87 ++++++++++++++++++---- app/web/src/nova/prod/root.tsx | 2 +- 7 files changed, 99 insertions(+), 33 deletions(-) create mode 100644 app/web/src/nova/prod/base/cache.ts diff --git a/app/srv/api/prod.ts b/app/srv/api/prod.ts index 79a6d10d..1e58d607 100644 --- a/app/srv/api/prod.ts +++ b/app/srv/api/prod.ts @@ -109,10 +109,16 @@ window._prasi={basepath: "/prod/${site_id}",site_id:"${site_id}"} if (validate(page_id)) { const page = await _db.page.findFirst({ where: { id: page_id }, - select: { content_tree: true }, + select: { content_tree: true, url: true }, }); if (page) { - return gzipAsync(JSON.stringify(page.content_tree) as any); + return gzipAsync( + JSON.stringify({ + id: page_id, + root: page.content_tree, + url: page.url, + }) as any + ); } } return null; diff --git a/app/srv/ws/sync/actions/code_action.ts b/app/srv/ws/sync/actions/code_action.ts index 2fbe825b..2a682fd0 100644 --- a/app/srv/ws/sync/actions/code_action.ts +++ b/app/srv/ws/sync/actions/code_action.ts @@ -105,17 +105,29 @@ export const code_action: SAction["code"]["action"] = async function ( await Bun.write( Bun.file(path.join(dir, "global.d.ts")), `\ -import prisma from "./prisma"; import type * as SRVAPI from "gen/srv/api/srv"; +import { Server, WebSocketHandler } from "bun"; +import prisma from "./prisma"; + declare global { const db: prisma.PrismaClient; type Api = typeof SRVAPI; type ApiName = keyof Api; const api: { [k in ApiName]: Awaited["_"]["api"] }; + + type PrasiServer = { + ws?: WebSocketHandler<{ url: string }>; + http: (arg: { + url: URL; + req: Request; + server: Server; + handle: (req: Request) => Promise; + }) => Promise; + }; } - ` +` ); Bun.spawn({ diff --git a/app/srv/ws/sync/editor/code/build-code.ts b/app/srv/ws/sync/editor/code/build-code.ts index d17c1e9b..f14379b5 100644 --- a/app/srv/ws/sync/editor/code/build-code.ts +++ b/app/srv/ws/sync/editor/code/build-code.ts @@ -21,17 +21,9 @@ export const codeBuild = async (id_site: any) => { await writeAsync( server_main, `\ -import { Server, WebSocketHandler } from "bun"; +import type {} from "./typings/global"; -export const server: { - ws?: WebSocketHandler<{ url: string }>; - http: (arg: { - url: URL; - req: Request; - server: Server; - handle: (req: Request) => Promise; - }) => Promise; -} = { +export const server: PrasiServer = { async http({ req, handle, url, server }) { return await handle(req); }, @@ -93,7 +85,6 @@ if (typeof global.server_hook === "function") { name: "prasi", setup(setup) { setup.onEnd((res) => { - console.log("en bul"); server.init(id_site); }); }, diff --git a/app/srv/ws/sync/editor/code/server-main.ts b/app/srv/ws/sync/editor/code/server-main.ts index 8b3d9fda..e5d2a2a3 100644 --- a/app/srv/ws/sync/editor/code/server-main.ts +++ b/app/srv/ws/sync/editor/code/server-main.ts @@ -27,7 +27,6 @@ const serverMain = () => ({ init(site_id: string) { clearTimeout(this.init_timeout); this.init_timeout = setTimeout(() => { - console.log("initing", site_id); try { const server_src_path = code.path( site_id, diff --git a/app/web/src/nova/prod/base/cache.ts b/app/web/src/nova/prod/base/cache.ts new file mode 100644 index 00000000..3a974744 --- /dev/null +++ b/app/web/src/nova/prod/base/cache.ts @@ -0,0 +1,3 @@ +import { createStore } from "idb-keyval"; + +export const prodCache = createStore(`prasi-prod`, `prasi-cache-prod`); diff --git a/app/web/src/nova/prod/base/page.tsx b/app/web/src/nova/prod/base/page.tsx index 8a477d0f..58851921 100644 --- a/app/web/src/nova/prod/base/page.tsx +++ b/app/web/src/nova/prod/base/page.tsx @@ -1,26 +1,81 @@ import { base } from "./base"; import { IRoot } from "../../../utils/types/root"; import { decompressBlob } from "./util"; +import { prodCache } from "./cache"; +import { get, set } from "idb-keyval"; -export const loadPage = async (page_id: string) => { - const raw = await (await fetch(base.url`_prasi/page/${page_id}`)).blob(); - const res = JSON.parse(await (await decompressBlob(raw)).text()) as IRoot; - return res; -}; - -export const loadPages = async (page_ids: string[]) => { - const raw = await ( - await fetch(base.url`_prasi/pages`, { - method: "POST", - body: JSON.stringify({ ids: [...new Set(page_ids)] }), - }) - ).blob(); - const res = JSON.parse(await (await decompressBlob(raw)).text()) as { +export const loadPage = (page_id: string) => { + return new Promise<{ id: string; url: string; root: IRoot; - }[]; - return res; + }>(async (done) => { + let returned = false; + const cached = await get(`page-${page_id}`, prodCache); + if (cached) { + done(cached); + returned = true; + } + + const raw = await (await fetch(base.url`_prasi/page/${page_id}`)).blob(); + const res = JSON.parse(await (await decompressBlob(raw)).text()) as { + id: string; + url: string; + root: IRoot; + }; + set( + `page-${page_id}`, + { id: page_id, url: res.url, root: res.root }, + prodCache + ); + + if (!returned) done(res); + }); +}; + +export const loadPages = (page_ids: string[]) => { + return new Promise< + { + id: string; + url: string; + root: IRoot; + }[] + >(async (done) => { + const result: any = {}; + const ids = [...new Set(page_ids)]; + let is_complete = true; + for (const id of ids) { + const page = await get(`page-${id}`, prodCache); + if (page) { + result[id] = page; + } else { + is_complete = false; + } + } + if (is_complete) { + done(result); + } + + const raw = await ( + await fetch(base.url`_prasi/pages`, { + method: "POST", + body: JSON.stringify({ ids }), + }) + ).blob(); + const res = JSON.parse(await (await decompressBlob(raw)).text()) as { + id: string; + url: string; + root: IRoot; + }[]; + + for (const [k, v] of Object.entries(res)) { + set(`page-${k}`, v, prodCache); + } + + if (!is_complete) { + done(res); + } + }); }; export const loadUrls = async (urls: string[]) => { diff --git a/app/web/src/nova/prod/root.tsx b/app/web/src/nova/prod/root.tsx index 1d760db8..e9d702f2 100644 --- a/app/web/src/nova/prod/root.tsx +++ b/app/web/src/nova/prod/root.tsx @@ -61,7 +61,7 @@ export const Root = () => { if (!cache) { loadPage(page.id) - .then(async (root) => { + .then(async ({ root }) => { const p = { id: page.id, url: page.url,