perf: hasOgImage filter uses memoized Set lookup; drop getCallbacks() per-page call
Build & Deploy / build-and-deploy (push) Successful in 2m23s

This commit is contained in:
svemagie
2026-05-11 09:09:53 +02:00
parent 7c394384c5
commit 7d7cb3f13d
+5 -8
View File
@@ -404,15 +404,11 @@ export default function (eleventyConfig) {
// Performance: skip PostHTML parsing for pages without <img> tags. // Performance: skip PostHTML parsing for pages without <img> tags.
// Both registered PostHTML plugins (remote-image-marker, eleventy-img) only // Both registered PostHTML plugins (remote-image-marker, eleventy-img) only
// target <img> elements — no point parsing+serializing HTML without them. // target <img> elements — no point parsing+serializing HTML without them.
// Overrides the default @11ty/eleventy/html-transformer transform (same name // No URL transform callbacks are registered by any plugin, so the getCallbacks()
// overwrites via addTransform) with a pre-check that avoids the full PostHTML // check is omitted — it costs ~1ms × 639 pages for a result that is always false.
// parse/serialize cycle (~3ms/page) for image-free pages.
eleventyConfig.addTransform("@11ty/eleventy/html-transformer", async function(content) { eleventyConfig.addTransform("@11ty/eleventy/html-transformer", async function(content) {
if (typeof this.outputPath === "string" && this.outputPath.endsWith(".html") && !content.includes("<img")) { if (typeof this.outputPath === "string" && this.outputPath.endsWith(".html") && !content.includes("<img")) {
// Safety: if URL transform callbacks exist (they modify <a>, <link>, etc.) return content;
// we must still run the full pipeline even without images.
const hasUrlCallbacks = eleventyConfig.htmlTransformer.getCallbacks("html", this).length > 0;
if (!hasUrlCallbacks) return content;
} }
return eleventyConfig.htmlTransformer.transformContent(this.outputPath, content, this); return eleventyConfig.htmlTransformer.transformContent(this.outputPath, content, this);
}); });
@@ -1018,9 +1014,10 @@ export default function (eleventyConfig) {
}); });
// Check if a generated OG image exists for this slug // Check if a generated OG image exists for this slug
// Delegates to the memoized hasOgImage() above (directory listed once per build → Set lookup)
eleventyConfig.addFilter("hasOgImage", (slug) => { eleventyConfig.addFilter("hasOgImage", (slug) => {
if (!slug) return false; if (!slug) return false;
return existsSync(resolve(OG_CACHE_DIR, `${slug}.png`)); return hasOgImage(slug);
}); });
// Inline file contents (for critical CSS inlining) // Inline file contents (for critical CSS inlining)