fix page_id from server.ts
This commit is contained in:
parent
cdef4085f8
commit
c49a88b79a
|
|
@ -8,12 +8,12 @@ import { gzipAsync } from "../ws/sync/entity/zlib";
|
|||
export const _ = {
|
||||
url: "/prod/:site_id/**",
|
||||
async api() {
|
||||
const { req, res } = apiContext(this);
|
||||
const { req, prasi } = apiContext(this);
|
||||
|
||||
const pathname: string = req.params["*"] || "";
|
||||
const site_id = req.params.site_id as string;
|
||||
|
||||
const index_html = new Response(prodIndex(site_id).render(), {
|
||||
const index_html = new Response(prodIndex(site_id, prasi).render(), {
|
||||
headers: { "content-type": "text/html" },
|
||||
});
|
||||
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -35,7 +35,7 @@ glb.server_hook = async (arg) => {
|
|||
if (arr.length >= 3 && validate(site_id)) {
|
||||
try {
|
||||
if (!g.server_runtime[site_id]) {
|
||||
await g.createServerRuntime(site_id)
|
||||
await g.createServerRuntime(site_id);
|
||||
}
|
||||
|
||||
const res = await server.http(site_id, arg);
|
||||
|
|
@ -45,7 +45,7 @@ glb.server_hook = async (arg) => {
|
|||
return new Response("403: Please see server.log", { status: 403 });
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
export const prodIndex = (site_id: string) => {
|
||||
export const prodIndex = (
|
||||
site_id: string,
|
||||
prasi: { page_id?: string; params?: any }
|
||||
) => {
|
||||
return {
|
||||
head: [] as string[],
|
||||
body: [] as string[],
|
||||
|
|
@ -19,7 +22,16 @@ export const prodIndex = (site_id: string) => {
|
|||
${this.body.join("\n")}
|
||||
<div id="root"></div>
|
||||
<script>
|
||||
window._prasi = { basepath: "/prod/${site_id}", site_id: "${site_id}" }
|
||||
window._prasi = {
|
||||
basepath: "/prod/${site_id}",
|
||||
site_id: "${site_id}",${
|
||||
prasi.page_id ? `\n page_id: "${prasi.page_id}",` : ""
|
||||
}${
|
||||
typeof prasi.params === "object"
|
||||
? `\n params: ${JSON.stringify(prasi.params)},`
|
||||
: ""
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script src="/prod/${site_id}/main.js" type="module"></script>
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { Subprocess, spawn } from "bun";
|
||||
import { waitUntil } from "web-utils";
|
||||
import { SAction } from "../actions";
|
||||
import { code } from "../editor/code/util-code";
|
||||
import { code, codeGlobalTypings } from "../editor/code/util-code";
|
||||
import { docs } from "../entity/docs";
|
||||
import { snapshot } from "../entity/snapshot";
|
||||
import { SyncConnection } from "../type";
|
||||
|
|
@ -105,31 +105,12 @@ export const code_action: SAction["code"]["action"] = async function (
|
|||
);
|
||||
await Bun.write(
|
||||
Bun.file(path.join(dir, "global.d.ts")),
|
||||
`//@ts-ignore
|
||||
import type * as SRVAPI from "gen/srv/api/srv";
|
||||
import { Server, WebSocketHandler } from "bun";
|
||||
import prisma from "./prisma";
|
||||
|
||||
declare global {
|
||||
codeGlobalTypings.replace(
|
||||
`declare global {`,
|
||||
`declare global {
|
||||
const db: prisma.PrismaClient & ${prismaExtendType};
|
||||
|
||||
type Api = typeof SRVAPI;
|
||||
type ApiName = keyof Api;
|
||||
const api: { [k in ApiName]: Awaited<Api[k]["handler"]>["_"]["api"] } & { _raw: any };
|
||||
|
||||
type PrasiServer = {
|
||||
ws?: WebSocketHandler<{ url: string }>;
|
||||
http: (arg: {
|
||||
url: { raw: URL; pathname: string };
|
||||
req: Request;
|
||||
server: Server;
|
||||
mode: "dev" | "prod";
|
||||
handle: (req: Request) => Promise<Response>;
|
||||
index: { head: string[]; body: string[]; render: () => string };
|
||||
}) => Promise<Response>;
|
||||
};
|
||||
}
|
||||
`
|
||||
)
|
||||
);
|
||||
|
||||
Bun.spawn({
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { codeBuild } from "./build-code";
|
||||
import { CodeMode, code } from "./util-code";
|
||||
import { CodeMode, code, codeGlobalTypings } from "./util-code";
|
||||
|
||||
export const prepCodeSnapshot = async (id_site: string, mode: CodeMode) => {
|
||||
await code
|
||||
|
|
@ -9,6 +9,7 @@ export const prepCodeSnapshot = async (id_site: string, mode: CodeMode) => {
|
|||
"package.json",
|
||||
JSON.stringify({ name: `${mode}-${id_site}`, dependencies: {} }, null, 2)
|
||||
)
|
||||
.new_file("typings/global.d.ts", codeGlobalTypings)
|
||||
.await();
|
||||
|
||||
await codeBuild(id_site);
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ const serverMain = () => ({
|
|||
delete require.cache[server_src_path];
|
||||
const svr = require(server_src_path);
|
||||
|
||||
if (svr && typeof svr.server === 'object') {
|
||||
if (svr && typeof svr.server === "object") {
|
||||
this.handler[site_id] = svr.server;
|
||||
this.handler[site_id].site_id = site_id;
|
||||
}
|
||||
|
|
@ -101,7 +101,7 @@ const serverMain = () => ({
|
|||
raw: arg.url,
|
||||
},
|
||||
mode: "dev",
|
||||
index: prodIndex(site_id),
|
||||
index: prodIndex(site_id, arg.prasi),
|
||||
});
|
||||
} catch (e: any) {
|
||||
_fs.appendFile(
|
||||
|
|
@ -123,6 +123,7 @@ type PrasiServer = {
|
|||
handle: (req: Request) => Promise<undefined | Response>;
|
||||
mode: "dev" | "prod";
|
||||
index: { head: string[]; body: string[]; render: () => string };
|
||||
prasi: { page_id?: string; params?: Record<string, any> };
|
||||
}) => Promise<Response>;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -59,3 +59,28 @@ export const code = {
|
|||
};
|
||||
},
|
||||
};
|
||||
|
||||
export const codeGlobalTypings = `//@ts-ignore
|
||||
import type * as SRVAPI from "gen/srv/api/srv";
|
||||
import { Server, WebSocketHandler } from "bun";
|
||||
import prisma from "./prisma";
|
||||
|
||||
declare global {
|
||||
type Api = typeof SRVAPI;
|
||||
type ApiName = keyof Api;
|
||||
const api: { [k in ApiName]: Awaited<Api[k]["handler"]>["_"]["api"] } & { _raw: any };
|
||||
|
||||
type PrasiServer = {
|
||||
ws?: WebSocketHandler<{ url: string }>;
|
||||
http: (arg: {
|
||||
url: { raw: URL; pathname: string };
|
||||
req: Request;
|
||||
server: Server;
|
||||
mode: "dev" | "prod";
|
||||
handle: (req: Request) => Promise<Response>;
|
||||
index: { head: string[]; body: string[]; render: () => string };
|
||||
prasi: { page_id?: string; params?: Record<string, any> };
|
||||
}) => Promise<Response>;
|
||||
};
|
||||
}
|
||||
`;
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import { loadPage, loadUrls } from "./base/page";
|
|||
import { detectResponsiveMode } from "./base/responsive";
|
||||
import { initBaseRoute, rebuildMeta } from "./base/route";
|
||||
import { w } from "./w";
|
||||
import { MatchedRoute } from "radix3";
|
||||
|
||||
export const isPreview = () => {
|
||||
return (
|
||||
|
|
@ -87,13 +88,42 @@ export const Root = () => {
|
|||
}
|
||||
}
|
||||
|
||||
let page = router.lookup(base.pathname);
|
||||
let page: MatchedRoute<{
|
||||
id: string;
|
||||
url: string;
|
||||
}> | null = null;
|
||||
|
||||
// hydrate page_id from server.ts
|
||||
if (w._prasi.page_id) {
|
||||
router.insert(base.pathname, {
|
||||
id: w._prasi.page_id,
|
||||
url: base.pathname,
|
||||
});
|
||||
|
||||
page = { id: w._prasi.page_id, url: "", params: w._prasi.params };
|
||||
w._prasi.routed = { page_id: w._prasi.page_id, params: w._prasi.params };
|
||||
delete w._prasi.page_id;
|
||||
delete w._prasi.params;
|
||||
}
|
||||
// regular route
|
||||
else {
|
||||
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 &&
|
||||
w._prasi.routed &&
|
||||
w._prasi.routed.page_id === page.id &&
|
||||
w._prasi.routed.params
|
||||
) {
|
||||
page.params = w._prasi.routed.params;
|
||||
}
|
||||
}
|
||||
if (!page) return <DeadEnd>Page Not Found</DeadEnd>;
|
||||
|
||||
if (page.id !== local.page_id) {
|
||||
|
|
|
|||
|
|
@ -5,5 +5,13 @@ export const w = window as unknown as {
|
|||
};
|
||||
params: any;
|
||||
navigateOverride: (href: string) => void;
|
||||
_prasi: { basepath: string };
|
||||
_prasi: {
|
||||
basepath: string;
|
||||
page_id?: string;
|
||||
params?: any;
|
||||
routed?: {
|
||||
page_id?: string;
|
||||
params?: any;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ export const viLoadSnapshot = async (p: PG) => {
|
|||
if (api_url && apiURL.hostname) {
|
||||
await loadApiProxyDef(api_url, true);
|
||||
|
||||
|
||||
const api = w.prasiApi[api_url];
|
||||
if (api && api.apiTypes && api.prismaTypes) {
|
||||
const zip = JSON.stringify({
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ export const apiContext = (ctx: any) => {
|
|||
return {
|
||||
mode: g.mode,
|
||||
req: ctx.req as Request & { params: any; query_parameters: any },
|
||||
prasi: ctx.prasi || {},
|
||||
res: {
|
||||
...ctx.res,
|
||||
send: (body) => {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,8 @@ export const createServer = async () => {
|
|||
maxRequestBodySize: 1024 * 1024 * 128,
|
||||
websocket: await serveWS(wsHandler),
|
||||
async fetch(req, server) {
|
||||
const url = new URL(req.url);
|
||||
const url = new URL(req.url) as URL;
|
||||
const prasi = {};
|
||||
const handle = async (req: Request) => {
|
||||
if (wsHandler[url.pathname]) {
|
||||
if (
|
||||
|
|
@ -45,7 +46,7 @@ export const createServer = async () => {
|
|||
return serveStatic.serve(url);
|
||||
}
|
||||
|
||||
const api_response = await serveAPI.serve(url, req);
|
||||
const api_response = await serveAPI.serve(url, req, prasi);
|
||||
if (api_response) {
|
||||
return api_response;
|
||||
}
|
||||
|
|
@ -54,7 +55,14 @@ export const createServer = async () => {
|
|||
};
|
||||
|
||||
if (g.server_hook) {
|
||||
return await g.server_hook({ url, req, server, handle, wsHandler });
|
||||
return await g.server_hook({
|
||||
url,
|
||||
req,
|
||||
server,
|
||||
handle,
|
||||
wsHandler,
|
||||
prasi,
|
||||
});
|
||||
} else {
|
||||
return await handle(req);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ export const serveAPI = {
|
|||
g.router.insert(route.url.replace(/\*/gi, "**"), route);
|
||||
}
|
||||
},
|
||||
serve: async (url: URL, req: Request) => {
|
||||
serve: async (url: URL, req: Request, prasi: { page_id?: string }) => {
|
||||
let found = g.router.lookup(url.pathname);
|
||||
let found_not_match = false;
|
||||
if (!found?.url) {
|
||||
|
|
@ -89,6 +89,7 @@ export const serveAPI = {
|
|||
res: new Response(),
|
||||
...found,
|
||||
params,
|
||||
prasi,
|
||||
};
|
||||
|
||||
const finalResponse = await current.fn(...args);
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ export const g = global as unknown as {
|
|||
server: Server;
|
||||
handle: (req: Request) => Promise<Response | undefined>;
|
||||
wsHandler: Record<string, WebSocketHandler<WSData>>;
|
||||
prasi: { page_id?: string };
|
||||
}) => Promise<Response | undefined>;
|
||||
server_runtime: Record<
|
||||
string,
|
||||
|
|
|
|||
Loading…
Reference in New Issue