From a3cc8ca0679d1c91b5ccc36e92b38d472bf17f68 Mon Sep 17 00:00:00 2001 From: rishab11250 Date: Wed, 15 Jul 2026 09:19:03 +0530 Subject: [PATCH] fix: invalidate HTML cache when files change on disk via mtime check (#281) --- server.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 302324fa..09fd5fd6 100644 --- a/server.js +++ b/server.js @@ -81,8 +81,16 @@ app.use((req, res, next) => { // 4. HTML page routes — inject per-request nonce into __NONCE__ placeholders const htmlCache = {}; function serveHtml(res, filePath) { - if (htmlCache[filePath]) { - const html = htmlCache[filePath].replace(/__NONCE__/g, res.locals.nonce); + let stats; + try { + stats = fs.statSync(filePath); + } catch (err) { + return res.status(500).send("Error loading page"); + } + + const cached = htmlCache[filePath]; + if (cached && cached.mtime >= stats.mtimeMs) { + const html = cached.data.replace(/__NONCE__/g, res.locals.nonce); return res.type("html").send(html); } @@ -90,7 +98,7 @@ function serveHtml(res, filePath) { if (err) { return res.status(500).send("Error loading page"); } - htmlCache[filePath] = data; + htmlCache[filePath] = { mtime: stats.mtimeMs, data }; const html = data.replace(/__NONCE__/g, res.locals.nonce); res.type("html").send(html); });