fix: og-image got lost ... again
All checks were successful
Build & Deploy / build-and-deploy (push) Successful in 1m15s

This commit is contained in:
svemagie
2026-03-31 19:16:45 +02:00
parent bb6e866978
commit c40456bcae
2 changed files with 20 additions and 11 deletions

View File

@@ -403,18 +403,19 @@ export default function (eleventyConfig) {
if (!outputPath || !outputPath.endsWith(".html")) return content;
// Derive correct page URL and OG slug from outputPath (immune to race condition)
// Content pages match: .../type/slug/index.html (plain URLs, no date segments)
// Content pages match: .../type/yyyy/mm/dd/slug/index.html
const postMatch = outputPath.match(
/\/([\w-]+)\/([\w-]+)\/index\.html$/
/\/([\w-]+)\/(\d{4})\/(\d{2})\/(\d{2})\/([\w-]+)\/index\.html$/
);
if (postMatch) {
const [, type, slug] = postMatch;
const pageUrlPath = `/${type}/${slug}/`;
const [, type, year, month, day, slug] = postMatch;
const pageUrlPath = `/${type}/${year}/${month}/${day}/${slug}/`;
const correctFullUrl = `${siteUrl}${pageUrlPath}`;
const hasOg = hasOgImage(slug);
const ogSlug = `${year}-${month}-${day}-${slug}`;
const hasOg = hasOgImage(ogSlug);
const ogImageUrl = hasOg
? `${siteUrl}/og/${slug}.png`
? `${siteUrl}/og/${ogSlug}.png`
: `${siteUrl}/images/og-default.png`;
const twitterCard = hasOg ? "summary_large_image" : "summary";

View File

@@ -126,11 +126,19 @@ function formatDate(dateStr) {
}
/**
* Use the filename (without extension) as the OG image slug.
* Matches the last URL path segment, which Eleventy derives from the filename.
* Use the date+slug (e.g., 2024-03-31-my-post) as the OG image slug.
* Matches the canonical permalink structure.
*/
function toOgSlug(filename) {
return filename;
function toOgSlug(filePath, fm) {
// filePath: content/{type}/{yyyy}-{MM}-{dd}-{slug}.md
const filename = basename(filePath);
const match = filename.match(/(\d{4})-(\d{2})-(\d{2})-(.+)\.md$/);
if (match) {
const [, year, month, day, slug] = match;
return `${year}-${month}-${day}-${slug}`;
}
// fallback: use filename only (without extension)
return filename.replace(/\.md$/, "");
}
/**
@@ -453,7 +461,7 @@ export async function generateOgImages(contentDir, cacheDir, siteName, batchSize
continue;
}
const slug = toOgSlug(basename(filePath, ".md"));
const slug = toOgSlug(filePath, fm);
const postType = detectPostType(filePath);
const date = fm.published || fm.date || "";