From f6e627e0453919e11bcdfd2a2132092c6de83028 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 17 Dec 2023 18:57:24 -0500 Subject: [PATCH] refactor: use getServerSideProps --- src/app/page.tsx | 56 +++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/app/page.tsx b/src/app/page.tsx index 175ea83..3094a7b 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,27 +1,34 @@ -import { redirect } from "next/navigation" -import { headers } from "next/headers" -import Feed from "@/components/Feed" -import SearchBar from "@/components/SearchBar" -import { fetchFeedData } from "@/lib/feed.ts" -import * as GraphicSection from "@/components/GraphicSection" -import { ArrowIcon } from "@/components/ArrowIcon" +import Feed from "@/components/Feed"; +import SearchBar from "@/components/SearchBar"; +import { ApiResponse, fetchFeedData } from "@/lib/feed.ts"; +import * as GraphicSection from "@/components/GraphicSection"; +import { ArrowIcon } from "@/components/ArrowIcon"; +import { GetServerSideProps } from "next"; +import { Article } from "@/lib/prisma.ts"; type Props = { - searchParams?: { - q?: string | undefined - } -} + data: ApiResponse
; +}; -export default async function Home({ searchParams }: Props) { - const headerList = headers() - const referer = headerList.get("referer") - - if (!referer && searchParams?.q) { - redirect(`/search?q=${searchParams.q}`) +export const getServerSideProps: GetServerSideProps = async ({ + req, + params, +}) => { + if (!req.headers.referer && params?.q) { + return { + redirect: { + destination: `/search?q=${params?.q}`, + permanent: false, + }, + }; } - const data = await fetchFeedData({}) + const data = await fetchFeedData({}); + return { props: { data } }; +}; + +export default async function Home({ data }: Props) { return ( <> @@ -34,7 +41,10 @@ export default async function Home({ searchParams }: Props) { initialData={data.data} /> - +
- - Learn more about Lume and join our community - - + Learn more about Lume and join our community +
- ) + ); }