static data generation with cache

This commit is contained in:
Owen Rees
2023-06-13 20:36:48 +02:00
parent db266e6ef3
commit 112c257321
9 changed files with 117 additions and 118 deletions
+30
View File
@@ -0,0 +1,30 @@
import { Db } from "mongodb";
/**
* Converts date from string into a date to allow ordering
*/
export const dateOrderingFrance = async (db: Db) => {
return await db
.collection("tournaments")
.aggregate([
{
$addFields: {
dateParts: {
$dateFromString: {
dateString: "$date",
format: "%d/%m/%Y",
},
},
},
},
{
$sort: {
dateParts: 1,
},
},
{
$unset: "dateParts",
},
])
.toArray();
};
-15
View File
@@ -1,15 +0,0 @@
/**
* Retrieves tournament data from /api/tournaments/:country
* @remarks The result is cached for the revalidation period in seconds
*/
export default async function getTournaments(country: string) {
const server = process.env.NEXT_PUBLIC_SITE_URL;
const res = await fetch(`${server}/api/tournaments/${country}`, {
next: { revalidate: 300 },
});
if (res.status !== 200) {
throw new Error("Failed to fetch tournament data");
}
return await res.json();
}