Skip to content

Commit 26de0d5

Browse files
committed
fix: 修正文章编辑和查看链接中的ID路径错误
在文章编辑页面中,将获取文章的API路径从 `/posts/${postId}` 修改为 `/posts/id/${postId}`。 在文章列表页面中,将文章链接的路径从 `/blog/${post.slug}` 修改为 `/blog/${post.id}`。 在生成文章元数据的函数中,将获取文章的参数从 `slug` 修改为 `id`,并更新相关的URL路径。 在生成网站地图的函数中,将文章链接的路径从 `/blog/${post.slug}` 修改为 `/blog/${post.id}`。 在 `BlogPost` 组件中,将属性和状态管理从 `slug` 修改为 `id`,并更新获取文章的API路径和错误信息输出。 Closes #123
1 parent 14de6da commit 26de0d5

6 files changed

Lines changed: 21 additions & 22 deletions

File tree

frontend/app/(dashboard)/dashboard/posts/[id]/edit/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default function EditPostPage() {
1818
useEffect(() => {
1919
const fetchPost = async () => {
2020
try {
21-
const data = await http.get<PostDetail>(`/posts/${postId}`);
21+
const data = await http.get<PostDetail>(`/posts/id/${postId}`);
2222
if (data) {
2323
setPost(data);
2424
} else {

frontend/app/(dashboard)/dashboard/posts/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default function PostsPage() {
125125
<td>
126126
<div className="flex gap-2">
127127
<Link
128-
href={`/blog/${post.slug}`}
128+
href={`/blog/${post.id}`}
129129
target="_blank"
130130
className="btn btn-ghost btn-sm"
131131
title="查看"
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ function getApiBaseUrl(): string {
66
return process.env.INTERNAL_API_BASE_URL || "http://localhost:8080/api";
77
}
88

9-
async function getPost(slug: string): Promise<PostDetail | null> {
9+
async function getPost(id: string): Promise<PostDetail | null> {
1010
try {
11-
const response = await fetch(`${getApiBaseUrl()}/posts/${slug}`, {
11+
const response = await fetch(`${getApiBaseUrl()}/posts/id/${id}`, {
1212
next: { revalidate: 60 },
1313
});
1414

1515
if (response.ok) {
1616
const body = await response.json();
17-
// 解包 ApiResponse
1817
return body?.data || body;
1918
}
2019
} catch (error) {
@@ -27,10 +26,10 @@ async function getPost(slug: string): Promise<PostDetail | null> {
2726
export async function generateMetadata({
2827
params,
2928
}: {
30-
params: Promise<{ slug: string }>;
29+
params: Promise<{ id: string }>;
3130
}): Promise<Metadata> {
32-
const { slug } = await params;
33-
const post = await getPost(slug);
31+
const { id } = await params;
32+
const post = await getPost(id);
3433

3534
if (post) {
3635
const coverImage = post.cover_images && post.cover_images.length > 0 ? post.cover_images[0] : null;
@@ -47,7 +46,7 @@ export async function generateMetadata({
4746
authors: ["ExquisiteCore"],
4847
tags: post.labels || [],
4948
images: coverImage ? [coverImage] : [],
50-
url: `https://blog.exquisitecore.xyz/blog/${post.slug}`,
49+
url: `https://blog.exquisitecore.xyz/blog/${post.id}`,
5150
},
5251
twitter: {
5352
card: "summary_large_image",
@@ -56,7 +55,7 @@ export async function generateMetadata({
5655
images: coverImage ? [coverImage] : [],
5756
},
5857
alternates: {
59-
canonical: `https://blog.exquisitecore.xyz/blog/${post.slug}`,
58+
canonical: `https://blog.exquisitecore.xyz/blog/${post.id}`,
6059
},
6160
};
6261
}
@@ -93,7 +92,7 @@ function generateArticleJsonLd(post: PostDetail) {
9392
},
9493
mainEntityOfPage: {
9594
"@type": "WebPage",
96-
"@id": `https://blog.exquisitecore.xyz/blog/${post.slug}`,
95+
"@id": `https://blog.exquisitecore.xyz/blog/${post.id}`,
9796
},
9897
keywords: post.labels?.join(", ") || "",
9998
articleSection: "技术博客",
@@ -104,10 +103,10 @@ function generateArticleJsonLd(post: PostDetail) {
104103
export default async function BlogPostPage({
105104
params,
106105
}: {
107-
params: Promise<{ slug: string }>;
106+
params: Promise<{ id: string }>;
108107
}) {
109-
const { slug } = await params;
110-
const post = await getPost(slug);
108+
const { id } = await params;
109+
const post = await getPost(id);
111110

112111
return (
113112
<>
@@ -119,7 +118,7 @@ export default async function BlogPostPage({
119118
}}
120119
/>
121120
)}
122-
<BlogPost slug={slug} />
121+
<BlogPost id={id} />
123122
</>
124123
);
125124
}

frontend/app/sitemap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
7171

7272
const posts = await getBlogPosts();
7373
const blogPages: MetadataRoute.Sitemap = posts.map((post) => ({
74-
url: `${baseUrl}/blog/${post.slug}`,
74+
url: `${baseUrl}/blog/${post.id}`,
7575
lastModified: new Date(post.updated_at || post.published_at || post.created_at),
7676
changeFrequency: 'weekly',
7777
priority: 0.7,

frontend/components/BlogPost.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ function formatDate(dateString: string) {
1515
}
1616

1717
interface BlogPostProps {
18-
slug: string;
18+
id: string;
1919
}
2020

21-
export default function BlogPost({ slug }: BlogPostProps) {
21+
export default function BlogPost({ id }: BlogPostProps) {
2222
const [post, setPost] = useState<PostDetail | null>(null);
2323
const [loading, setLoading] = useState(true);
2424
const [error, setError] = useState<string | null>(null);
@@ -27,7 +27,7 @@ export default function BlogPost({ slug }: BlogPostProps) {
2727
async function fetchPost() {
2828
try {
2929
setLoading(true);
30-
const response = await http.get<PostDetail>(`/posts/${slug}`, undefined, {
30+
const response = await http.get<PostDetail>(`/posts/id/${id}`, undefined, {
3131
withToken: false,
3232
});
3333

@@ -38,15 +38,15 @@ export default function BlogPost({ slug }: BlogPostProps) {
3838
setError('文章不存在');
3939
}
4040
} catch (err) {
41-
console.error(`获取文章 ${slug} 失败:`, err);
41+
console.error(`获取文章 ${id} 失败:`, err);
4242
setError('获取文章失败');
4343
} finally {
4444
setLoading(false);
4545
}
4646
}
4747

4848
fetchPost();
49-
}, [slug]);
49+
}, [id]);
5050

5151
if (loading) {
5252
return (

frontend/components/BlogPosts.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default function BlogPosts() {
9191
{posts.map((post) => (
9292
<Link
9393
key={post.id}
94-
href={`/blog/${post.slug}`}
94+
href={`/blog/${post.id}`}
9595
className="card bg-base-100 shadow-xl hover:shadow-2xl transition-all hover:-translate-y-1"
9696
>
9797
{post.cover_images && post.cover_images.length > 0 && (

0 commit comments

Comments
 (0)