Skip to content

Commit f4c3e4d

Browse files
committed
feat: cache for page
1 parent 227c608 commit f4c3e4d

File tree

1 file changed

+31
-6
lines changed
  • apps/docs/app/(home)/contributors

1 file changed

+31
-6
lines changed

apps/docs/app/(home)/contributors/page.tsx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import ContributorsHero from './contributors-hero';
1010
import PunchCardHeatmap from './punch-card-heatmap';
1111
import ReleasesTimeline from './releases-timeline';
1212

13+
const statsCache: {
14+
commitActivity?: ProcessedCommitActivity[];
15+
codeFrequency?: ProcessedCodeFrequency[];
16+
punchCard?: ProcessedPunchCard[];
17+
lastUpdated?: number;
18+
} = {};
19+
1320
export const metadata: Metadata = {
1421
title: 'Contributors | Databuddy',
1522
description:
@@ -179,7 +186,7 @@ async function fetchCommitActivity(
179186
if (response.ok) {
180187
const data = await response.json();
181188
if (Array.isArray(data) && data.length > 0) {
182-
return data
189+
const processedData = data
183190
.filter(
184191
(week: unknown): week is GitHubCommitActivity =>
185192
week !== null &&
@@ -194,6 +201,10 @@ async function fetchCommitActivity(
194201
commits: week.total,
195202
date: new Date(week.week * 1000),
196203
}));
204+
205+
statsCache.commitActivity = processedData;
206+
statsCache.lastUpdated = Date.now();
207+
return processedData;
197208
}
198209
} else {
199210
console.warn(
@@ -203,7 +214,8 @@ async function fetchCommitActivity(
203214
} catch (error) {
204215
console.error('Failed to fetch commit activity:', error);
205216
}
206-
return [];
217+
218+
return statsCache.commitActivity || [];
207219
}
208220

209221
async function fetchCodeFrequency(
@@ -218,22 +230,28 @@ async function fetchCodeFrequency(
218230
if (response.ok) {
219231
const data = await response.json();
220232
if (Array.isArray(data) && data.length > 0) {
221-
return data
233+
const processedData = data
222234
.filter((week: unknown) => Array.isArray(week) && week.length === 3)
223235
.map((week: GitHubCodeFrequency) => ({
224236
week: new Date(week[0] * 1000).toISOString().split('T')[0],
225237
additions: week[1],
226238
deletions: Math.abs(week[2]), // Make deletions positive for display
227239
date: new Date(week[0] * 1000),
228240
}));
241+
242+
// Cache the successful result
243+
statsCache.codeFrequency = processedData;
244+
statsCache.lastUpdated = Date.now();
245+
return processedData;
229246
}
230247
} else {
231248
console.warn(`GitHub API returned ${response.status} for code frequency`);
232249
}
233250
} catch (error) {
234251
console.error('Failed to fetch code frequency:', error);
235252
}
236-
return [];
253+
254+
return statsCache.codeFrequency || [];
237255
}
238256

239257
async function fetchPunchCard(
@@ -249,22 +267,29 @@ async function fetchPunchCard(
249267
const data = await response.json();
250268
if (Array.isArray(data) && data.length > 0) {
251269
const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
252-
return data
270+
const processedData = data
253271
.filter((item: unknown) => Array.isArray(item) && item.length === 3)
254272
.map((item: GitHubPunchCard) => ({
255273
day: item[0],
256274
hour: item[1],
257275
commits: item[2],
258276
dayName: dayNames[item[0]] || 'Unknown',
259277
}));
278+
279+
// Cache the successful result
280+
statsCache.punchCard = processedData;
281+
statsCache.lastUpdated = Date.now();
282+
return processedData;
260283
}
261284
} else {
262285
console.warn(`GitHub API returned ${response.status} for punch card`);
263286
}
264287
} catch (error) {
265288
console.error('Failed to fetch punch card:', error);
266289
}
267-
return [];
290+
291+
// Return cached data if available, otherwise empty array
292+
return statsCache.punchCard || [];
268293
}
269294

270295
async function fetchReleases(

0 commit comments

Comments
 (0)