From f3fba8e74f662356dad4276c70d4a3b06c1c3103 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Wed, 8 Jan 2025 19:10:57 +0800 Subject: [PATCH] fix: invalid page number after changing the pagination size. --- package-lock.json | 10 +++++----- package.json | 4 ++-- src/pages/cats/[slug]/page/[num].astro | 2 +- src/pages/page/[num].astro | 6 ++++++ src/pages/search/[keyword]/page/[num].astro | 5 ++--- src/pages/tags/[slug]/page/[num].astro | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index c91eab0..5f0021d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,7 +36,7 @@ "@types/pg": "^8.11.10", "@types/qrcode-svg": "^1.1.5", "@types/unist": "^3.0.3", - "astro-netease-player": "^1.0.1", + "astro-netease-player": "^1.0.2", "astro-uploader": "^1.2.3", "bootstrap": "^5.3.3", "photoswipe": "^5.4.4", @@ -55,7 +55,7 @@ "unist-util-select": "^5.1.0" }, "engines": { - "node": "22.12.0" + "node": "22.13.0" } }, "node_modules/@astrojs/check": { @@ -2596,9 +2596,9 @@ } }, "node_modules/astro-netease-player": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/astro-netease-player/-/astro-netease-player-1.0.1.tgz", - "integrity": "sha512-k+5erGNLHyy/GRKLqUOuKiN7d2HeX6KHzbPKEa/oL0tda4s09Eu4Zzt29P05bO/QNs5EGJbsz9UGiUGeVevVmA==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/astro-netease-player/-/astro-netease-player-1.0.2.tgz", + "integrity": "sha512-LIY747btm4iZUUjGQ/k/khUHnTyoFsGUdboIfLt3LTqIEs5cJctBPM945Ikjyg5inAw77QKqa8a9VyB58tOiUQ==", "dev": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 40a1613..02d8653 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "@types/pg": "^8.11.10", "@types/qrcode-svg": "^1.1.5", "@types/unist": "^3.0.3", - "astro-netease-player": "^1.0.1", + "astro-netease-player": "^1.0.2", "astro-uploader": "^1.2.3", "bootstrap": "^5.3.3", "photoswipe": "^5.4.4", @@ -89,7 +89,7 @@ }, "packageManager": "npm@11.0.0", "engines": { - "node": "22.12.0" + "node": "22.13.0" }, "overrides": { "astro-uploader": { diff --git a/src/pages/cats/[slug]/page/[num].astro b/src/pages/cats/[slug]/page/[num].astro index 2deca9e..1dd11d8 100644 --- a/src/pages/cats/[slug]/page/[num].astro +++ b/src/pages/cats/[slug]/page/[num].astro @@ -20,7 +20,7 @@ const pageSize = options.settings.pagination.category; const total = Math.ceil(filteredPosts.length / pageSize); if (pageNum > total) { - return Astro.redirect('/404'); + return Astro.redirect(`${total}`, 302); } --- diff --git a/src/pages/page/[num].astro b/src/pages/page/[num].astro index 5bc8d25..ce0e2b8 100644 --- a/src/pages/page/[num].astro +++ b/src/pages/page/[num].astro @@ -1,6 +1,7 @@ --- import { posts, tags } from '@/helpers/schema'; import PostsLayout from '@/layouts/posts/PostsLayout.astro'; +import options from '@/options'; const { num } = Astro.params; @@ -13,6 +14,11 @@ const pageNum = Number.parseInt(num); if (pageNum <= 1) { return Astro.redirect('/'); } + +const total = Math.ceil(posts.length / options.settings.pagination.posts); +if (pageNum > total) { + return Astro.redirect(`${total}`, 302); +} --- diff --git a/src/pages/search/[keyword]/page/[num].astro b/src/pages/search/[keyword]/page/[num].astro index 629ed6c..5ae1672 100644 --- a/src/pages/search/[keyword]/page/[num].astro +++ b/src/pages/search/[keyword]/page/[num].astro @@ -21,11 +21,10 @@ const searchResults = searchPosts(query) .map((slug) => posts.find((post) => post.slug === slug)) .filter((post) => post !== undefined); -const pageSize = options.settings.pagination.tags; -const total = Math.ceil(searchResults.length / pageSize); +const total = Math.ceil(searchResults.length / options.settings.pagination.tags); if (pageNum > total) { - return Astro.redirect('/404'); + return Astro.redirect(`${total}`, 302); } --- diff --git a/src/pages/tags/[slug]/page/[num].astro b/src/pages/tags/[slug]/page/[num].astro index 8e7cb84..33bafb6 100644 --- a/src/pages/tags/[slug]/page/[num].astro +++ b/src/pages/tags/[slug]/page/[num].astro @@ -20,7 +20,7 @@ const pageSize = options.settings.pagination.tags; const total = Math.ceil(filteredPosts.length / pageSize); if (pageNum > total) { - return Astro.redirect('/404'); + return Astro.redirect(`${total}`, 302); } ---