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:
@@ -16,32 +16,31 @@ const metadata = {
|
|||||||
const lang = Astro.params.lang || 'en';
|
const lang = Astro.params.lang || 'en';
|
||||||
const t = getTranslation(lang);
|
const t = getTranslation(lang);
|
||||||
|
|
||||||
// Fetch Git repository data at build time
|
// Cache Gitea commits across all language builds
|
||||||
const GITEA_COMMITS_URL = 'https://git.365devnet.eu/api/v1/repos/365DevNet/365devnet/commits?sha=main&stat=true&verification=true&files=true';
|
let cachedCommits = null;
|
||||||
|
async function getCachedCommits() {
|
||||||
// Fetch commits data
|
if (cachedCommits) return cachedCommits;
|
||||||
let commits: any[] = [];
|
const GITEA_COMMITS_URL = 'https://git.365devnet.eu/api/v1/repos/365DevNet/365devnet/commits?sha=main&stat=true&verification=true&files=true';
|
||||||
try {
|
let commits = [];
|
||||||
const headers: Record<string, string> = {
|
try {
|
||||||
'accept': 'application/json',
|
const headers = { accept: 'application/json' };
|
||||||
};
|
|
||||||
// Use Authorization header if token is present
|
|
||||||
if (import.meta.env.GITEA_TOKEN) {
|
if (import.meta.env.GITEA_TOKEN) {
|
||||||
headers['Authorization'] = `token ${import.meta.env.GITEA_TOKEN}`;
|
headers['Authorization'] = `token ${import.meta.env.GITEA_TOKEN}`;
|
||||||
}
|
}
|
||||||
const commitsResponse = await fetch(GITEA_COMMITS_URL, {
|
const commitsResponse = await fetch(GITEA_COMMITS_URL, { headers });
|
||||||
headers,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (commitsResponse.ok) {
|
if (commitsResponse.ok) {
|
||||||
commits = await commitsResponse.json();
|
commits = await commitsResponse.json();
|
||||||
} else {
|
|
||||||
console.error('Failed to fetch commits:', commitsResponse.statusText);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching commits:', 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
|
// Format date to a readable format
|
||||||
const formatDate = (dateString: string) => {
|
const formatDate = (dateString: string) => {
|
||||||
const date = new Date(dateString);
|
const date = new Date(dateString);
|
||||||
|
Reference in New Issue
Block a user