From 9ba7980bc328fc88fa11f71b23c44fd9a3a9edd2 Mon Sep 17 00:00:00 2001 From: svemagie <869694+svemagie@users.noreply.github.com> Date: Thu, 19 Mar 2026 22:15:30 +0100 Subject: [PATCH] perf: memoize hash filter to eliminate redundant disk reads per build Co-Authored-By: Claude Sonnet 4.6 --- eleventy.config.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/eleventy.config.js b/eleventy.config.js index f823fa0..32dd0d1 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -740,14 +740,17 @@ export default function (eleventyConfig) { return url.endsWith("/") ? url.slice(0, -1) : url; }); - // Hash filter for cache busting - generates MD5 hash of file content + // Hash filter for cache busting — memoized (same paths repeat across every page render) + const _hashCache = new Map(); + eleventyConfig.on("eleventy.before", () => { _hashCache.clear(); }); eleventyConfig.addFilter("hash", (filePath) => { + if (_hashCache.has(filePath)) return _hashCache.get(filePath); try { const fullPath = resolve(__dirname, filePath.startsWith("/") ? `.${filePath}` : filePath); - const content = readFileSync(fullPath); - return createHash("md5").update(content).digest("hex").slice(0, 8); + const result = createHash("md5").update(readFileSync(fullPath)).digest("hex").slice(0, 8); + _hashCache.set(filePath, result); + return result; } catch { - // Return timestamp as fallback if file not found return Date.now().toString(36); } });