fix: invalid page number after changing the pagination size.

This commit is contained in:
Yufan Sheng 2025-01-08 16:05:34 +08:00
parent 2a4cb691e4
commit 7972966884
Signed by: syhily
GPG Key ID: 9D18A22A7DCD5A9B
5 changed files with 11 additions and 6 deletions
package.json
src/pages
cats/[slug]/page
page
search/[keyword]/page
tags/[slug]/page

View File

@ -89,7 +89,7 @@
}, },
"packageManager": "npm@11.0.0", "packageManager": "npm@11.0.0",
"engines": { "engines": {
"node": "22.12.0" "node": "22.13.0"
}, },
"overrides": { "overrides": {
"astro-uploader": { "astro-uploader": {

View File

@ -20,7 +20,7 @@ const pageSize = options.settings.pagination.category;
const total = Math.ceil(filteredPosts.length / pageSize); const total = Math.ceil(filteredPosts.length / pageSize);
if (pageNum > total) { if (pageNum > total) {
return Astro.redirect('/404'); return Astro.redirect(`${total}`, 302);
} }
--- ---

View File

@ -1,6 +1,7 @@
--- ---
import { posts, tags } from '@/helpers/schema'; import { posts, tags } from '@/helpers/schema';
import PostsLayout from '@/layouts/posts/PostsLayout.astro'; import PostsLayout from '@/layouts/posts/PostsLayout.astro';
import options from '@/options';
const { num } = Astro.params; const { num } = Astro.params;
@ -13,6 +14,11 @@ const pageNum = Number.parseInt(num);
if (pageNum <= 1) { if (pageNum <= 1) {
return Astro.redirect('/'); return Astro.redirect('/');
} }
const total = Math.ceil(posts.length / options.settings.pagination.posts);
if (pageNum > total) {
return Astro.redirect(`${total}`, 302);
}
--- ---
<PostsLayout {posts} {pageNum} {tags} /> <PostsLayout {posts} {pageNum} {tags} />

View File

@ -21,11 +21,10 @@ const searchResults = searchPosts(query)
.map((slug) => posts.find((post) => post.slug === slug)) .map((slug) => posts.find((post) => post.slug === slug))
.filter((post) => post !== undefined); .filter((post) => post !== undefined);
const pageSize = options.settings.pagination.tags; const total = Math.ceil(searchResults.length / options.settings.pagination.tags);
const total = Math.ceil(searchResults.length / pageSize);
if (pageNum > total) { if (pageNum > total) {
return Astro.redirect('/404'); return Astro.redirect(`${total}`, 302);
} }
--- ---

View File

@ -20,7 +20,7 @@ const pageSize = options.settings.pagination.tags;
const total = Math.ceil(filteredPosts.length / pageSize); const total = Math.ceil(filteredPosts.length / pageSize);
if (pageNum > total) { if (pageNum > total) {
return Astro.redirect('/404'); return Astro.redirect(`${total}`, 302);
} }
--- ---