diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml new file mode 100644 index 0000000..3c8f27c --- /dev/null +++ b/.github/workflows/CI.yaml @@ -0,0 +1,37 @@ +name: Node.js CI + +on: + push: + branches: + - main + pull_request: + branches: + - main + - dev + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + fail-fast: true + matrix: + node: [18, 20] + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Setup node + uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node }} + + - name: Install dependencies + run: npm ci + + - name: Run build + run: npm run build + env: + NEXT_PUBLIC_API_URL: ${{ secrets.NEXT_PUBLIC_API_URL}} + NEXT_PUBLIC_KAKAO_API_KEY: ${{ secrets.NEXT_PUBLIC_KAKAO_API_KEY }} diff --git a/src/app/action/invalidateHomeData.ts b/src/app/action/invalidateHomeData.ts new file mode 100644 index 0000000..5beccfe --- /dev/null +++ b/src/app/action/invalidateHomeData.ts @@ -0,0 +1,7 @@ +"use server"; + +import { revalidateTag } from "next/cache"; + +export const InvalidateHomeData = async () => { + revalidateTag("homeData"); +}; diff --git a/src/app/components/Navbar.tsx b/src/app/components/Navbar.tsx index 9f80635..aef5809 100644 --- a/src/app/components/Navbar.tsx +++ b/src/app/components/Navbar.tsx @@ -2,17 +2,41 @@ import Image from "next/image"; import { useRouter } from "next/navigation"; - -import Logo from "/public/Logo.png"; -import BookIcon from "/public/icons/Books.png"; +import { useEffect } from "react"; import { useSearchStore } from "@/stores/searchStore"; import SearchBar from "./SearchBar"; +import { InvalidateHomeData } from "../action/invalidateHomeData"; + +import Logo from "/public/Logo.png"; +import BookIcon from "/public/icons/Books.png"; export default function Navbar() { const router = useRouter(); const { resetSearchState } = useSearchStore(); + useEffect(() => { + revalidateData(); + }, []); + + const revalidateData = async () => { + const currentDate = new Date().getDate(); + + if (localStorage.getItem("date") !== currentDate.toString()) { + await InvalidateHomeData(); + localStorage.setItem("date", currentDate.toString()); + } + }; + + const handleHomeClick = async () => { + if (window.location.pathname === "/") { + return; + } + await revalidateData(); + resetSearchState(); + router.push("/"); + }; + return (
{ - resetSearchState(); - router.push("/"); - }} + onClick={handleHomeClick} />

오늘의 주제

-

{topics[randomIndex]}

+

{topics[index]}

오늘의 작가

-

{authors[randomIndex]}

+

{authors[index]}

diff --git a/src/libs/apis/todayAuthors.ts b/src/libs/apis/todayAuthors.ts index 36d540c..70babd1 100644 --- a/src/libs/apis/todayAuthors.ts +++ b/src/libs/apis/todayAuthors.ts @@ -1,10 +1,7 @@ import authors from "@/data/authors"; -export const todayAuthors = async ( - randomIndex: number, - revalidationTime: number, -) => { - const query = authors[randomIndex]; +export const todayAuthors = async (index: number) => { + const query = authors[index]; const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL}v3/search/book?query=${query}&target=person`, { @@ -13,7 +10,7 @@ export const todayAuthors = async ( "Content-Type": "application/json", Authorization: `KakaoAK ${process.env.NEXT_PUBLIC_KAKAO_API_KEY}`, }, - next: { revalidate: revalidationTime }, + next: { tags: ["homeData"] }, }, ); diff --git a/src/libs/apis/todayTopic.ts b/src/libs/apis/todayTopic.ts index 9044d41..b6a2fc8 100644 --- a/src/libs/apis/todayTopic.ts +++ b/src/libs/apis/todayTopic.ts @@ -1,10 +1,7 @@ import topics from "@/data/topics"; -export const todayTopic = async ( - randomIndex: number, - revalidationTime: number, -) => { - const query = topics[randomIndex]; +export const todayTopic = async (index: number) => { + const query = topics[index]; const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL}v3/search/book?query=${query}`, { @@ -13,7 +10,7 @@ export const todayTopic = async ( "Content-Type": "application/json", Authorization: `KakaoAK ${process.env.NEXT_PUBLIC_KAKAO_API_KEY}`, }, - next: { revalidate: revalidationTime }, + next: { tags: ["homeData"] }, }, ); diff --git a/src/libs/isr/GetIndex.ts b/src/libs/isr/GetIndex.ts new file mode 100644 index 0000000..87d902d --- /dev/null +++ b/src/libs/isr/GetIndex.ts @@ -0,0 +1,9 @@ +export default function GetIndex() { + const currentDate = new Date().getDate(); + + if (currentDate === 31) { + return 7; + } else { + return currentDate % 15; + } +} diff --git a/src/libs/isr/GetRevalidationTime.ts b/src/libs/isr/GetRevalidationTime.ts deleted file mode 100644 index 8732b83..0000000 --- a/src/libs/isr/GetRevalidationTime.ts +++ /dev/null @@ -1,7 +0,0 @@ -export default function GetRevalidationTime() { - const now = new Date(); - const setTime = new Date(); - setTime.setUTCHours(15, 0, 0); - - return (Date.parse(setTime.toString()) - Date.parse(now.toString())) / 1000; -}