Skip to content

Commit ffc2854

Browse files
committed
fix: og api
1 parent 19ba8a8 commit ffc2854

File tree

5 files changed

+74
-17
lines changed

5 files changed

+74
-17
lines changed

src/app/(page-detail)/[slug]/layout.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export const generateMetadata = async ({
3737
const { title, text } = data
3838
const description = getSummaryFromMd(text ?? '')
3939

40-
const ogImage = getOgUrl(title, '关于我')
40+
const ogImage = getOgUrl('page', {
41+
slug,
42+
})
43+
4144
return {
4245
title,
4346
description,

src/app/api/og/route.tsx

+52-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { apiClient } from '~/lib/request'
88
const fontNormal = fetch(
99
'https://github.com/lxgw/LxgwWenKai/releases/download/v1.300/LXGWWenKai-Regular.ttf',
1010
).then((res) => res.arrayBuffer())
11-
export const runtime = 'edge'
11+
// export const runtime = 'edge'
1212

1313
export const revalidate = 60 * 60 * 24 // 24 hours
1414
export const GET = async (req: NextRequest) => {
@@ -17,8 +17,50 @@ export const GET = async (req: NextRequest) => {
1717

1818
const { searchParams } = req.nextUrl
1919

20-
const titlePost = searchParams.get('title')
21-
const subtitlePost = searchParams.get('subtitle') || ''
20+
const dataString = searchParams.get('data') as string
21+
22+
let data:
23+
| { type: 'post'; category: string; slug: string }
24+
| { type: 'note'; nid: number }
25+
| {
26+
type: 'page'
27+
slug: string
28+
}
29+
30+
try {
31+
data = JSON.parse(decodeURIComponent(dataString))
32+
} catch {
33+
return new Response('Failed to parse the data.', { status: 400 })
34+
}
35+
36+
let document: { title: string; subtitle: string }
37+
38+
switch (data.type) {
39+
case 'post': {
40+
const { category, slug } = data
41+
document = await apiClient.post
42+
.getPost(category, slug)
43+
.then((r) => ({ title: r.title, subtitle: r.category.name }))
44+
break
45+
}
46+
47+
case 'note': {
48+
const { nid } = data
49+
document = await apiClient.note
50+
.getNoteById(+nid)
51+
.then((r) => ({ title: r.data.title, subtitle: '生活记录' }))
52+
break
53+
}
54+
case 'page': {
55+
const { slug } = data
56+
document = await apiClient.page.getBySlug(slug).then((data) => ({
57+
title: data.title,
58+
subtitle: data.subtitle || '',
59+
}))
60+
break
61+
}
62+
}
63+
const { title, subtitle } = document
2264

2365
const aggregation = await fetch(apiClient.aggregate.proxy.toString(true), {
2466
next: {
@@ -28,7 +70,7 @@ export const GET = async (req: NextRequest) => {
2870

2971
const {
3072
user: { avatar },
31-
seo: { title },
73+
seo,
3274
} = aggregation
3375

3476
if (!title)
@@ -37,17 +79,17 @@ export const GET = async (req: NextRequest) => {
3779
{ status: 400 },
3880
)
3981

40-
const bgAccent = uniqolor(titlePost + subtitlePost, {
82+
const bgAccent = uniqolor(title + subtitle, {
4183
saturation: [30, 35],
4284
lightness: [60, 70],
4385
}).color
4486

45-
const bgAccentLight = uniqolor(titlePost + subtitlePost, {
87+
const bgAccentLight = uniqolor(title + subtitle, {
4688
saturation: [30, 35],
4789
lightness: [80, 90],
4890
}).color
4991

50-
const bgAccentUltraLight = uniqolor(titlePost + subtitlePost, {
92+
const bgAccentUltraLight = uniqolor(title + subtitle, {
5193
saturation: [30, 35],
5294
lightness: [95, 96],
5395
}).color
@@ -96,7 +138,7 @@ export const GET = async (req: NextRequest) => {
96138
fontSize: '2rem',
97139
}}
98140
>
99-
<h3>{title}</h3>
141+
<h3>{seo.title}</h3>
100142
</span>
101143
</div>
102144
<div
@@ -119,15 +161,15 @@ export const GET = async (req: NextRequest) => {
119161
lineClamp: 1,
120162
}}
121163
>
122-
{titlePost?.slice(0, 20)}
164+
{title?.slice(0, 20)}
123165
</h1>
124166
<h2
125167
style={{
126168
color: 'rgba(230, 230, 230, 0.85)',
127169
fontSize: '3rem',
128170
}}
129171
>
130-
{subtitlePost}
172+
{subtitle}
131173
</h2>
132174
</div>
133175
</div>

src/app/notes/[id]/layout.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ export const generateMetadata = async ({
3737
const { title, text } = data
3838
const description = getSummaryFromMd(text ?? '')
3939

40-
const ogUrl = getOgUrl(title, '生活记录')
40+
const ogUrl = getOgUrl('note', {
41+
nid: params.id,
42+
})
4143

4244
return {
4345
title,

src/app/posts/(post-detail)/[category]/[slug]/layout.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ export const generateMetadata = async ({
3232
)
3333
const {
3434
title,
35-
category: { name },
35+
category: { slug: categorySlug },
3636
text,
3737
} = data
3838
const description = getSummaryFromMd(text ?? '')
3939

40-
const ogImage = getOgUrl(title, name)
40+
const ogImage = getOgUrl('post', {
41+
category: categorySlug,
42+
slug,
43+
})
4144

4245
return {
4346
title,

src/lib/helper.server.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ export const getHost = () => {
2929
return host
3030
}
3131

32-
export const getOgUrl = (title: string, subtitle: string) => {
32+
export const getOgUrl = (type: 'post' | 'note' | 'page', data: any) => {
3333
const ogUrl = new URL(`${isDev ? 'http' : 'https'}://${getHost()}/api/og`)
34-
ogUrl.searchParams.set('title', title)
35-
ogUrl.searchParams.set('subtitle', subtitle)
34+
ogUrl.searchParams.set(
35+
'data',
36+
encodeURIComponent(
37+
JSON.stringify({
38+
type,
39+
...data,
40+
}),
41+
),
42+
)
3643
return ogUrl
3744
}

0 commit comments

Comments
 (0)