From e40db345ec895b1c1745708788791a5b0bcd1482 Mon Sep 17 00:00:00 2001 From: Rizky Date: Fri, 9 Feb 2024 21:04:11 +0700 Subject: [PATCH] wip fix cache --- app/web/src/nova/prod/base/component.tsx | 27 +++++++++++++++++ app/web/src/nova/prod/base/page.tsx | 9 +++--- app/web/src/nova/prod/base/route.tsx | 38 ++++++++++++++++++------ 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/app/web/src/nova/prod/base/component.tsx b/app/web/src/nova/prod/base/component.tsx index c074da0c..37fc92b1 100644 --- a/app/web/src/nova/prod/base/component.tsx +++ b/app/web/src/nova/prod/base/component.tsx @@ -1,8 +1,10 @@ +import { get, set } from "idb-keyval"; import { IContent } from "../../../utils/types/general"; import { IItem } from "../../../utils/types/item"; import { ISection } from "../../../utils/types/section"; import { base } from "./base"; import { decompressBlob } from "./util"; +import { prodCache } from "./cache"; export const scanComponent = async (items: IContent[]) => { const comp = base.comp; @@ -13,6 +15,29 @@ export const scanComponent = async (items: IContent[]) => { } } + console.log(comp.pending); + + if (comp.pending.size > 0) { + let all_found = true; + const founds: any = []; + for (const id of [...comp.pending]) { + const item = await get(`comp-${id}`, prodCache); + if (!item) { + all_found = false; + } + comp.list[id] = item; + founds.push(item); + } + + if (all_found) { + for (const id of [...comp.pending]) { + comp.pending.delete(id); + } + await scanComponent(founds); + return; + } + } + if (comp.pending.size > 0) { try { const raw = await ( @@ -27,6 +52,8 @@ export const scanComponent = async (items: IContent[]) => { for (const [id, item] of Object.entries(res)) { comp.pending.delete(id); comp.list[id] = item; + + await set(`comp-${id}`, item, prodCache); } await scanComponent(Object.values(res)); } catch (e) {} diff --git a/app/web/src/nova/prod/base/page.tsx b/app/web/src/nova/prod/base/page.tsx index 58851921..55f4e4ee 100644 --- a/app/web/src/nova/prod/base/page.tsx +++ b/app/web/src/nova/prod/base/page.tsx @@ -43,16 +43,17 @@ export const loadPages = (page_ids: string[]) => { >(async (done) => { const result: any = {}; const ids = [...new Set(page_ids)]; - let is_complete = true; + let is_done = true; for (const id of ids) { const page = await get(`page-${id}`, prodCache); if (page) { result[id] = page; } else { - is_complete = false; + is_done = false; + break; } } - if (is_complete) { + if (is_done) { done(result); } @@ -72,7 +73,7 @@ export const loadPages = (page_ids: string[]) => { set(`page-${k}`, v, prodCache); } - if (!is_complete) { + if (!is_done) { done(res); } }); diff --git a/app/web/src/nova/prod/base/route.tsx b/app/web/src/nova/prod/base/route.tsx index 57d9763a..265acc29 100644 --- a/app/web/src/nova/prod/base/route.tsx +++ b/app/web/src/nova/prod/base/route.tsx @@ -6,19 +6,39 @@ import { genMeta } from "../../vi/meta/meta"; import { IMeta } from "../../vi/utils/types"; import { base } from "./base"; import { decompressBlob } from "./util"; +import { prodCache } from "./cache"; +import { get, set } from "idb-keyval"; + +const getRoute = () => { + return new Promise<{ + site: any; + urls: { + id: string; + url: string; + }[]; + layout: any; + }>(async (done) => { + let is_done = false; + const route_cache = await get("route", prodCache); + if (route_cache) { + done(route_cache); + is_done = true; + } + + let raw = await (await fetch(base.url`_prasi/route`)).blob(); + const res = JSON.parse(await (await decompressBlob(raw)).text()); + + await set("route", res, prodCache); + if (!is_done) { + done(res); + } + }); +}; export const initBaseRoute = async () => { - const raw = await (await fetch(base.url`_prasi/route`)).blob(); const router = createRouter<{ id: string; url: string }>(); try { - const res = JSON.parse(await (await decompressBlob(raw)).text()) as { - site: any; - urls: { - id: string; - url: string; - }[]; - layout: any; - }; + const res = await getRoute(); if (res && res.site && res.urls) { if (res.layout) {