Add Caching
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user