Skip to content

Commit

Permalink
Add error handling for invalid page numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
priyanshuxkumar committed Jan 21, 2025
1 parent af0e6a1 commit 1873828
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pages/blog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ export default function BlogIndexPage() {
useEffect(() => {
const pageFromQuery = parseInt(router.query.page as string, 10);

if (!Number.isNaN(pageFromQuery) && pageFromQuery >= 1 && pageFromQuery !== currentPage) {
setCurrentPage(pageFromQuery);
if (!Number.isNaN(pageFromQuery) && maxPage > 0) {
if (pageFromQuery >= 1 && pageFromQuery <= maxPage && pageFromQuery !== currentPage) {
setCurrentPage(pageFromQuery);
} else if (pageFromQuery < 1 || pageFromQuery > maxPage) {
// Only reset to page 1 if the page number is actually invalid
handlePageChange(1);
}
}
}, [router.query.page]);
}, [router.query.page, maxPage, currentPage]);

const [isClient, setIsClient] = useState(false);

Expand Down

0 comments on commit 1873828

Please sign in to comment.