Skip to content

Commit

Permalink
Merge pull request #62 from ruchernchong/61-add-pages-revalidation
Browse files Browse the repository at this point in the history
61 add pages revalidation
  • Loading branch information
ruchernchong authored Jul 27, 2024
2 parents f92b518 + 0180328 commit 168703d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
14 changes: 14 additions & 0 deletions app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -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() });
};
10 changes: 7 additions & 3 deletions app/cars/[type]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ export const generateStaticParams = () =>
const CarsByFuelTypePage = async ({ params, searchParams }: Props) => {
const { type } = params;

const cars = await fetchApi<Car[]>(`${API_URL}/cars/${type}`);
const cars = await fetchApi<Car[]>(`${API_URL}/cars/${type}`, {
next: { tags: ["cars"] },
});
const [months, latestMonth] = await Promise.all([
fetchApi<Month[]>(`${API_URL}/months`),
fetchApi<LatestMonth>(`${API_URL}/months/latest`),
fetchApi<Month[]>(`${API_URL}/months`, { next: { tags: ["cars"] } }),
fetchApi<LatestMonth>(`${API_URL}/months/latest`, {
next: { tags: ["cars"] },
}),
]);

const totals = new Map();
Expand Down
9 changes: 7 additions & 2 deletions app/coe/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ import { UnreleasedFeature } from "@/components/UnreleasedFeature";
export const metadata: Metadata = { alternates: { canonical: "/coe" } };

const COEPage = async () => {
const fetchHistoricalResult = fetchApi<COEResult[]>(`${API_URL}/coe`);
const fetchMonthlyResult = fetchApi<COEResult[]>(`${API_URL}/coe/latest`);
const fetchHistoricalResult = fetchApi<COEResult[]>(`${API_URL}/coe`, {
next: { tags: ["coe"] },
});
const fetchMonthlyResult = fetchApi<COEResult[]>(`${API_URL}/coe/latest`, {
next: { tags: ["coe"] },
});
const fetchLatestMonth = fetchApi<Record<string, string>>(
`${API_URL}/months/latest`,
{ next: { tags: ["coe"] } },
);

const [historicalResults, monthlyResults, latestMonth] = await Promise.all([
Expand Down
1 change: 1 addition & 0 deletions app/coe/prices/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Typography from "@/components/Typography";
const COEPricesPage = async () => {
const historicalResult: COEResult[] = await fetchApi<COEResult[]>(
`${API_URL}/coe?orderBy=asc`,
{ next: { tags: ["coe"] } },
);

const groupedData: COEBiddingResult[] = historicalResult.reduce(
Expand Down
4 changes: 3 additions & 1 deletion app/make/[make]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const generateMetadata = async ({ params, searchParams }: Props) => {
};

export const generateStaticParams = async () => {
const makes = await fetchApi<string[]>(`${API_URL}/make`);
const makes = await fetchApi<string[]>(`${API_URL}/make`, {
next: { tags: ["cars"] },
});
return makes.map((make) => ({ make }));
};

Expand Down
6 changes: 1 addition & 5 deletions utils/fetchApi.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
interface Options extends RequestInit {}

export const fetchApi = async <T>(
url: string,
options: Options = {},
options: RequestInit = {},
): Promise<T> => {
options = { cache: "no-store", ...options };

const response = await fetch(url, options);

if (!response.ok) {
Expand Down

0 comments on commit 168703d

Please sign in to comment.