fix: the 404 page for search result.

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

View File

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

View File

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

View File

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

View File

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

View File

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