Skip to content

Commit 01a7525

Browse files
committed
Added dynamic OG Generation via vercel
1 parent 3bf3f42 commit 01a7525

File tree

4 files changed

+254
-3
lines changed

4 files changed

+254
-3
lines changed

package-lock.json

Lines changed: 191 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"@astrojs/mdx": "^4.3.1",
1414
"@astrojs/rss": "^4.0.12",
1515
"@astrojs/sitemap": "^3.4.1",
16+
"@vercel/og": "^0.8.5",
1617
"astro": "^5.12.0",
1718
"sharp": "^0.34.2"
1819
}
19-
}
20+
}

src/components/BaseHead.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const canonicalURL = new URL(Astro.url.pathname, Astro.site);
1616
1717
const { title, description, image } = Astro.props;
1818
19-
// Use custom OG image from public folder, or provided image, or fallback
19+
// Generate dynamic OG image URL with title as parameter, or use provided image
2020
const ogImageUrl = image
2121
? new URL(image.src, Astro.url)
22-
: new URL('/og-image.png', Astro.url);
22+
: new URL(`/og/image.png?title=${encodeURIComponent(title)}`, Astro.url);
2323
---
2424

2525
<!-- Global Metadata -->

src/pages/og/[...route].ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ImageResponse } from '@vercel/og';
2+
import type { APIRoute } from 'astro';
3+
4+
export const GET: APIRoute = async ({ url }) => {
5+
const title = url.searchParams.get('title') || 'PwnFuzz Labs';
6+
7+
return new ImageResponse(
8+
{
9+
type: 'div',
10+
props: {
11+
style: {
12+
height: '100%',
13+
width: '100%',
14+
display: 'flex',
15+
flexDirection: 'column',
16+
alignItems: 'center',
17+
justifyContent: 'center',
18+
backgroundColor: '#1a1b26',
19+
padding: '80px',
20+
fontFamily: 'sans-serif',
21+
},
22+
children: [
23+
{
24+
type: 'div',
25+
props: {
26+
style: {
27+
fontSize: '64px',
28+
fontWeight: 'bold',
29+
color: '#c0caf5',
30+
textAlign: 'center',
31+
lineHeight: '1.2',
32+
maxWidth: '1000px',
33+
},
34+
children: title,
35+
},
36+
},
37+
{
38+
type: 'div',
39+
props: {
40+
style: {
41+
position: 'absolute',
42+
bottom: '40px',
43+
right: '60px',
44+
fontSize: '28px',
45+
fontWeight: '600',
46+
color: '#ff9e64',
47+
},
48+
children: 'PwnFuzz Labs',
49+
},
50+
},
51+
],
52+
},
53+
},
54+
{
55+
width: 1200,
56+
height: 630,
57+
}
58+
);
59+
};

0 commit comments

Comments
 (0)