fix: add an option to disable dynamic OG image generation (#476)

* fix: add an option to disable dynamic OG image generation

Add new `dynamicOgImage` option in the `SITE` object inside 
`src/config.ts` file to disable auto og-image generation during build.

Resolves #428

* docs: update guides for `SITE.dynamicOgImage` option
This commit is contained in:
Sat Naing
2025-03-12 20:47:12 +07:00
committed by GitHub
parent dc5ca47b1d
commit 6749f62015
6 changed files with 70 additions and 29 deletions
+2 -2
View File
@@ -23,14 +23,14 @@ const {
author = SITE.author,
profile = SITE.profile,
description = SITE.desc,
ogImage = SITE.ogImage,
ogImage = SITE.ogImage ? `/${SITE.ogImage}` : "/og.png",
canonicalURL = new URL(Astro.url.pathname, Astro.url),
pubDatetime,
modDatetime,
scrollSmooth = false,
} = Astro.props;
const socialImageURL = new URL(ogImage ?? SITE.ogImage ?? "og.png", Astro.url);
const socialImageURL = new URL(ogImage, Astro.url);
const structuredData = {
"@context": "https://schema.org",
+20 -7
View File
@@ -24,7 +24,7 @@ const {
title,
author,
description,
ogImage,
ogImage: initOgImage,
canonicalURL,
pubDatetime,
modDatetime,
@@ -34,11 +34,24 @@ const {
const { Content } = await render(post);
const ogImageUrl = typeof ogImage === "string" ? ogImage : ogImage?.src;
const ogUrl = new URL(
ogImageUrl ?? `/posts/${slugifyStr(title)}/index.png`,
Astro.url.origin
).href;
let ogImageUrl: string | undefined;
// Determine OG image source
if (typeof initOgImage === "string") {
ogImageUrl = initOgImage; // Remote OG image (absolute URL)
} else if (initOgImage?.src) {
ogImageUrl = initOgImage.src; // Local asset
}
// Use dynamic OG image if enabled and no remote|local ogImage
if (!ogImageUrl && SITE.dynamicOgImage) {
ogImageUrl = `/posts/${slugifyStr(title)}/index.png`;
}
// Resolve OG image URL (or fallback to SITE.ogImage / default `og.png`)
const ogImage = ogImageUrl
? new URL(ogImageUrl, Astro.url.origin).href
: undefined;
const layoutProps = {
title: `${title} | ${SITE.title}`,
@@ -47,7 +60,7 @@ const layoutProps = {
pubDatetime,
modDatetime,
canonicalURL,
ogImage: ogUrl,
ogImage,
scrollSmooth: true,
};