My personal portfolio and blog (mirror)
www.matthew-hre.com/
portfolio
blog
1const githubApiUrl =
2 "https://api.github.com/repos/matthew-hre/matthew-hre/commits";
3
4let cachedCommitData: CommitData | null = null;
5let cacheTimestamp: number | null = null;
6const cacheTTL = 5 * 60 * 1000; // cache time-to-live: 5 minutes
7
8export async function getCommitData(): Promise<CommitData | null> {
9 try {
10 const now = Date.now();
11
12 if (cachedCommitData && cacheTimestamp && now - cacheTimestamp < cacheTTL) {
13 return cachedCommitData;
14 }
15
16 const response = await fetch(githubApiUrl);
17
18 if (!response.ok) {
19 throw new Error(`Failed to fetch data. Status: ${response.status}`);
20 }
21
22 const commitData = await response.json();
23
24 if (!commitData || commitData.length === 0) {
25 return null;
26 }
27
28 const latestCommit = commitData[0];
29 const commitSha = latestCommit.sha;
30 const commitMessage = latestCommit.commit.message;
31
32 const commitTime = new Date(latestCommit.commit.author.date).toLocaleString(
33 "en-US",
34 { timeZone: "MST", hour12: false }
35 );
36
37 cachedCommitData = {
38 sha: commitSha,
39 time: commitTime.replace(", ", " at "),
40 message: commitMessage,
41 };
42 cacheTimestamp = now;
43
44 return cachedCommitData;
45 } catch (error) {
46 console.error("Error fetching commit data:", error);
47 throw error;
48 }
49}
50
51interface CommitData {
52 sha: string;
53 time: string;
54 message: string;
55}