From 2bb010907b09009005f917def7f740e94e09fd1b Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Sun, 7 Jul 2024 21:38:19 +0800 Subject: [PATCH] fix: the 404 page for search result. --- src/components/page/post/PostCards.astro | 2 +- src/helpers/formatter.ts | 3 ++- src/layouts/posts/CategoryLayout.astro | 6 ++---- src/layouts/posts/SearchLayout.astro | 7 +------ src/layouts/posts/TagLayout.astro | 6 ++---- 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/components/page/post/PostCards.astro b/src/components/page/post/PostCards.astro index 60612b5..12cf050 100644 --- a/src/components/page/post/PostCards.astro +++ b/src/components/page/post/PostCards.astro @@ -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'); } diff --git a/src/helpers/formatter.ts b/src/helpers/formatter.ts index 3cce141..e07ff17 100644 --- a/src/helpers/formatter.ts +++ b/src/helpers/formatter.ts @@ -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) => { diff --git a/src/layouts/posts/CategoryLayout.astro b/src/layouts/posts/CategoryLayout.astro index e874ac4..661ded8 100644 --- a/src/layouts/posts/CategoryLayout.astro +++ b/src/layouts/posts/CategoryLayout.astro @@ -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; --- diff --git a/src/layouts/posts/SearchLayout.astro b/src/layouts/posts/SearchLayout.astro index 1ba0729..1899d6a 100644 --- a/src/layouts/posts/SearchLayout.astro +++ b/src/layouts/posts/SearchLayout.astro @@ -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); --- diff --git a/src/layouts/posts/TagLayout.astro b/src/layouts/posts/TagLayout.astro index 2b8ff36..f48c94b 100644 --- a/src/layouts/posts/TagLayout.astro +++ b/src/layouts/posts/TagLayout.astro @@ -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; ---