forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocial-image.tsx
205 lines (191 loc) · 5.78 KB
/
social-image.tsx
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { ImageResponse } from '@vercel/og'
import ky from 'ky'
import { type NextRequest } from 'next/server'
import { api, apiHost, rootNotionPageId } from '@/lib/config'
import { type NotionPageInfo } from '@/lib/types'
const host = process.env.VERCEL_URL
? `https://${process.env.VERCEL_URL}`
: `http://localhost:3000`
const interRegularFont = fetch(`${host}/fonts/Inter-Regular.ttf`).then((res) =>
res.arrayBuffer()
)
const interBoldFont = fetch(`${host}/fonts/Inter-SemiBold.ttf`).then((res) =>
res.arrayBuffer()
)
export const config = {
runtime: 'edge'
}
export default async function GET(req: Request) {
const { searchParams } = new URL(req.url)
const pageId = searchParams.get('id') || rootNotionPageId
if (!pageId) {
return new Response('Invalid notion page id', { status: 400 })
}
const pageInfoRes = await fetch(`${apiHost}${api.getNotionPageInfo}`, {
method: 'POST',
body: JSON.stringify({ pageId }),
headers: {
'content-type': 'application/json'
}
})
if (!pageInfoRes.ok) {
return new Response(pageInfoRes.statusText, { status: pageInfoRes.status })
}
const pageInfo: NotionPageInfo = await pageInfoRes.json()
// console.log(pageInfo)
const showText = searchParams.get('text') !== 'false'
// Wait for both fonts to load
const [fontRegular, fontBold] = await Promise.all([
interRegularFont,
interBoldFont
])
// NOTE: The image is pulled from the header cover of the Notion page!
try {
const imgResponse = new ImageResponse(
(
<div
style={{
position: 'relative',
width: '100%',
height: '100%',
display: 'flex',
flexDirection: 'column',
backgroundColor: '#1F2027',
alignItems: 'center',
justifyContent: 'center',
fontFamily: '"Inter", sans-serif',
color: 'black'
}}
>
{pageInfo.image && (
<img
src={pageInfo.image}
style={{
position: 'absolute',
width: '100%',
height: '100%',
objectFit: 'cover',
filter: showText ? 'blur(2px)' : 'none'
}}
/>
)}
{showText && (
<>
<div
style={{
position: 'relative',
width: 900,
height: 465,
display: 'flex',
flexDirection: 'column',
border: '16px solid rgba(0,0,0,0.3)',
borderRadius: 12,
zIndex: 1
}}
>
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-around',
backgroundColor: '#fff',
padding: 36
}}
>
<div
style={{
fontSize: 60,
fontWeight: 700,
fontFamily: 'Inter',
display: '-webkit-box',
WebkitLineClamp: 3,
WebkitBoxOrient: 'vertical',
overflow: 'hidden',
textOverflow: 'ellipsis'
}}
>
{pageInfo.title}
</div>
<hr
style={{
borderTop: '2px solid rgba(0, 0, 0, 0.3)',
margin: '10px 0'
}}
/>
{pageInfo.detail && (
<div
style={{
display: 'flex',
flexDirection: 'column',
fontSize: 32,
opacity: 0.6,
overflow: 'hidden',
textOverflow: 'ellipsis',
maxWidth: '100%'
}}
>
<div>{pageInfo.detail}</div>
{pageInfo.author && (
<div style={{ alignItems: 'flex-start' }}>
{pageInfo.author}
</div>
)}
</div>
)}
</div>
</div>
{pageInfo.authorImage && (
<div
style={{
position: 'absolute',
top: 47,
left: 104,
height: 128,
width: 128,
display: 'flex',
borderRadius: '50%',
border: '4px solid #fff',
zIndex: '5'
}}
>
<img
src={pageInfo.authorImage}
style={{
width: '100%',
height: '100%'
// transform: 'scale(1.04)'
}}
/>
</div>
)}
</>
)}
</div>
),
{
width: 1200,
height: 630,
fonts: [
{
name: 'Inter',
data: fontRegular,
style: 'normal',
weight: 400
},
{
name: 'Inter',
data: fontBold,
style: 'normal',
weight: 700
}
]
}
)
return imgResponse
} catch (error) {
console.error('Failed to create ImageResponse:', error)
return new Response('Internal Server Error', { status: 500 })
}
}