Skip to content

Commit

Permalink
Update API to add revalidation tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Ru Chern Chong committed Jul 27, 2024
1 parent 832e586 commit 0180328
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
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 0180328

Please sign in to comment.