perf: Don't go to disk for html more than once (#4312)

This commit is contained in:
Tom Moor
2022-10-17 20:51:30 -04:00
committed by GitHub
parent cbb2bdf80c
commit 0da46321b8

View File

@@ -12,14 +12,25 @@ import prefetchTags from "@server/utils/prefetchTags";
const isProduction = env.ENVIRONMENT === "production";
const isTest = env.ENVIRONMENT === "test";
const readFile = util.promisify(fs.readFile);
let indexHtmlCache: Buffer | undefined;
const readIndexFile = async (ctx: Context): Promise<Buffer> => {
if (isProduction) {
return readFile(path.join(__dirname, "../../app/index.html"));
return (
indexHtmlCache ??
(indexHtmlCache = await readFile(
path.join(__dirname, "../../app/index.html")
))
);
}
if (isTest) {
return readFile(path.join(__dirname, "../static/index.html"));
return (
indexHtmlCache ??
(indexHtmlCache = await readFile(
path.join(__dirname, "../static/index.html")
))
);
}
const middleware = ctx.devMiddleware;