Add Caching

This commit is contained in:
2026-03-17 21:47:12 +01:00
parent 35930857fd
commit 32e914fdc1
17 changed files with 298 additions and 18 deletions
@@ -46,7 +46,7 @@ export default async function genWebHTML({
}</script>\n`;
if (bundledMap?.path) {
html += ` <script src="/${bundledMap.path}" type="module" defer></script>\n`;
html += ` <script src="/${bundledMap.path}" type="module" async></script>\n`;
}
if (isDevelopment()) {
@@ -1,5 +1,7 @@
import type { GrabPageComponentRes } from "../../../types";
import isDevelopment from "../../../utils/is-development";
import getCache from "../../cache/get-cache";
import writeCache from "../../cache/write-cache";
import genWebHTML from "./generate-web-html";
import grabPageComponent from "./grab-page-component";
import grabPageErrorComponent from "./grab-page-error-component";
@@ -12,6 +14,24 @@ export default async function handleWebPages({
req,
}: Params): Promise<Response> {
try {
if (!isDevelopment()) {
const url = new URL(req.url);
const key = url.pathname + (url.search || "");
const existing_cache = getCache({ key, paradigm: "html" });
if (existing_cache) {
const res_opts: ResponseInit = {
headers: {
"Content-Type": "text/html",
"X-Bunext-Cache": "HIT",
},
};
return new Response(existing_cache, res_opts);
}
}
const componentRes = await grabPageComponent({ req });
return await generateRes(componentRes);
} catch (error: any) {
@@ -65,6 +85,20 @@ async function generateRes({
};
}
const cache_page =
module.config?.cachePage || serverRes?.cachePage || false;
const expiry_seconds = module.config?.cacheExpiry || serverRes?.cacheExpiry;
if (cache_page && routeParams?.url) {
const key = routeParams.url.pathname + (routeParams.url.search || "");
writeCache({
key,
value: html,
paradigm: "html",
expiry_seconds,
});
}
const res = new Response(html, res_opts);
if (routeParams?.resTransform) {