mirror of
https://github.com/svemagie/blog-eleventy-indiekit.git
synced 2026-05-14 22:48:50 +02:00
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:
+3
-3
@@ -363,11 +363,11 @@ export default function (eleventyConfig) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Derive OG slug from page.url (reliable) instead of page.fileSlug
|
// 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) => {
|
eleventyConfig.addFilter("ogSlug", (url) => {
|
||||||
if (!url) return "";
|
if (!url) return "";
|
||||||
const last = url.replace(/\/$/, "").split("/").pop();
|
return url.replace(/\/$/, "").split("/").pop();
|
||||||
return last.replace(/^\d{4}-\d{2}-\d{2}-/, "");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if a generated OG image exists for this slug
|
// Check if a generated OG image exists for this slug
|
||||||
|
|||||||
@@ -106,11 +106,12 @@ function formatDate(dateStr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strip YYYY-MM-DD- date prefix from filename to match Eleventy's page.fileSlug.
|
* Use the full filename (with date prefix) as the OG image slug.
|
||||||
* Eleventy v3 parses dates from filenames and removes the prefix from fileSlug.
|
* This matches the URL path segment directly, avoiding Eleventy's page.fileSlug
|
||||||
|
* race condition in Nunjucks parallel rendering.
|
||||||
*/
|
*/
|
||||||
function toEleventySlug(filename) {
|
function toOgSlug(filename) {
|
||||||
return filename.replace(/^\d{4}-\d{2}-\d{2}-/, "");
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
function truncateTitle(title, max = 120) {
|
function truncateTitle(title, max = 120) {
|
||||||
@@ -307,7 +308,7 @@ export async function generateOgImages(contentDir, cacheDir, siteName) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const slug = toEleventySlug(basename(filePath, ".md"));
|
const slug = toOgSlug(basename(filePath, ".md"));
|
||||||
const title = fm.title || fm.name || extractBodyText(raw);
|
const title = fm.title || fm.name || extractBodyText(raw);
|
||||||
const date = fm.published || fm.date || "";
|
const date = fm.published || fm.date || "";
|
||||||
const postType = detectPostType(filePath);
|
const postType = detectPostType(filePath);
|
||||||
|
|||||||
Reference in New Issue
Block a user