From 4db00f34829ea61e9d7c74798c3d3799788d6eba Mon Sep 17 00:00:00 2001 From: monoid Date: Tue, 16 Apr 2024 21:27:25 +0900 Subject: [PATCH] serve assets --- packages/server/src/server.ts | 55 ++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/packages/server/src/server.ts b/packages/server/src/server.ts index f1afd78..96b4a7a 100644 --- a/packages/server/src/server.ts +++ b/packages/server/src/server.ts @@ -18,6 +18,7 @@ import type { DocumentAccessor, TagAccessor, UserAccessor } from "./model/mod"; import { getTagRounter } from "./route/tags"; import { config } from "dotenv"; +import { extname, join } from "node:path"; config(); class ServerApplication { @@ -38,7 +39,7 @@ class ServerApplication { this.diffManger = new DiffManager(this.documentController); this.app = new Koa(); - this.index_html = readFileSync("index.html", "utf-8"); + this.index_html = readFileSync("dist/index.html", "utf-8"); } private async setup() { const setting = get_setting(); @@ -195,26 +196,38 @@ class ServerApplication { } } private serve_static_file(router: Router) { - const static_file_server = (path: string, type: string) => { - router.get(`/${path}`, async (ctx, next) => { - const setting = get_setting(); - ctx.type = type; - ctx.body = createReadStream(path); - ctx.set("x-content-type-options", "no-sniff"); - if (setting.mode === "development") { - ctx.set("cache-control", "no-cache"); - } else { - ctx.set("cache-control", "public, max-age=3600"); - } - }); - }; - const setting = get_setting(); - static_file_server("dist/bundle.css", "css"); - static_file_server("dist/bundle.js", "js"); - if (setting.mode === "development") { - static_file_server("dist/bundle.js.map", "text"); - static_file_server("dist/bundle.css.map", "text"); - } + router.get("/assets/(.*)", async (ctx, next) => { + const setting = get_setting(); + const ext = extname(ctx.path); + ctx.type = ext; + ctx.body = createReadStream(join("dist",`.${ctx.path}`)); + ctx.set("x-content-type-options", "no-sniff"); + if (setting.mode === "development") { + ctx.set("cache-control", "no-cache"); + } else { + ctx.set("cache-control", "public, max-age=3600"); + } + }); + // const static_file_server = (path: string, type: string) => { + // router.get(`/${path}`, async (ctx, next) => { + // const setting = get_setting(); + // ctx.type = type; + // ctx.body = createReadStream(path); + // ctx.set("x-content-type-options", "no-sniff"); + // if (setting.mode === "development") { + // ctx.set("cache-control", "no-cache"); + // } else { + // ctx.set("cache-control", "public, max-age=3600"); + // } + // }); + // }; + // const setting = get_setting(); + // static_file_server("dist/bundle.css", "css"); + // static_file_server("dist/bundle.js", "js"); + // if (setting.mode === "development") { + // static_file_server("dist/bundle.js.map", "text"); + // static_file_server("dist/bundle.css.map", "text"); + // } } start_server() { const setting = get_setting();