Skip to content

Commit 0618de2

Browse files
committed
feat: og
1 parent 1bc6b40 commit 0618de2

File tree

6 files changed

+62
-47
lines changed

6 files changed

+62
-47
lines changed

config/seo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ export const seo = {
99
: 'http://localhost:3000',
1010
),
1111
} as const;
12+
13+
export const siteUrl = new URL(
14+
process.env.NODE_ENV === 'production' ? 'https://blog-rbtb.vercel.app/' : 'http://localhost:3000',
15+
);

markdown/index.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[
2-
{
2+
{ "authors": ["yy"],
33
"path": "yyblog.md",
44
"title": "yyblog",
55
"tag": "技术/react",
66
"coverImage": "https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/0b76076eab124179816d68b846280cdc~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAgTWVnYXRyb25LaW5n:q75.awebp?rk3s=f64ab15b&x-expires=1731736114&x-signature=3ggdE9od5WvMZaVJ7VFlQtt48rA%3D"
77
},
8-
{
8+
{ "authors": ["yy"],
99
"path": "react19.md",
1010
"title": "react19",
1111
"tag": "随笔/生活",

src/app/(app)/notes/[nid]/page.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,31 @@ export const generateMetadata = async ({ params }: { params: any }): Promise<Met
2727
const postData = postDataMap[nid];
2828
const { title, summary } = postData;
2929

30+
const ogParams = new URLSearchParams();
31+
ogParams.set('title', postData.title);
32+
ogParams.set('tag', postData.tag);
33+
3034
return {
35+
authors: postData.authors?.map((author) => ({
36+
name: author,
37+
})),
38+
3139
title: { absolute: title },
3240
description: summary,
41+
alternates: { canonical: `/notes/${nid}` },
42+
openGraph: {
43+
authors: postData.authors,
44+
title: postData.title,
45+
description: postData.summary,
46+
type: 'article',
47+
url: `/notes/${nid}`,
48+
images: [
49+
{
50+
url: `/api/og?${ogParams.toString()}`,
51+
alt: postData.title,
52+
},
53+
],
54+
},
3355
} satisfies Metadata;
3456
} catch {
3557
return {};

src/app/api/og/router.tsx renamed to src/app/api/og/route.tsx

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ import { ImageResponseOptions, NextRequest } from 'next/server';
33
import uniqolor from 'uniqolor';
44

55
import { seo } from '~/index';
6-
import { buildPostData } from '@/core';
76

87
export const runtime = 'edge';
98

10-
const { postDataMap } = buildPostData();
11-
129
const resOptions = {
1310
width: 1200,
1411
height: 600,
@@ -18,7 +15,8 @@ const resOptions = {
1815
['cdn-cache-control', 'max-age=3600, stale-while-revalidate=600'],
1916
]),
2017
} as ImageResponseOptions;
21-
const HomeOGImage = () => {
18+
19+
const HomeOGImage = ({ img }: { img: ArrayBuffer }) => {
2220
const seed = Math.random().toString(36).substring(7);
2321
const bgAccent = uniqolor(seed, {
2422
saturation: [30, 35],
@@ -41,25 +39,21 @@ const HomeOGImage = () => {
4139
display: 'flex',
4240
height: '100%',
4341
width: '100%',
44-
4542
background: `linear-gradient(37deg, ${bgAccent} 27.82%, ${bgAccentLight} 79.68%, ${bgAccentUltraLight} 100%)`,
46-
4743
fontFamily: 'Noto Sans, Inter, "Material Icons"',
48-
4944
padding: '80px 15rem',
5045
alignItems: 'center',
5146
justifyContent: 'space-between',
5247
}}
5348
>
5449
<img
55-
src="/image/owner.jpg"
50+
src={img as unknown as any}
5651
style={{
5752
borderRadius: '50%',
5853
}}
5954
height={256}
6055
width={256}
6156
/>
62-
6357
<div
6458
style={{
6559
display: 'flex',
@@ -97,36 +91,31 @@ const HomeOGImage = () => {
9791
};
9892

9993
export const GET = async (req: NextRequest) => {
94+
const imageData = await fetch(
95+
new URL('../../../../public/image/owner.jpg', import.meta.url),
96+
).then((res) => res.arrayBuffer());
97+
10098
try {
10199
const { searchParams } = req.nextUrl;
102100

103-
const dataString = searchParams.get('nid') as string;
101+
const title = searchParams.get('title');
102+
const tag = searchParams.get('tag');
104103

105-
if (!dataString) {
106-
return new ImageResponse(<HomeOGImage />, resOptions);
104+
if (!title || !tag) {
105+
return new ImageResponse(<HomeOGImage img={imageData} />, resOptions);
107106
}
108107

109-
try {
110-
if (!(dataString in postDataMap)) {
111-
throw new Error('No such post');
112-
}
113-
} catch {
114-
return new Response('Failed to parse the data.', { status: 400 });
115-
}
116-
117-
const postName = postDataMap[dataString].title;
118-
119-
const bgAccent = uniqolor(postName, {
108+
const bgAccent = uniqolor(title, {
120109
saturation: [30, 35],
121110
lightness: [60, 70],
122111
}).color;
123112

124-
const bgAccentLight = uniqolor(postName, {
113+
const bgAccentLight = uniqolor(title, {
125114
saturation: [30, 35],
126115
lightness: [80, 90],
127116
}).color;
128117

129-
const bgAccentUltraLight = uniqolor(postName, {
118+
const bgAccentUltraLight = uniqolor(title, {
130119
saturation: [30, 35],
131120
lightness: [95, 96],
132121
}).color;
@@ -138,11 +127,8 @@ export const GET = async (req: NextRequest) => {
138127
display: 'flex',
139128
height: '100%',
140129
width: '100%',
141-
142130
background: `linear-gradient(37deg, ${bgAccent} 27.82%, ${bgAccentLight} 79.68%, ${bgAccentUltraLight} 100%)`,
143-
144131
fontFamily: 'Inter, Noto Sans, Inter, "Material Icons"',
145-
146132
padding: '80px',
147133
alignItems: 'flex-end',
148134
justifyContent: 'flex-end',
@@ -151,7 +137,6 @@ export const GET = async (req: NextRequest) => {
151137
<div
152138
style={{
153139
display: 'flex',
154-
155140
position: 'absolute',
156141
left: '5rem',
157142
top: '5rem',
@@ -160,23 +145,13 @@ export const GET = async (req: NextRequest) => {
160145
}}
161146
>
162147
<img
163-
src="/image/owner.jpg"
148+
src={imageData as unknown as any}
164149
style={{
165150
borderRadius: '50%',
166151
}}
167152
height={128}
168153
width={128}
169154
/>
170-
171-
<span
172-
style={{
173-
marginLeft: '3rem',
174-
color: '#ffffff99',
175-
fontSize: '2rem',
176-
}}
177-
>
178-
<h3>{seo.ogTitle}</h3>
179-
</span>
180155
</div>
181156
<div
182157
style={{
@@ -187,7 +162,7 @@ export const GET = async (req: NextRequest) => {
187162
textAlign: 'right',
188163
}}
189164
>
190-
<h1
165+
<h2
191166
style={{
192167
color: 'rgba(255, 255, 255, 0.92)',
193168
fontSize: '50px',
@@ -196,17 +171,28 @@ export const GET = async (req: NextRequest) => {
196171
fontWeight: 'bold',
197172
}}
198173
>
199-
{postName}
200-
</h1>
174+
{title}
175+
</h2>
201176
<h2
177+
style={{
178+
color: 'rgba(255, 255, 255, 0.92)',
179+
fontSize: '42px',
180+
overflow: 'hidden',
181+
maxHeight: '150px',
182+
fontWeight: 'bold',
183+
}}
184+
>
185+
{tag}
186+
</h2>
187+
<h3
202188
style={{
203189
color: 'rgba(255, 255, 255, 0.85)',
204190
fontSize: '38px',
205191
fontWeight: 'lighter',
206192
}}
207193
>
208194
{seo.word}
209-
</h2>
195+
</h3>
210196
</div>
211197
</div>
212198
),

src/core/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { symbolsTime, symbolsCount } from '@/lib/count';
55
import { getFirstGitCommitTime, getLastGitUpdateTime } from '@/lib/git';
66

77
export type PostItem = {
8+
authors: string[];
89
path: string;
910
rawFilePath: string;
1011
text: string;
@@ -20,6 +21,7 @@ export type PostItem = {
2021
};
2122

2223
export type PostJsonType = {
24+
authors: string[];
2325
path: string;
2426
title: string;
2527
tag: string;
@@ -42,6 +44,7 @@ export function buildPostData() {
4244
function processPostItem(item: PostJsonType) {
4345
const itemInfo = {} as PostItem;
4446
const file = importMarkdownFile(`./${item.path}`);
47+
itemInfo.authors = item.authors;
4548
itemInfo.title = item.title;
4649
itemInfo.tag = item.tag;
4750
itemInfo.path = item.path.replace('.md', '');

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"include": [
2828
"next-env.d.ts",
2929
"**/**/*.ts",
30-
"**/*.tsx",
30+
"**/**/*.tsx",
3131
"**/**/*.md",
3232
".next/types/**/*.ts",
3333
"commitlint.config.mjs",

0 commit comments

Comments
 (0)