Based on your context, I can suggest the optimal solution for your situation. You discovered that shields.io provides JSON endpoints, which is the key insight here.[1][2]
Instead of parsing SVG, shields.io provides a .json endpoint that returns structured data. Here's the cleanest approach:[1]
async function getBadgeValue(owner, repo, metric) {
try {
const response = await fetch(`https://img.shields.io/github/${metric}/${owner}/${repo}/total.json`);
const data = await response.json();
return data.message; // e.g., "1,234"
} catch (e) {
return '0';
}
}
// Usage
(async function() {
const downloads = await getBadgeValue('edgeleap', 'diffsense', 'downloads');
const stars = await getBadgeValue('edgeleap', 'diffsense', 'stars');
const forks = await getBadgeValue('edgeleap', 'diffsense', 'forks');
document.getElementById('downloads-count').textContent = downloads;
document.getElementById('stars-count').textContent = stars;
document.getElementById('forks-count').textContent = forks;
})();Shields.io as primary with GitHub API as fallback - This addresses your exact suggestion from the issue. Shields.io benefits:[1]
- Uses authenticated GitHub tokens (5,000/hour vs 60/hour)
- Built-in caching (~5 minutes)
- Fast response times
- No additional dependencies
- No repository commits
GitHub API as fallback - Only use if shields.io fails:
async function getBadgeValue(owner, repo, metric) {
// Try shields.io first
try {
const response = await fetch(`https://img.shields.io/github/${metric}/${owner}/${repo}/total.json`);
if (response.ok) {
const data = await response.json();
return data.message;
}
} catch (e) {}
// Fallback to GitHub API
try {
if (metric === 'downloads') {
const res = await fetch(`https://api.github.com/repos/${owner}/${repo}/releases`);
const releases = await res.json();
let total = 0;
releases.forEach(r => {
(r.assets || []).forEach(a => total += a.download_count || 0);
});
return total.toLocaleString();
}
// Similar logic for stars/forks
} catch (e) {
return '0';
}
}This approach avoids:[2][1]
- No Cloudflare Workers - No extra dependencies
- No GitHub Actions cron - No commit spam
- No SVG parsing - Cleaner than regex/DOM parsing
- Instant updates - Faster than GitHub API which can be "slow and shows 0 for a bit" as you mentioned
The shields.io JSON endpoint is the "hack that isn't needed" discovery you mentioned - it's the official, cleaner solution that gives you both speed and reliability under high traffic.[1]