From 3dc258992b5346f4b57ad64993ff317f5f398574 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 27 Jan 2026 08:54:57 +0000 Subject: [PATCH] Cache css/main.css read in optimize-html.js Eliminate repeated synchronous file I/O by caching the CSS content. Co-authored-by: si <18108+si@users.noreply.github.com> --- _11ty/optimize-html.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/_11ty/optimize-html.js b/_11ty/optimize-html.js index 197d18e..d3ef9ce 100644 --- a/_11ty/optimize-html.js +++ b/_11ty/optimize-html.js @@ -37,6 +37,8 @@ const csso = require("csso"); * Optimizes AMP */ +let cachedCss = null; + const purifyCss = async (rawContent, outputPath) => { let content = rawContent; if ( @@ -45,14 +47,18 @@ const purifyCss = async (rawContent, outputPath) => { !isAmp(content) && !/data-style-override/.test(content) ) { - let before = require("fs").readFileSync("css/main.css", { - encoding: "utf-8", - }); + if (!cachedCss) { + cachedCss = require("fs").readFileSync("css/main.css", { + encoding: "utf-8", + }); + + cachedCss = cachedCss.replace( + /@font-face {/g, + "@font-face {font-display:optional;" + ); + } - before = before.replace( - /@font-face {/g, - "@font-face {font-display:optional;" - ); + let before = cachedCss; const purged = await new PurgeCSS().purge({ content: [