From 52db89e05b10d054b3fbe638f389f44ac490f1d4 Mon Sep 17 00:00:00 2001 From: Rizky Date: Tue, 2 Jul 2024 09:10:41 +0700 Subject: [PATCH] fix --- app/srv/api/prod.ts | 30 +++++++++++++++++++++++++----- pkgs/core/utils/global.ts | 5 +++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/srv/api/prod.ts b/app/srv/api/prod.ts index fcca69c0..970b8677 100644 --- a/app/srv/api/prod.ts +++ b/app/srv/api/prod.ts @@ -333,12 +333,32 @@ export const _ = { } else if (pathname === "index.html" || pathname === "_") { return index_html; } else { - const res = dir.path(`/app/srv/core/${pathname}`); - const file = Bun.file(res); - if (!(await file.exists())) { - return index_html; + const src_path = dir.path(`/app/srv/core/${pathname}`); + + if (!g.main_cache) g.main_cache = {}; + if (!g.main_cache[src_path]) { + if (!g.br) { + g.br = await brotliPromise; + } + const file = Bun.file(src_path); + if (await file.exists()) { + g.main_cache[src_path] = { + content: g.br.compress( + new Uint8Array(await Bun.file(src_path).arrayBuffer()) + ), + type: mime.getType(src_path) || "", + }; + } } - return new Response(file); + if (g.main_cache[src_path]) { + return new Response(g.main_cache[src_path].content, { + headers: { + "content-encoding": "br", + "content-type": g.main_cache[src_path].type, + }, + }); + } + return index_html; } }, }; diff --git a/pkgs/core/utils/global.ts b/pkgs/core/utils/global.ts index f53a9491..c0ec5a0d 100644 --- a/pkgs/core/utils/global.ts +++ b/pkgs/core/utils/global.ts @@ -65,7 +65,8 @@ export const g = global as unknown as { code_index_compressing: Set; code_index_cache: Record< string, - Record + Record >; - br: BrotliWasmType + main_cache: Record; + br: BrotliWasmType; };