perf: og-fix bail-out, ogDescription memoize, htmlmin conservativeCollapse
Build & Deploy / build-and-deploy (push) Successful in 2m35s
Build & Deploy / build-and-deploy (push) Successful in 2m35s
This commit is contained in:
+9
-5
@@ -455,6 +455,7 @@ export default function (eleventyConfig) {
|
||||
|
||||
eleventyConfig.addTransform("og-fix", function (content, outputPath) {
|
||||
if (!outputPath || !outputPath.endsWith(".html")) return content;
|
||||
if (!content.includes("__OG_IMAGE_PLACEHOLDER__")) return content;
|
||||
|
||||
// Derive correct page URL and OG slug from outputPath (immune to race condition)
|
||||
// Content pages match: .../type/slug/index.html
|
||||
@@ -688,9 +689,9 @@ export default function (eleventyConfig) {
|
||||
try {
|
||||
return await minify(content, {
|
||||
collapseWhitespace: true,
|
||||
conservativeCollapse: true,
|
||||
removeComments: true,
|
||||
html5: true,
|
||||
decodeEntities: true,
|
||||
minifyCSS: false,
|
||||
minifyJS: false,
|
||||
});
|
||||
@@ -828,23 +829,26 @@ export default function (eleventyConfig) {
|
||||
});
|
||||
|
||||
// Clean excerpt for OpenGraph - strips HTML, decodes entities, removes extra whitespace
|
||||
// Memoized: same post content is processed once per post page + once per listing page card
|
||||
const _ogDescCache = new Map();
|
||||
eleventyConfig.on("eleventy.before", () => { _ogDescCache.clear(); });
|
||||
eleventyConfig.addFilter("ogDescription", (content, len = 200) => {
|
||||
if (!content) return "";
|
||||
// Strip HTML tags
|
||||
const key = `${len}:${content.length}:${content.slice(0, 64)}`;
|
||||
const cached = _ogDescCache.get(key);
|
||||
if (cached !== undefined) return cached;
|
||||
let text = content.replace(/<[^>]+>/g, ' ');
|
||||
// Decode common HTML entities
|
||||
text = text.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, "'")
|
||||
.replace(/ /g, ' ');
|
||||
// Remove extra whitespace
|
||||
text = text.replace(/\s+/g, ' ').trim();
|
||||
// Truncate
|
||||
if (text.length > len) {
|
||||
text = text.slice(0, len).trim() + "...";
|
||||
}
|
||||
_ogDescCache.set(key, text);
|
||||
return text;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user