From 9fd25138e4609851e8935a8b560ed1af63d02dab Mon Sep 17 00:00:00 2001 From: Richard Bergsma Date: Fri, 6 Jun 2025 23:57:17 +0200 Subject: [PATCH] Refactor commit fetching in development.astro to implement caching for improved performance - Introduce a caching mechanism for Gitea commits to reduce API calls across language builds. - Update the commit fetching logic to handle errors more gracefully and ensure consistent data retrieval. --- src/pages/[lang]/development.astro | 45 +++++++++++++++--------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/pages/[lang]/development.astro b/src/pages/[lang]/development.astro index bb0a508..4b1db2d 100644 --- a/src/pages/[lang]/development.astro +++ b/src/pages/[lang]/development.astro @@ -16,32 +16,31 @@ const metadata = { const lang = Astro.params.lang || 'en'; const t = getTranslation(lang); -// Fetch Git repository data at build time -const GITEA_COMMITS_URL = 'https://git.365devnet.eu/api/v1/repos/365DevNet/365devnet/commits?sha=main&stat=true&verification=true&files=true'; - -// Fetch commits data -let commits: any[] = []; -try { - const headers: Record = { - 'accept': 'application/json', - }; - // Use Authorization header if token is present - if (import.meta.env.GITEA_TOKEN) { - headers['Authorization'] = `token ${import.meta.env.GITEA_TOKEN}`; +// Cache Gitea commits across all language builds +let cachedCommits = null; +async function getCachedCommits() { + if (cachedCommits) return cachedCommits; + const GITEA_COMMITS_URL = 'https://git.365devnet.eu/api/v1/repos/365DevNet/365devnet/commits?sha=main&stat=true&verification=true&files=true'; + let commits = []; + try { + const headers = { accept: 'application/json' }; + if (import.meta.env.GITEA_TOKEN) { + headers['Authorization'] = `token ${import.meta.env.GITEA_TOKEN}`; + } + const commitsResponse = await fetch(GITEA_COMMITS_URL, { headers }); + if (commitsResponse.ok) { + commits = await commitsResponse.json(); + } + } catch (error) { + console.error('Error fetching commits:', error); } - const commitsResponse = await fetch(GITEA_COMMITS_URL, { - headers, - }); - - if (commitsResponse.ok) { - commits = await commitsResponse.json(); - } else { - console.error('Failed to fetch commits:', commitsResponse.statusText); - } -} catch (error) { - console.error('Error fetching commits:', error); + cachedCommits = commits; + return commits; } +// Fetch Git repository data at build time (cached) +const commits = await getCachedCommits(); + // Format date to a readable format const formatDate = (dateString: string) => { const date = new Date(dateString);