From 643deed0565a6d177f1f343b820f9d1342b738e0 Mon Sep 17 00:00:00 2001 From: Azvya Erstevan Date: Wed, 21 Aug 2024 18:58:58 +0700 Subject: [PATCH] Infinite redirection fixed --- src/app/components.tsx | 9 ++++++++- src/app/stories/sections.tsx | 10 +++++----- src/components/footer/footer.tsx | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/app/components.tsx b/src/app/components.tsx index 338a309..df24c68 100644 --- a/src/app/components.tsx +++ b/src/app/components.tsx @@ -214,7 +214,14 @@ function MostHighlightedStory({ story }: { story?: StoriesIndexEntry }) { function LatestStories({ stories }: { stories: StoriesIndexEntry[] }) { return (
-

Latest Stories

+ {stories.length > 0 && ( +

Latest Stories

+ )} + {stories.length === 0 && ( +

+ No published stories yet +

+ )}
{stories.map((index, k) => ( diff --git a/src/app/stories/sections.tsx b/src/app/stories/sections.tsx index 571819b..04d31e4 100644 --- a/src/app/stories/sections.tsx +++ b/src/app/stories/sections.tsx @@ -7,7 +7,7 @@ import { readFileSync } from 'fs'; import jsonata from 'jsonata'; import Image from 'next/image'; import Link from 'next/link'; -import { notFound, redirect } from 'next/navigation'; +import { redirect } from 'next/navigation'; import path from 'path'; import { cache } from 'react'; import { FiChevronLeft, FiChevronRight } from 'react-icons/fi'; @@ -53,17 +53,17 @@ async function listStories( } const expression = jsonata(query); const result: StoriesIndexEntry[] = await expression.evaluate(indexStories); - + const maxPage = Math.ceil(result.length / 6); return { total: query === `$[]` ? meta.totalStories : result.length, result: result.slice((page - 1) * 6, 6 * page) ?? [], - maxPage: Math.ceil(result.length / 6), + maxPage: maxPage === 0 ? 1 : maxPage, }; } catch (error) { return { total: 0, result: [], - maxPage: 0, + maxPage: 1, }; } } @@ -105,7 +105,7 @@ async function FilteringSection({ }) { const meta = await checkMeta(); if (category && !meta.categories.includes(category)) { - return notFound(); + return redirect('/stories'); } return (
diff --git a/src/components/footer/footer.tsx b/src/components/footer/footer.tsx index 757c84c..4dd1dd7 100644 --- a/src/components/footer/footer.tsx +++ b/src/components/footer/footer.tsx @@ -5,7 +5,7 @@ import Link from 'next/link'; function Footer() { return ( -