-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathog.tsx
More file actions
142 lines (134 loc) · 3.62 KB
/
og.tsx
File metadata and controls
142 lines (134 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { ImageResponse } from '@vercel/og'
import { NextRequest } from 'next/server'
export const config = {
runtime: 'edge'
}
const COPY_FALLBACK = {
title: 'The Digital Infrastructure of Global Hacker Movement',
desc: 'Dora Factory creates protocols, toolings, and public good infrastructures that help open source communities and frontier tech builders to thrive.'
}
const CHAR_LIMIT = {
title: 140,
desc: 280
}
function arrayBufferToBase64(buffer: ArrayBuffer) {
var binary = ''
var bytes = new Uint8Array(buffer)
var len = bytes.byteLength
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i])
}
return btoa(binary)
}
async function fetchAssets(): Promise<
[
{ name: string; data: ArrayBuffer; weight: 400 | 800; style: 'normal' }[],
string
]
> {
const [publicSans400, publicSans800, bg] = await Promise.all([
fetch(
new URL('../../assets/PublicSans-Regular.woff', import.meta.url)
).then(res => res.arrayBuffer()),
fetch(
new URL('../../assets/PublicSans-ExtraBold.woff', import.meta.url)
).then(res => res.arrayBuffer()),
fetch(new URL('../../assets/og-bg.png', import.meta.url)).then(res =>
res.arrayBuffer()
)
])
return [
[
{
name: 'Public Sans',
data: publicSans400,
weight: 400,
style: 'normal'
},
{
name: 'Public Sans',
data: publicSans800,
weight: 800,
style: 'normal'
}
],
arrayBufferToBase64(bg)
]
}
export default async function handler(request: NextRequest) {
try {
const [fonts, bg] = await fetchAssets()
const { searchParams } = new URL(request.url)
// ?title=<title>
const hasTitle = searchParams.has('title')
const title = hasTitle
? searchParams.get('title')?.slice(0, CHAR_LIMIT.title)
: COPY_FALLBACK.title
const hasDesc = searchParams.has('desc')
const desc = hasDesc
? searchParams.get('desc')?.slice(0, CHAR_LIMIT.desc)
: COPY_FALLBACK.desc
return new ImageResponse(
(
<div
style={{
display: 'flex',
width: '100%',
height: '100%',
backgroundColor: '#111111',
backgroundImage: `url(data:image/png;base64,${bg})`,
backgroundSize: '1200px 628px'
}}
>
<div
style={{
display: 'flex',
flexDirection: 'column',
width: '744px',
margin: '200px 324px 0 132px'
}}
>
<div
style={{
display: 'flex',
color: 'rgba(0, 0, 0, 0)',
fontFamily: '"Public Sans"',
fontSize: '54px',
fontWeight: 800,
lineHeight: '60px',
// textWrap: 'wrap',
backgroundClip: 'text',
backgroundImage:
'linear-gradient(72deg, rgba(255, 255, 255, 0.8) 0%, rgba(255, 255, 255, 0.98) 50%)'
}}
>
{title}
</div>
<div
style={{
marginTop: '24px',
color: '#9B9BB0',
fontFamily: '"Public Sans"',
fontSize: '24px',
fontWeight: 400,
lineHeight: '32px'
}}
>
{desc}
</div>
</div>
</div>
),
{
width: 1200,
height: 628,
fonts
}
)
} catch (e: any) {
console.log(`${e.message}`)
return new Response(`Failed to generate the OG image`, {
status: 500
})
}
}