@@ -8,7 +8,7 @@ import { apiClient } from '~/lib/request'
8
8
const fontNormal = fetch (
9
9
'https://github.com/lxgw/LxgwWenKai/releases/download/v1.300/LXGWWenKai-Regular.ttf' ,
10
10
) . then ( ( res ) => res . arrayBuffer ( ) )
11
- export const runtime = 'edge'
11
+ // export const runtime = 'edge'
12
12
13
13
export const revalidate = 60 * 60 * 24 // 24 hours
14
14
export const GET = async ( req : NextRequest ) => {
@@ -17,8 +17,50 @@ export const GET = async (req: NextRequest) => {
17
17
18
18
const { searchParams } = req . nextUrl
19
19
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
22
64
23
65
const aggregation = await fetch ( apiClient . aggregate . proxy . toString ( true ) , {
24
66
next : {
@@ -28,7 +70,7 @@ export const GET = async (req: NextRequest) => {
28
70
29
71
const {
30
72
user : { avatar } ,
31
- seo : { title } ,
73
+ seo,
32
74
} = aggregation
33
75
34
76
if ( ! title )
@@ -37,17 +79,17 @@ export const GET = async (req: NextRequest) => {
37
79
{ status : 400 } ,
38
80
)
39
81
40
- const bgAccent = uniqolor ( titlePost + subtitlePost , {
82
+ const bgAccent = uniqolor ( title + subtitle , {
41
83
saturation : [ 30 , 35 ] ,
42
84
lightness : [ 60 , 70 ] ,
43
85
} ) . color
44
86
45
- const bgAccentLight = uniqolor ( titlePost + subtitlePost , {
87
+ const bgAccentLight = uniqolor ( title + subtitle , {
46
88
saturation : [ 30 , 35 ] ,
47
89
lightness : [ 80 , 90 ] ,
48
90
} ) . color
49
91
50
- const bgAccentUltraLight = uniqolor ( titlePost + subtitlePost , {
92
+ const bgAccentUltraLight = uniqolor ( title + subtitle , {
51
93
saturation : [ 30 , 35 ] ,
52
94
lightness : [ 95 , 96 ] ,
53
95
} ) . color
@@ -96,7 +138,7 @@ export const GET = async (req: NextRequest) => {
96
138
fontSize : '2rem' ,
97
139
} }
98
140
>
99
- < h3 > { title } </ h3 >
141
+ < h3 > { seo . title } </ h3 >
100
142
</ span >
101
143
</ div >
102
144
< div
@@ -119,15 +161,15 @@ export const GET = async (req: NextRequest) => {
119
161
lineClamp : 1 ,
120
162
} }
121
163
>
122
- { titlePost ?. slice ( 0 , 20 ) }
164
+ { title ?. slice ( 0 , 20 ) }
123
165
</ h1 >
124
166
< h2
125
167
style = { {
126
168
color : 'rgba(230, 230, 230, 0.85)' ,
127
169
fontSize : '3rem' ,
128
170
} }
129
171
>
130
- { subtitlePost }
172
+ { subtitle }
131
173
</ h2 >
132
174
</ div >
133
175
</ div >
0 commit comments