From 0da46321b86895b10e6b6f8f06ead5ba5c8b776a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 17 Oct 2022 20:51:30 -0400 Subject: [PATCH] perf: Don't go to disk for html more than once (#4312) --- server/routes/app.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/server/routes/app.ts b/server/routes/app.ts index d192b2e48..49877ba41 100644 --- a/server/routes/app.ts +++ b/server/routes/app.ts @@ -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 => { 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;