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.
This commit is contained in:
2025-06-06 23:57:17 +02:00
parent 89d9de5403
commit 9fd25138e4

View File

@@ -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<string, string> = {
'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);