This commit is contained in:
Rizky 2024-03-15 08:29:03 +07:00
parent 2b9c360770
commit dd67ef0c86
6 changed files with 61 additions and 96 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,3 +0,0 @@
import { createStore } from "idb-keyval";
export const prodCache = createStore(`prasi-prod`, `prasi-cache-prod`);

View File

@ -3,9 +3,8 @@ import { IContent } from "../../../utils/types/general";
import { IItem } from "../../../utils/types/item";
import { ISection } from "../../../utils/types/section";
import { base } from "./base";
import { prodCache } from "./cache";
export const scanComponent = async (items: IContent[], use_cache?: boolean) => {
export const scanComponent = async (items: IContent[]) => {
const comp = base.comp;
for (const item of items) {
@ -17,25 +16,14 @@ export const scanComponent = async (items: IContent[], use_cache?: boolean) => {
if (comp.pending.size > 0) {
let all_found = true;
const founds: any = [];
if (use_cache !== false) {
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, use_cache);
await scanComponent(founds);
return;
}
}
}
if (comp.pending.size > 0) {
try {
@ -49,9 +37,9 @@ export const scanComponent = async (items: IContent[], use_cache?: boolean) => {
comp.pending.delete(id);
comp.list[id] = item;
await set(`comp-${id}`, item, prodCache);
await set(`comp-${id}`, item);
}
await scanComponent(Object.values(res), use_cache);
await scanComponent(Object.values(res));
} catch (e) {}
}
};

View File

@ -1,9 +1,8 @@
import { base } from "./base";
import { IRoot } from "../../../utils/types/root";
import { prodCache } from "./cache";
import { get, set } from "idb-keyval";
export const loadPage = (page_id: string, use_cache?: boolean) => {
export const loadPage = (page_id: string) => {
return new Promise<{
id: string;
url: string;
@ -11,13 +10,11 @@ export const loadPage = (page_id: string, use_cache?: boolean) => {
}>(async (done) => {
let returned = false;
if (use_cache !== false) {
const cached = await get(`page-${page_id}`, prodCache);
const cached = await get(`page-${page_id}`);
if (cached) {
done(cached);
returned = true;
}
}
const res = (await (
await fetch(base.url`_prasi/page/${page_id}`)
@ -26,11 +23,6 @@ export const loadPage = (page_id: string, use_cache?: boolean) => {
url: string;
root: IRoot;
};
set(
`page-${page_id}`,
{ id: page_id, url: res.url, root: res.root },
prodCache
);
if (!returned) done(res);
});
@ -48,7 +40,7 @@ export const loadPages = (page_ids: string[]) => {
const ids = [...new Set(page_ids)];
let is_done = true;
for (const id of ids) {
const page = await get(`page-${id}`, prodCache);
const page = await get(`page-${id}`);
if (page) {
result[id] = page;
} else {
@ -71,10 +63,6 @@ export const loadPages = (page_ids: string[]) => {
root: IRoot;
}[];
for (const [k, v] of Object.entries(res)) {
set(`page-${k}`, v, prodCache);
}
if (!is_done) {
done(res);
}

View File

@ -1,4 +1,3 @@
import { get, set } from "idb-keyval";
import { createRouter } from "radix3";
import { apiProxy } from "../../../base/load/api/api-proxy";
import { dbProxy } from "../../../base/load/db/db-proxy";
@ -6,7 +5,6 @@ import { IRoot } from "../../../utils/types/root";
import { genMeta } from "../../vi/meta/meta";
import { IMeta } from "../../vi/utils/types";
import { base } from "./base";
import { prodCache } from "./cache";
import { scanComponent } from "./component";
const getRoute = () => {
@ -19,21 +17,15 @@ const getRoute = () => {
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 res = await (await fetch(base.url`_prasi/route`)).json();
await set("route", res, prodCache);
if (!is_done) {
done(res);
}
});
};
export const initBaseRoute = async (isPreviewProd: boolean) => {
export const initBaseRoute = async () => {
const router = createRouter<{ id: string; url: string }>();
const pages = [] as { id: string; url: string }[];
try {
@ -45,7 +37,7 @@ export const initBaseRoute = async (isPreviewProd: boolean) => {
base.layout.root = res.layout.root;
base.layout.meta = {};
if (base.layout.root) {
await scanComponent(base.layout.root.childs, !isPreviewProd);
await scanComponent(base.layout.root.childs);
rebuildMeta(base.layout.meta, base.layout.root);
}
}

View File

@ -39,7 +39,7 @@ export const Root = () => {
if (base.route.status !== "ready") {
if (base.route.status === "init") {
base.route.status = "loading";
initBaseRoute(isPreviewProd).then(async ({ router, pages }) => {
initBaseRoute().then(async ({ router, pages }) => {
detectResponsiveMode();
base.route.status = "ready";
base.route.router = router;
@ -97,7 +97,7 @@ export const Root = () => {
const cache = base.page.cache[page.id];
if (!cache) {
loadPage(page.id, !isPreviewProd)
loadPage(page.id)
.then(async ({ root }) => {
if (page) {
const p = {
@ -106,7 +106,7 @@ export const Root = () => {
root,
meta: {},
};
await scanComponent(root.childs, !isPreviewProd);
await scanComponent(root.childs);
rebuildMeta(p.meta, root);
base.page.cache[p.id] = p;
render();
@ -199,7 +199,7 @@ export const Root = () => {
root: page.root,
meta: {},
};
await scanComponent(page.root.childs, !isPreviewProd);
await scanComponent(page.root.childs);
rebuildMeta(p.meta, page.root);
base.page.cache[p.id] = p;
}