From 42343a3af9a602dce6e26bd709eff38ca50fe038 Mon Sep 17 00:00:00 2001 From: Rizky Date: Fri, 26 Apr 2024 06:20:43 +0700 Subject: [PATCH] fix deploy index --- pkgs/server/create.ts | 33 ++++++++++++--------------------- pkgs/utils/deploy.ts | 3 --- pkgs/utils/global.ts | 1 - pkgs/utils/prod-index.ts | 16 ++++++++++++++-- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/pkgs/server/create.ts b/pkgs/server/create.ts index 540eb4c..4c90b43 100644 --- a/pkgs/server/create.ts +++ b/pkgs/server/create.ts @@ -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 }); diff --git a/pkgs/utils/deploy.ts b/pkgs/utils/deploy.ts index a0417dc..81e2fe7 100644 --- a/pkgs/utils/deploy.ts +++ b/pkgs/utils/deploy.ts @@ -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, }; } diff --git a/pkgs/utils/global.ts b/pkgs/utils/global.ts index 8452bae..687e57b 100644 --- a/pkgs/utils/global.ts +++ b/pkgs/utils/global.ts @@ -101,6 +101,5 @@ export const g = global as unknown as { deploy: { ts: string }; }; server: PrasiServer | null; - index: ReturnType | null; }; }; diff --git a/pkgs/utils/prod-index.ts b/pkgs/utils/prod-index.ts index eeb5876..b48296d 100644 --- a/pkgs/utils/prod-index.ts +++ b/pkgs/utils/prod-index.ts @@ -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")}