wip fix prasi
This commit is contained in:
parent
fcf953b478
commit
a55c2e48e2
File diff suppressed because one or more lines are too long
|
|
@ -83,50 +83,65 @@ export const edInitSync = (p: PG) => {
|
||||||
|
|
||||||
if (!params.page_id) {
|
if (!params.page_id) {
|
||||||
if (location.pathname.startsWith("/ed")) {
|
if (location.pathname.startsWith("/ed")) {
|
||||||
if (!params.site_id) {
|
(async () => {
|
||||||
_db.page
|
const e = await _db.page.findFirst({
|
||||||
.findFirst({
|
where: {
|
||||||
|
is_deleted: false,
|
||||||
|
is_default_layout: false,
|
||||||
|
site: params.site_id
|
||||||
|
? { id: params.site_id }
|
||||||
|
: {
|
||||||
|
id_user: p.user.id,
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
contains: "root",
|
||||||
|
mode: "insensitive",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
select: { id: true, id_site: true },
|
||||||
|
orderBy: {
|
||||||
|
site: {
|
||||||
|
name: "asc",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (e) location.href = `/ed/${e.id_site}/${e.id}`;
|
||||||
|
else {
|
||||||
|
const e = await _db.page.findFirst({
|
||||||
where: {
|
where: {
|
||||||
is_deleted: false,
|
is_deleted: false,
|
||||||
is_default_layout: false,
|
is_default_layout: false,
|
||||||
site: {
|
site: params.site_id
|
||||||
id_user: p.user.id,
|
? { id: params.site_id }
|
||||||
|
: {
|
||||||
|
id_user: p.user.id,
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
contains: "home",
|
||||||
|
mode: "insensitive",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
select: { id: true, id_site: true },
|
select: { id: true, id_site: true },
|
||||||
})
|
});
|
||||||
.then((e) => {
|
|
||||||
|
if (e) location.href = `/ed/${e.id_site}/${e.id}`;
|
||||||
|
else {
|
||||||
|
const e = await _db.page.findFirst({
|
||||||
|
where: {
|
||||||
|
is_deleted: false,
|
||||||
|
is_default_layout: false,
|
||||||
|
site: params.site_id
|
||||||
|
? { id: params.site_id }
|
||||||
|
: {
|
||||||
|
id_user: p.user.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
select: { id: true, id_site: true },
|
||||||
|
});
|
||||||
if (e) location.href = `/ed/${e.id_site}/${e.id}`;
|
if (e) location.href = `/ed/${e.id_site}/${e.id}`;
|
||||||
});
|
}
|
||||||
} else {
|
}
|
||||||
_db.page
|
})();
|
||||||
.findFirst({
|
|
||||||
where: {
|
|
||||||
is_deleted: false,
|
|
||||||
is_default_layout: false,
|
|
||||||
id_site: params.site_id,
|
|
||||||
},
|
|
||||||
select: { id: true, id_site: true },
|
|
||||||
})
|
|
||||||
.then(async (e) => {
|
|
||||||
if (e) location.href = `/ed/${params.site_id}/${e.id}`;
|
|
||||||
else {
|
|
||||||
const res = await _db.page.create({
|
|
||||||
data: {
|
|
||||||
content_tree: {
|
|
||||||
childs: [],
|
|
||||||
id: "root",
|
|
||||||
type: "root",
|
|
||||||
},
|
|
||||||
name: "home",
|
|
||||||
url: "/",
|
|
||||||
site: { connect: { id: params.site_id } },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (res) location.href = `/ed/${params.site_id}/${res.id}`;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ export const base = {
|
||||||
mode: "" as "desktop" | "mobile",
|
mode: "" as "desktop" | "mobile",
|
||||||
route: {
|
route: {
|
||||||
status: "init" as "init" | "loading" | "ready",
|
status: "init" as "init" | "loading" | "ready",
|
||||||
|
pages: [] as { id: string; url: string }[],
|
||||||
router: null as null | RadixRouter<{ id: string; url: string }>,
|
router: null as null | RadixRouter<{ id: string; url: string }>,
|
||||||
},
|
},
|
||||||
comp: {
|
comp: {
|
||||||
|
|
|
||||||
|
|
@ -3,17 +3,20 @@ import { IRoot } from "../../../utils/types/root";
|
||||||
import { prodCache } from "./cache";
|
import { prodCache } from "./cache";
|
||||||
import { get, set } from "idb-keyval";
|
import { get, set } from "idb-keyval";
|
||||||
|
|
||||||
export const loadPage = (page_id: string) => {
|
export const loadPage = (page_id: string, use_cache?: boolean) => {
|
||||||
return new Promise<{
|
return new Promise<{
|
||||||
id: string;
|
id: string;
|
||||||
url: string;
|
url: string;
|
||||||
root: IRoot;
|
root: IRoot;
|
||||||
}>(async (done) => {
|
}>(async (done) => {
|
||||||
let returned = false;
|
let returned = false;
|
||||||
const cached = await get(`page-${page_id}`, prodCache);
|
|
||||||
if (cached) {
|
if (use_cache !== false) {
|
||||||
done(cached);
|
const cached = await get(`page-${page_id}`, prodCache);
|
||||||
returned = true;
|
if (cached) {
|
||||||
|
done(cached);
|
||||||
|
returned = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = (await (
|
const res = (await (
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ const getRoute = () => {
|
||||||
|
|
||||||
export const initBaseRoute = async () => {
|
export const initBaseRoute = async () => {
|
||||||
const router = createRouter<{ id: string; url: string }>();
|
const router = createRouter<{ id: string; url: string }>();
|
||||||
|
const pages = [] as { id: string; url: string }[];
|
||||||
try {
|
try {
|
||||||
const res = await getRoute();
|
const res = await getRoute();
|
||||||
|
|
||||||
|
|
@ -61,11 +62,12 @@ export const initBaseRoute = async () => {
|
||||||
|
|
||||||
for (const item of res.urls) {
|
for (const item of res.urls) {
|
||||||
router.insert(item.url, item);
|
router.insert(item.url, item);
|
||||||
|
pages.push(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
return router;
|
return { router, pages };
|
||||||
};
|
};
|
||||||
|
|
||||||
const injectSiteScript = () => {
|
const injectSiteScript = () => {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { createRoot } from "react-dom/client";
|
import { createRoot } from "react-dom/client";
|
||||||
import { defineReact, defineWindow } from "web-utils";
|
import { defineReact, defineWindow } from "web-utils";
|
||||||
import { Root } from "./root";
|
import { Root, isPreview } from "./root";
|
||||||
import { initBaseConfig } from "./base/base";
|
import { initBaseConfig } from "./base/base";
|
||||||
import { w } from "./w";
|
import { w } from "./w";
|
||||||
|
|
||||||
|
|
@ -14,15 +14,7 @@ import { w } from "./w";
|
||||||
|
|
||||||
w.navigateOverride = (_href: string) => {
|
w.navigateOverride = (_href: string) => {
|
||||||
if (_href && _href.startsWith("/")) {
|
if (_href && _href.startsWith("/")) {
|
||||||
if (
|
if (isPreview()) {
|
||||||
location.hostname.split(".").length === 4 ||
|
|
||||||
location.hostname === "prasi.app" ||
|
|
||||||
location.hostname === "prasi.avolut.com" ||
|
|
||||||
location.hostname.includes("ngrok") ||
|
|
||||||
location.hostname === "localhost" ||
|
|
||||||
location.hostname === "127.0.0.1" ||
|
|
||||||
location.hostname === "10.0.2.2" // android localhost
|
|
||||||
) {
|
|
||||||
if (
|
if (
|
||||||
location.pathname.startsWith("/prod") &&
|
location.pathname.startsWith("/prod") &&
|
||||||
!_href.startsWith("/prod")
|
!_href.startsWith("/prod")
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { FC, useState } from "react";
|
import { FC, useState } from "react";
|
||||||
|
import { validate } from "uuid";
|
||||||
import { GlobalContext, useLocal } from "web-utils";
|
import { GlobalContext, useLocal } from "web-utils";
|
||||||
import { DeadEnd } from "../../utils/ui/deadend";
|
import { DeadEnd } from "../../utils/ui/deadend";
|
||||||
import { Loading } from "../../utils/ui/loading";
|
import { Loading } from "../../utils/ui/loading";
|
||||||
|
|
@ -11,6 +12,18 @@ import { detectResponsiveMode } from "./base/responsive";
|
||||||
import { initBaseRoute, rebuildMeta } from "./base/route";
|
import { initBaseRoute, rebuildMeta } from "./base/route";
|
||||||
import { w } from "./w";
|
import { w } from "./w";
|
||||||
|
|
||||||
|
export const isPreview = () => {
|
||||||
|
return (
|
||||||
|
location.hostname.split(".").length === 4 ||
|
||||||
|
location.hostname === "prasi.app" ||
|
||||||
|
location.hostname === "prasi.avolut.com" ||
|
||||||
|
location.hostname.includes("ngrok") ||
|
||||||
|
location.hostname === "localhost" ||
|
||||||
|
location.hostname === "127.0.0.1" ||
|
||||||
|
location.hostname === "10.0.2.2"
|
||||||
|
); // android localhost
|
||||||
|
};
|
||||||
|
|
||||||
export const Root = () => {
|
export const Root = () => {
|
||||||
// #region context
|
// #region context
|
||||||
const local = useLocal({ page_id: "" });
|
const local = useLocal({ page_id: "" });
|
||||||
|
|
@ -24,10 +37,11 @@ export const Root = () => {
|
||||||
if (base.route.status !== "ready") {
|
if (base.route.status !== "ready") {
|
||||||
if (base.route.status === "init") {
|
if (base.route.status === "init") {
|
||||||
base.route.status = "loading";
|
base.route.status = "loading";
|
||||||
initBaseRoute().then(async (router) => {
|
initBaseRoute().then(async ({ router, pages }) => {
|
||||||
detectResponsiveMode();
|
detectResponsiveMode();
|
||||||
base.route.status = "ready";
|
base.route.status = "ready";
|
||||||
base.route.router = router;
|
base.route.router = router;
|
||||||
|
base.route.pages = pages;
|
||||||
|
|
||||||
const site_script = evalCJS(
|
const site_script = evalCJS(
|
||||||
await (
|
await (
|
||||||
|
|
@ -53,7 +67,22 @@ export const Root = () => {
|
||||||
const router = base.route.router;
|
const router = base.route.router;
|
||||||
if (!router) return <DeadEnd>Failed to create Router</DeadEnd>;
|
if (!router) return <DeadEnd>Failed to create Router</DeadEnd>;
|
||||||
|
|
||||||
const page = router.lookup(base.pathname);
|
let page_id_from_url = "";
|
||||||
|
const isPreviewProd = isPreview() && location.pathname.startsWith("/prod");
|
||||||
|
if (isPreviewProd) {
|
||||||
|
const parts = location.pathname.split("/");
|
||||||
|
if (validate(parts[3])) {
|
||||||
|
page_id_from_url = parts[3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let page = router.lookup(base.pathname);
|
||||||
|
if (page_id_from_url) {
|
||||||
|
const found = base.route.pages.find((e) => page_id_from_url === e.id);
|
||||||
|
if (found) {
|
||||||
|
page = found;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!page) return <DeadEnd>Page Not Found</DeadEnd>;
|
if (!page) return <DeadEnd>Page Not Found</DeadEnd>;
|
||||||
|
|
||||||
if (page.id !== local.page_id) {
|
if (page.id !== local.page_id) {
|
||||||
|
|
@ -67,18 +96,20 @@ export const Root = () => {
|
||||||
const cache = base.page.cache[page.id];
|
const cache = base.page.cache[page.id];
|
||||||
|
|
||||||
if (!cache) {
|
if (!cache) {
|
||||||
loadPage(page.id)
|
loadPage(page.id, !isPreviewProd)
|
||||||
.then(async ({ root }) => {
|
.then(async ({ root }) => {
|
||||||
const p = {
|
if (page) {
|
||||||
id: page.id,
|
const p = {
|
||||||
url: page.url,
|
id: page.id,
|
||||||
root,
|
url: page.url,
|
||||||
meta: {},
|
root,
|
||||||
};
|
meta: {},
|
||||||
await scanComponent(root.childs);
|
};
|
||||||
rebuildMeta(p.meta, root);
|
await scanComponent(root.childs);
|
||||||
base.page.cache[p.id] = p;
|
rebuildMeta(p.meta, root);
|
||||||
render();
|
base.page.cache[p.id] = p;
|
||||||
|
render();
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
render();
|
render();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue