This commit is contained in:
Rizky 2024-07-02 09:10:41 +07:00
parent 398f9e6926
commit 52db89e05b
2 changed files with 28 additions and 7 deletions

View File

@ -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;
}
},
};

View File

@ -65,7 +65,8 @@ export const g = global as unknown as {
code_index_compressing: Set<string>;
code_index_cache: Record<
string,
Record<string, { ts: number; content: any;type: string; }>
Record<string, { ts: number; content: any; type: string }>
>;
br: BrotliWasmType
main_cache: Record<string, { content: any; type: string }>;
br: BrotliWasmType;
};