From f1938cb22f245bbc4bcd878e1aaa2aaca9f6988f Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 26 Feb 2023 21:06:38 -0600 Subject: [PATCH] Cached file will be used if it is not older than 1 hour --- src/pages/api/supporters.tsx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/pages/api/supporters.tsx b/src/pages/api/supporters.tsx index 5e4d687..2e67c49 100644 --- a/src/pages/api/supporters.tsx +++ b/src/pages/api/supporters.tsx @@ -10,6 +10,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) console.error('BMC_TOKEN or BMC_WEBHOOK_SECRET is not defined') throw new Error('BMC_TOKEN or BMC_WEBHOOK_SECRET is not defined') } + const cacheFilePath = 'supporters.json' + + // Check if the response has been cached and is not older than 1 hour + if (fs.existsSync(cacheFilePath) && cacheFilePath && Date.now() - fs.statSync(cacheFilePath).mtimeMs < 3600000) { + console.log('Returning cached supporters JSON data.') + const supportersJson = fs.readFileSync(cacheFilePath, 'utf8') + res.send(JSON.parse(supportersJson) as any) + return + } fetch(`https://developers.buymeacoffee.com/api/v1/supporters/?slug=sudoalex`, { headers: { Authorization: `Bearer ${TOKEN}`,