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
+20 -4
View File
@@ -2,8 +2,13 @@ import type { APIRoute } from "astro";
import { getCollection, type CollectionEntry } from "astro:content";
import { generateOgImageForPost } from "@/utils/generateOgImages";
import { slugifyStr } from "@/utils/slugify";
import { SITE } from "@/config";
export async function getStaticPaths() {
if (!SITE.dynamicOgImage) {
return [];
}
const posts = await getCollection("blog").then(p =>
p.filter(({ data }) => !data.draft && !data.ogImage)
);
@@ -14,7 +19,18 @@ export async function getStaticPaths() {
}));
}
export const GET: APIRoute = async ({ props }) =>
new Response(await generateOgImageForPost(props as CollectionEntry<"blog">), {
headers: { "Content-Type": "image/png" },
});
export const GET: APIRoute = async ({ props }) => {
if (!SITE.dynamicOgImage) {
return new Response(null, {
status: 404,
statusText: "Not found",
});
}
return new Response(
await generateOgImageForPost(props as CollectionEntry<"blog">),
{
headers: { "Content-Type": "image/png" },
}
);
};