mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 19:41:02 +08:00
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:
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user