fix: the 404 page for search result.

This commit is contained in:
Yufan Sheng 2024-07-07 21:38:19 +08:00
parent c7a4443513
commit 2bb010907b
Signed by: syhily
GPG Key ID: 9D18A22A7DCD5A9B
5 changed files with 8 additions and 16 deletions

View File

@ -13,7 +13,7 @@ interface Props {
const { pageNum, posts } = Astro.props;
const results = slicePosts(posts, pageNum, options.settings.pagination.posts);
if (!results) {
if (results.totalPage === 0) {
return Astro.redirect('/404');
}

View File

@ -6,7 +6,7 @@ export const slicePosts = (
posts: Post[],
pageNum: number,
pageSize: number,
): { currentPosts: Post[]; totalPage: number } | undefined => {
): { currentPosts: Post[]; totalPage: number } => {
const totalPage = Math.ceil(posts.length / pageSize);
if (totalPage >= pageNum) {
return {
@ -17,6 +17,7 @@ export const slicePosts = (
totalPage,
};
}
return { currentPosts: [], totalPage: 0 };
};
export const formatShowDate = (date: Date) => {

View File

@ -13,12 +13,10 @@ interface Props {
}
const { category, posts, pageNum } = Astro.props;
const results = slicePosts(posts, pageNum, options.settings.pagination.category);
if (!results) {
const { currentPosts, totalPage } = slicePosts(posts, pageNum, options.settings.pagination.category);
if (totalPage === 0) {
return Astro.redirect('/404');
}
const { currentPosts, totalPage } = results;
---
<BaseLayout title={category.name}>

View File

@ -20,12 +20,7 @@ if (pageNum < 1) {
return Astro.redirect('/');
}
const results = slicePosts(posts, pageNum, options.settings.pagination.search);
if (!results) {
return Astro.redirect('/404');
}
const { currentPosts, totalPage } = results;
const { currentPosts, totalPage } = slicePosts(posts, pageNum, options.settings.pagination.search);
---
<BaseLayout {title}>

View File

@ -13,12 +13,10 @@ interface Props {
}
const { tag, posts, pageNum } = Astro.props;
const results = slicePosts(posts, pageNum, options.settings.pagination.tags);
if (!results) {
const { currentPosts, totalPage } = slicePosts(posts, pageNum, options.settings.pagination.tags);
if (totalPage === 0) {
return Astro.redirect('/404');
}
const { currentPosts, totalPage } = results;
---
<BaseLayout title={`标签 “${tag.name}”`}>