fix: use full date-prefixed filenames for OG images

The Nunjucks race condition in Eleventy 3.x affects page.url too —
its value changes between {% set %} and {{ }} within the same
template render during parallel builds. Instead of trying to derive
slugs from page data, name OG images with the full filename
(including date prefix) to match URL path segments exactly.
This commit is contained in:
Ricardo
2026-02-18 17:08:05 +01:00
parent 8d800e2c28
commit b065dbfed7
2 changed files with 9 additions and 8 deletions
+3 -3
View File
@@ -363,11 +363,11 @@ export default function (eleventyConfig) {
});
// Derive OG slug from page.url (reliable) instead of page.fileSlug
// (which suffers from Nunjucks race conditions in Eleventy 3.x parallel rendering)
// (which suffers from Nunjucks race conditions in Eleventy 3.x parallel rendering).
// OG images are named with the full date prefix to match URL segments exactly.
eleventyConfig.addFilter("ogSlug", (url) => {
if (!url) return "";
const last = url.replace(/\/$/, "").split("/").pop();
return last.replace(/^\d{4}-\d{2}-\d{2}-/, "");
return url.replace(/\/$/, "").split("/").pop();
});
// Check if a generated OG image exists for this slug
+6 -5
View File
@@ -106,11 +106,12 @@ function formatDate(dateStr) {
}
/**
* Strip YYYY-MM-DD- date prefix from filename to match Eleventy's page.fileSlug.
* Eleventy v3 parses dates from filenames and removes the prefix from fileSlug.
* Use the full filename (with date prefix) as the OG image slug.
* This matches the URL path segment directly, avoiding Eleventy's page.fileSlug
* race condition in Nunjucks parallel rendering.
*/
function toEleventySlug(filename) {
return filename.replace(/^\d{4}-\d{2}-\d{2}-/, "");
function toOgSlug(filename) {
return filename;
}
function truncateTitle(title, max = 120) {
@@ -307,7 +308,7 @@ export async function generateOgImages(contentDir, cacheDir, siteName) {
continue;
}
const slug = toEleventySlug(basename(filePath, ".md"));
const slug = toOgSlug(basename(filePath, ".md"));
const title = fm.title || fm.name || extractBodyText(raw);
const date = fm.published || fm.date || "";
const postType = detectPostType(filePath);