From 832e58673608d7b6006ad12ccde49499edd65481 Mon Sep 17 00:00:00 2001 From: Ru Chern Chong Date: Sat, 27 Jul 2024 22:18:33 +0800 Subject: [PATCH 1/2] Add on-demand revalidation API --- .env.example | 1 + app/api/revalidate/route.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 app/api/revalidate/route.ts diff --git a/.env.example b/.env.example index 6444b1f..bfc8d19 100644 --- a/.env.example +++ b/.env.example @@ -2,3 +2,4 @@ NEXT_PUBLIC_SITE_URL= NEXT_PUBLIC_API_URL= NEXT_PUBLIC_GA_MEASUREMENT_ID= NEXT_PUBLIC_FEATURE_FLAG_RELEASED= +NEXT_PUBLIC_REVALIDATE_TOKEN= diff --git a/app/api/revalidate/route.ts b/app/api/revalidate/route.ts new file mode 100644 index 0000000..62e7fd7 --- /dev/null +++ b/app/api/revalidate/route.ts @@ -0,0 +1,14 @@ +import { NextRequest, NextResponse } from "next/server"; +import { revalidateTag } from "next/cache"; + +export const GET = (req: NextRequest) => { + const secret = req.nextUrl.searchParams.get("secret"); + const tags = req.nextUrl.searchParams.get("tags") as string; + + if (secret !== process.env.NEXT_PUBLIC_REVALIDATE_TOKEN) { + return NextResponse.json({ message: "Invalid token!" }, { status: 401 }); + } + + revalidateTag(tags); + return NextResponse.json({ revalidated: true, now: Date.now() }); +}; From 01803281772cc0a380ec649e68dadbbbaa4c6bb1 Mon Sep 17 00:00:00 2001 From: Ru Chern Chong Date: Sat, 27 Jul 2024 22:22:17 +0800 Subject: [PATCH 2/2] Update API to add revalidation tags --- app/cars/[type]/page.tsx | 10 +++++++--- app/coe/page.tsx | 9 +++++++-- app/coe/prices/page.tsx | 1 + app/make/[make]/page.tsx | 4 +++- utils/fetchApi.ts | 6 +----- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/cars/[type]/page.tsx b/app/cars/[type]/page.tsx index 45a2709..9fdc59c 100644 --- a/app/cars/[type]/page.tsx +++ b/app/cars/[type]/page.tsx @@ -56,10 +56,14 @@ export const generateStaticParams = () => const CarsByFuelTypePage = async ({ params, searchParams }: Props) => { const { type } = params; - const cars = await fetchApi(`${API_URL}/cars/${type}`); + const cars = await fetchApi(`${API_URL}/cars/${type}`, { + next: { tags: ["cars"] }, + }); const [months, latestMonth] = await Promise.all([ - fetchApi(`${API_URL}/months`), - fetchApi(`${API_URL}/months/latest`), + fetchApi(`${API_URL}/months`, { next: { tags: ["cars"] } }), + fetchApi(`${API_URL}/months/latest`, { + next: { tags: ["cars"] }, + }), ]); const totals = new Map(); diff --git a/app/coe/page.tsx b/app/coe/page.tsx index d57de17..4a0a3d4 100644 --- a/app/coe/page.tsx +++ b/app/coe/page.tsx @@ -18,10 +18,15 @@ import { UnreleasedFeature } from "@/components/UnreleasedFeature"; export const metadata: Metadata = { alternates: { canonical: "/coe" } }; const COEPage = async () => { - const fetchHistoricalResult = fetchApi(`${API_URL}/coe`); - const fetchMonthlyResult = fetchApi(`${API_URL}/coe/latest`); + const fetchHistoricalResult = fetchApi(`${API_URL}/coe`, { + next: { tags: ["coe"] }, + }); + const fetchMonthlyResult = fetchApi(`${API_URL}/coe/latest`, { + next: { tags: ["coe"] }, + }); const fetchLatestMonth = fetchApi>( `${API_URL}/months/latest`, + { next: { tags: ["coe"] } }, ); const [historicalResults, monthlyResults, latestMonth] = await Promise.all([ diff --git a/app/coe/prices/page.tsx b/app/coe/prices/page.tsx index 17fe0b9..7c54fe4 100644 --- a/app/coe/prices/page.tsx +++ b/app/coe/prices/page.tsx @@ -8,6 +8,7 @@ import Typography from "@/components/Typography"; const COEPricesPage = async () => { const historicalResult: COEResult[] = await fetchApi( `${API_URL}/coe?orderBy=asc`, + { next: { tags: ["coe"] } }, ); const groupedData: COEBiddingResult[] = historicalResult.reduce( diff --git a/app/make/[make]/page.tsx b/app/make/[make]/page.tsx index f642f50..6968677 100644 --- a/app/make/[make]/page.tsx +++ b/app/make/[make]/page.tsx @@ -35,7 +35,9 @@ export const generateMetadata = async ({ params, searchParams }: Props) => { }; export const generateStaticParams = async () => { - const makes = await fetchApi(`${API_URL}/make`); + const makes = await fetchApi(`${API_URL}/make`, { + next: { tags: ["cars"] }, + }); return makes.map((make) => ({ make })); }; diff --git a/utils/fetchApi.ts b/utils/fetchApi.ts index b61aa4e..6ada87a 100644 --- a/utils/fetchApi.ts +++ b/utils/fetchApi.ts @@ -1,11 +1,7 @@ -interface Options extends RequestInit {} - export const fetchApi = async ( url: string, - options: Options = {}, + options: RequestInit = {}, ): Promise => { - options = { cache: "no-store", ...options }; - const response = await fetch(url, options); if (!response.ok) {