Skip to content

Instantly share code, notes, and snippets.

@eonist
Created December 23, 2025 12:03
Show Gist options
  • Select an option

  • Save eonist/8b0d3410bc531671078dbc4f9408a0fa to your computer and use it in GitHub Desktop.

Select an option

Save eonist/8b0d3410bc531671078dbc4f9408a0fa to your computer and use it in GitHub Desktop.
shield_solution_2.md

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]

Recommended Solution: Use Shields.io JSON API as Primary

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;
})();

Why This Solves Your Concerns

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';
  }
}

Advantages Over Other Solutions

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]

1 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment