diff --git a/frontend/app/routes/books.tsx b/frontend/app/routes/books.tsx
index 0a9f4d4..3cfcc47 100644
--- a/frontend/app/routes/books.tsx
+++ b/frontend/app/routes/books.tsx
@@ -6,7 +6,7 @@ import { SortSelect } from "~/components/books/SortSelect/SortSelect";
import { PageBreadcrumb } from "~/components/PageBreadcrumb/PageBreadcrumb";
import { useBookSearchParams } from "~/hooks/useBookSearchParams";
import { useSuggestedBooks } from "~/hooks/useSuggestedBooks";
-import { useSearchParams } from "react-router";
+import { Navigate, useSearchParams } from "react-router";
import { useDataIsDelayed } from "~/hooks/useDataIsDelayed";
import type { ReactNode } from "react";
import { BookFilters } from "~/components/books/BookFilters/BookFilters";
@@ -128,9 +128,16 @@ export default function BooksPage() {
const suggestionParams = new URLSearchParams(searchParams);
suggestionParams.set("origin", "suggestions");
+ // Redirect to page 1, keeping the search query, filters and sort
+ function redirectToFirstPage() {
+ const firstPageParams = new URLSearchParams(searchParams);
+ firstPageParams.delete("page");
+ return ;
+ }
+
// Later loads keep showing the previous books until the new ones arrive,
// unless it takes a while
- if ((loading && !books) || slowLoading) {
+ if ((loading && !books?.length) || slowLoading) {
return (
@@ -152,7 +159,17 @@ export default function BooksPage() {
);
}
+ // The page is beyond the last page of the search results
+ if (page > 1 && totalCount > 0 && books?.length === 0) {
+ return redirectToFirstPage();
+ }
+
if (!books || books.length === 0) {
+ // The page is beyond the last page of the suggestions
+ if (page > 1 && suggestions?.length === 0 && suggestionCount > 0) {
+ return redirectToFirstPage();
+ }
+
if (suggestions && suggestions.length > 0) {
return (