Skip to content

Commit d47066f

Browse files
committed
optimize gitstats.js with GitHub search API
1 parent 5fc8bcb commit d47066f

2 files changed

Lines changed: 9 additions & 26 deletions

File tree

assets/js/components/gitstats.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,14 @@ async function getGitStats(username) {
88

99
const repos = await reposResponse.json();
1010

11-
// Use Promise.all to make requests in parallel
12-
const commitCounts = await Promise.all(
13-
repos.map(async (repo) => {
14-
try {
15-
const response = await fetch(`https://api.github.com/repos/${username}/${repo.name}/commits?author=${username}&per_page=1`);
16-
17-
if (response.ok) {
18-
const linkHeader = response.headers.get('link');
19-
if (linkHeader) {
20-
const match = linkHeader.match(/page=(\d+)>; rel="last"/);
21-
return match ? parseInt(match[1]) : 1;
22-
} else {
23-
const commits = await response.json();
24-
return commits.length;
25-
}
26-
}
27-
return 0;
28-
} catch (error) {
29-
console.warn(`Failed to fetch commits for ${repo.name}:`, error);
30-
return 0;
31-
}
32-
})
33-
);
34-
35-
const totalCommits = commitCounts.reduce((sum, count) => sum + count, 0);
11+
const commitsResponse = await fetch(`https://api.github.com/search/commits?q=author:${username}&per_page=1`);
12+
13+
if (!commitsResponse.ok) {
14+
throw new Error(`Failed to fetch commits: ${commitsResponse.status}`);
15+
}
16+
17+
const commitsData = await commitsResponse.json();
18+
const totalCommits = commitsData.total_count;
3619

3720
return {
3821
commitCount: totalCommits,

layouts/shortcodes/postcount.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{ len (where (where site.RegularPages "Section" "posts") "Params.unlisted" "ne" true) }}
1+
{{ len (where (where site.RegularPages "Section" "posts") "Params.unlisted" "ne" true) -}}

0 commit comments

Comments
 (0)