fix deploy index

This commit is contained in:
Rizky 2024-04-26 06:20:43 +07:00
parent b3b3bc9288
commit 42343a3af9
4 changed files with 26 additions and 27 deletions

View File

@ -7,6 +7,7 @@ import { g } from "../utils/global";
import { parseArgs } from "./parse-args";
import { serveAPI } from "./serve-api";
import { serveWeb } from "./serve-web";
import { prodIndex } from "utils/prod-index";
export const createServer = async () => {
g.router = createRouter({ strictTrailingSlash: true });
@ -65,19 +66,13 @@ export const createServer = async () => {
};
};
if (!g.deploy.server && (await existsAsync(dir(`app/web/server/index.js`)))) {
const res = require(dir(`app/web/server/index.js`));
if (res && typeof res.server === "object") {
g.deploy.server = res.server;
}
}
g.server = Bun.serve({
port: g.port,
maxRequestBodySize: 1024 * 1024 * 128,
async fetch(req) {
const url = new URL(req.url) as URL;
const prasi = {};
const index = prodIndex(g.deploy.config.site_id, prasi);
const handle = async (req: Request) => {
const api = await serveAPI(url, req);
@ -86,15 +81,13 @@ export const createServer = async () => {
return api;
}
if (g.deploy.index) {
if (g.deploy.router) {
const found = g.deploy.router.lookup(url.pathname);
if (found) {
return await serveWeb({
content: g.deploy.index.render(),
pathname: "index.html",
});
}
if (g.deploy.router) {
const found = g.deploy.router.lookup(url.pathname);
if (found) {
return await serveWeb({
content: index.render(),
pathname: "index.html",
});
}
if (g.deploy.gz) {
@ -110,7 +103,7 @@ export const createServer = async () => {
pathname === "index.htm"
) {
return await serveWeb({
content: g.deploy.index.render(),
content: index.render(),
pathname: "index.html",
});
}
@ -136,7 +129,7 @@ export const createServer = async () => {
!url.pathname.startsWith("/_deploy") &&
!url.pathname.startsWith("/_prasi")
) {
if (g.deploy.server && g.deploy.index) {
if (g.deploy.server && index) {
try {
return await g.deploy.server.http({
handle,
@ -144,7 +137,7 @@ export const createServer = async () => {
req,
server: g.server,
url: { pathname: url.pathname, raw: url },
index: g.deploy.index,
index: index,
prasi,
});
} catch (e) {
@ -163,5 +156,3 @@ export const createServer = async () => {
g.log.info(`Started at port: ${g.server.port}`);
}
};
await g.deploy?.server?.init?.({ port: g.server.port });

View File

@ -36,8 +36,6 @@ export const deploy = {
)
);
g.deploy.index = prodIndex(this.config.site_id);
if (g.deploy.gz) {
for (const page of g.deploy.gz.layouts) {
if (page.is_default_layout) {
@ -133,7 +131,6 @@ export const deploy = {
raw: null,
gz: null,
server: null,
index: null,
};
}

View File

@ -101,6 +101,5 @@ export const g = global as unknown as {
deploy: { ts: string };
};
server: PrasiServer | null;
index: ReturnType<typeof prodIndex> | null;
};
};

View File

@ -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: "/", 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="/main.js" type="module"></script>
</body>