Add Caching
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import trimAllCache from "../cache/trim-all-cache";
|
||||
|
||||
export default async function cron() {
|
||||
while (true) {
|
||||
await trimAllCache();
|
||||
await Bun.sleep(AppData["DefaultCronInterval"]);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import handleWebPages from "./web-pages/handle-web-pages";
|
||||
import handleRoutes from "./handle-routes";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import grabConstants from "../../utils/grab-constants";
|
||||
import { AppData } from "../../data/app-data";
|
||||
|
||||
type Params = {
|
||||
dev?: boolean;
|
||||
@@ -15,6 +16,8 @@ export default async function (params?: Params): Promise<ServeOptions> {
|
||||
const port = grabAppPort();
|
||||
const { PUBLIC_DIR } = grabDirNames();
|
||||
|
||||
const is_dev = isDevelopment();
|
||||
|
||||
return {
|
||||
async fetch(req, server) {
|
||||
try {
|
||||
@@ -34,7 +37,7 @@ export default async function (params?: Params): Promise<ServeOptions> {
|
||||
}
|
||||
}
|
||||
|
||||
if (url.pathname === "/__hmr" && isDevelopment()) {
|
||||
if (url.pathname === "/__hmr" && is_dev) {
|
||||
const referer_url = new URL(
|
||||
req.headers.get("referer") || "",
|
||||
);
|
||||
@@ -95,7 +98,15 @@ export default async function (params?: Params): Promise<ServeOptions> {
|
||||
),
|
||||
);
|
||||
|
||||
return new Response(file);
|
||||
let res_opts: ResponseInit = {};
|
||||
|
||||
if (!is_dev && url.pathname.match(/__bunext/)) {
|
||||
res_opts.headers = {
|
||||
"Cache-Control": `public, max-age=${AppData["BunextStaticFilesCacheExpiry"]}, must-revalidate`,
|
||||
};
|
||||
}
|
||||
|
||||
return new Response(file, res_opts);
|
||||
}
|
||||
|
||||
if (url.pathname.startsWith("/favicon.")) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import grabDirNames from "../../utils/grab-dir-names";
|
||||
import EJSON from "../../utils/ejson";
|
||||
import { readFileSync } from "fs";
|
||||
import type { BundlerCTXMap } from "../../types";
|
||||
import cron from "./cron";
|
||||
|
||||
const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
||||
|
||||
@@ -36,6 +37,7 @@ export default async function startServer(params?: Params) {
|
||||
}
|
||||
global.BUNDLER_CTX_MAP = artifacts;
|
||||
global.IS_FIRST_BUNDLE_READY = true;
|
||||
cron();
|
||||
}
|
||||
|
||||
let bundle_ready_retries = 0;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user