og to png

This commit is contained in:
2025-12-28 11:31:52 +08:00
parent 0a959abcb3
commit 5786a99b58
3 changed files with 14 additions and 3 deletions

View File

@@ -20,7 +20,7 @@ const ogImage = {
url: new URL(`/api/og/${encodeURIComponent(post.id)}`, siteUrl).toString(),
width: 1200,
height: 630,
type: 'image/svg+xml',
type: 'image/png',
}
const canonicalUrl = Astro.url
---

View File

@@ -1,4 +1,5 @@
import type { APIRoute } from 'astro'
import { Resvg } from '@resvg/resvg-js'
import { SITE } from '@/consts'
import { fetchRemotePost, normalizePost } from '@/lib/data-utils'
import { formatDate } from '@/lib/utils'
@@ -158,10 +159,19 @@ export const GET: APIRoute = async ({ params }) => {
siteHost,
})
return new Response(svg, {
const png = new Resvg(svg, {
fitTo: {
mode: 'width',
value: WIDTH,
},
})
.render()
.asPng()
return new Response(png, {
status: 200,
headers: {
'Content-Type': 'image/svg+xml; charset=utf-8',
'Content-Type': 'image/png',
'Cache-Control':
'public, max-age=0, s-maxage=86400, stale-while-revalidate=604800',
},