Skip to content

Commit 0b643fd

Browse files
committed
mod: optimizing patch detail page metadata
1 parent 11b64f6 commit 0b643fd

File tree

17 files changed

+88
-531
lines changed

17 files changed

+88
-531
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"cSpell.words": [
33
"abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ",
44
"Alcot",
5+
"alttitle",
56
"anidb",
67
"atrule",
78
"autoincrement",
@@ -93,6 +94,7 @@
9394
"Nyaa",
9495
"nyne",
9596
"Nysoure",
97+
"olang",
9698
"onedrive",
9799
"opendal",
98100
"optimizelegibility",

app/patch/[id]/comment/metadata.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import { kunMoyuMoe } from '~/config/moyu-moe'
2+
import { getPreferredLanguageText } from '~/utils/getPreferredLanguageText'
23
import type { Metadata } from 'next'
34
import type { KunSiteAuthor } from '~/config/config'
4-
import type { Patch, PatchComment } from '~/types/api/patch'
5+
import type { PatchHeader, PatchComment } from '~/types/api/patch'
56

67
export const generateKunMetadataTemplate = (
7-
patch: Patch,
8+
patch: PatchHeader,
89
comments: PatchComment[]
910
): Metadata => {
11+
const patchName = getPreferredLanguageText(patch.name)
12+
const patchNameJa = patch.name['ja-jp'] ? patch.name['ja-jp'] : ''
13+
const pageTitle =
14+
patchNameJa && patchName !== patchNameJa
15+
? `${patchName} | ${patchNameJa} 的评论和评价`
16+
: `${patchName} 的评论和评价`
17+
1018
const authors: KunSiteAuthor[] = comments.map((com) => ({
1119
name: com.user.name,
1220
url: `${kunMoyuMoe.domain.main}/user/${com.user.id}/resource`
@@ -17,24 +25,20 @@ export const generateKunMetadataTemplate = (
1725
)
1826
const uniqueAuthorsName = uniqueAuthors.map((u) => u.name)
1927

28+
const pageDescription = comments.length
29+
? `${uniqueAuthorsName}${patchName} 下发布了 ${comments[0].content} 等评论, 查看更多`
30+
: `点击查看 ${patchName} 下的评论`
31+
2032
return {
21-
title: patch.alias.length
22-
? `${patch.name} | ${patch.alias[0]} 的 评论`
23-
: `${patch.name} 的 评论`,
33+
title: pageTitle,
2434
keywords: [...patch.alias, '评论'],
2535
authors: uniqueAuthors,
2636
creator: patch.user.name,
2737
publisher: patch.user.name,
28-
description: comments.length
29-
? `${uniqueAuthorsName}${patch.name} 下发布了 ${comments[0].content} 等评论, 查看更多`
30-
: `点击查看 ${patch.name} 下的评论`,
38+
description: pageDescription,
3139
openGraph: {
32-
title: patch.alias.length
33-
? `${patch.name} | ${patch.alias[0]} 的 评论`
34-
: `${patch.name} 的 评论`,
35-
description: comments.length
36-
? `${uniqueAuthorsName}${patch.name} 下发布了 ${comments[0].content} 等评论, 查看更多`
37-
: `点击查看 ${patch.name} 下的评论`,
40+
title: pageTitle,
41+
description: pageDescription,
3842
type: 'article',
3943
publishedTime: patch.created,
4044
modifiedTime: patch.updated,
@@ -43,18 +47,14 @@ export const generateKunMetadataTemplate = (
4347
url: patch.banner,
4448
width: 1920,
4549
height: 1080,
46-
alt: patch.name
50+
alt: patchName
4751
}
4852
]
4953
},
5054
twitter: {
5155
card: 'summary',
52-
title: patch.alias.length
53-
? `${patch.name} | ${patch.alias[0]} 的 评论`
54-
: `${patch.name} 的 评论`,
55-
description: comments.length
56-
? `${uniqueAuthorsName}${patch.name} 下发布了 ${comments[0].content} 等评论, 查看更多`
57-
: `点击查看 ${patch.name} 下的评论`,
56+
title: pageTitle,
57+
description: pageDescription,
5858
images: [patch.banner]
5959
},
6060
alternates: {

app/patch/[id]/history/actions.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

app/patch/[id]/history/metadata.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

app/patch/[id]/history/page.tsx

Lines changed: 0 additions & 73 deletions
This file was deleted.

app/patch/[id]/introduction/metadata.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ import type { PatchDetail } from '~/types/api/patch'
66

77
export const generateKunMetadataTemplate = (detail: PatchDetail): Metadata => {
88
const patchName = getPreferredLanguageText(detail.name)
9+
const patchNameJa = detail.name['ja-jp'] ? detail.name['ja-jp'] : ''
10+
const pageTitle =
11+
patchNameJa && patchName !== patchNameJa
12+
? `${patchName} | ${patchNameJa}`
13+
: `${patchName}`
914
const patchIntro = convert(getPreferredLanguageText(detail.introduction), {
1015
wordwrap: false,
1116
selectors: [{ selector: 'p', format: 'inline' }]
1217
}).slice(0, 170)
1318

1419
return {
20+
title: pageTitle,
1521
keywords: [patchName, ...detail.alias.map((a) => a.name)],
1622
description: patchIntro.slice(0, 170),
1723
openGraph: {
24+
title: pageTitle,
1825
description: patchIntro.slice(0, 170),
1926
type: 'article',
2027
images: [
@@ -28,6 +35,7 @@ export const generateKunMetadataTemplate = (detail: PatchDetail): Metadata => {
2835
},
2936
twitter: {
3037
card: 'summary',
38+
title: pageTitle,
3139
description: patchIntro.slice(0, 170),
3240
images: [detail.banner]
3341
},

app/patch/[id]/layout.tsx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,14 @@ import {
44
kunGetPatchIntroductionActions,
55
kunUpdatePatchViewsActions
66
} from './actions'
7-
import { generateKunMetadataTemplate } from './metadata'
87
import { PatchContainer } from '~/components/patch/Container'
98
import { verifyHeaderCookie } from '~/utils/actions/verifyHeaderCookie'
10-
import type { Metadata } from 'next'
119

1210
interface Props {
1311
children: React.ReactNode
1412
params: Promise<{ id: string }>
1513
}
1614

17-
export const generateMetadata = async ({
18-
params
19-
}: Props): Promise<Metadata> => {
20-
const { id } = await params
21-
const patch = await kunGetPatchActions({
22-
patchId: Number(id)
23-
})
24-
if (typeof patch === 'string') {
25-
return {}
26-
}
27-
return generateKunMetadataTemplate(patch)
28-
}
29-
3015
export default async function Kun({ params, children }: Props) {
3116
const { id } = await params
3217

app/patch/[id]/metadata.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

app/patch/[id]/pr/actions.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)