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 { parseArgs } from "./parse-args";
import { serveAPI } from "./serve-api"; import { serveAPI } from "./serve-api";
import { serveWeb } from "./serve-web"; import { serveWeb } from "./serve-web";
import { prodIndex } from "utils/prod-index";
export const createServer = async () => { export const createServer = async () => {
g.router = createRouter({ strictTrailingSlash: true }); 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({ g.server = Bun.serve({
port: g.port, port: g.port,
maxRequestBodySize: 1024 * 1024 * 128, maxRequestBodySize: 1024 * 1024 * 128,
async fetch(req) { async fetch(req) {
const url = new URL(req.url) as URL; const url = new URL(req.url) as URL;
const prasi = {}; const prasi = {};
const index = prodIndex(g.deploy.config.site_id, prasi);
const handle = async (req: Request) => { const handle = async (req: Request) => {
const api = await serveAPI(url, req); const api = await serveAPI(url, req);
@ -86,15 +81,13 @@ export const createServer = async () => {
return api; return api;
} }
if (g.deploy.index) { if (g.deploy.router) {
if (g.deploy.router) { const found = g.deploy.router.lookup(url.pathname);
const found = g.deploy.router.lookup(url.pathname); if (found) {
if (found) { return await serveWeb({
return await serveWeb({ content: index.render(),
content: g.deploy.index.render(), pathname: "index.html",
pathname: "index.html", });
});
}
} }
if (g.deploy.gz) { if (g.deploy.gz) {
@ -110,7 +103,7 @@ export const createServer = async () => {
pathname === "index.htm" pathname === "index.htm"
) { ) {
return await serveWeb({ return await serveWeb({
content: g.deploy.index.render(), content: index.render(),
pathname: "index.html", pathname: "index.html",
}); });
} }
@ -136,7 +129,7 @@ export const createServer = async () => {
!url.pathname.startsWith("/_deploy") && !url.pathname.startsWith("/_deploy") &&
!url.pathname.startsWith("/_prasi") !url.pathname.startsWith("/_prasi")
) { ) {
if (g.deploy.server && g.deploy.index) { if (g.deploy.server && index) {
try { try {
return await g.deploy.server.http({ return await g.deploy.server.http({
handle, handle,
@ -144,7 +137,7 @@ export const createServer = async () => {
req, req,
server: g.server, server: g.server,
url: { pathname: url.pathname, raw: url }, url: { pathname: url.pathname, raw: url },
index: g.deploy.index, index: index,
prasi, prasi,
}); });
} catch (e) { } catch (e) {
@ -163,5 +156,3 @@ export const createServer = async () => {
g.log.info(`Started at port: ${g.server.port}`); 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) { if (g.deploy.gz) {
for (const page of g.deploy.gz.layouts) { for (const page of g.deploy.gz.layouts) {
if (page.is_default_layout) { if (page.is_default_layout) {
@ -133,7 +131,6 @@ export const deploy = {
raw: null, raw: null,
gz: null, gz: null,
server: null, server: null,
index: null,
}; };
} }

View File

@ -101,6 +101,5 @@ export const g = global as unknown as {
deploy: { ts: string }; deploy: { ts: string };
}; };
server: PrasiServer | null; 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 { return {
head: [] as string[], head: [] as string[],
body: [] as string[], body: [] as string[],
@ -19,7 +22,16 @@ export const prodIndex = (site_id: string) => {
${this.body.join("\n")} ${this.body.join("\n")}
<div id="root"></div> <div id="root"></div>
<script> <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>
<script src="/main.js" type="module"></script> <script src="/main.js" type="module"></script>
</body> </body>