From 1f8bcca779d7bb1af6372578d19637bc0c620740 Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Tue, 14 Apr 2026 19:51:12 +0200 Subject: [PATCH] feat: add GitHub stars to StatsBar and fix cache early-return MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a 4th stat (⭐ GitHub stars) to StatsBar with build-time fallback and live client-side fetch. Both Header and StatsBar share the same sessionStorage cache key and skip the API call when the cache is fresh (1h TTL), reducing fetches to at most one per session. --- site/src/components/Header.astro | 1 + site/src/components/StatsBar.astro | 56 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/site/src/components/Header.astro b/site/src/components/Header.astro index 97bb6b9..0c657d9 100644 --- a/site/src/components/Header.astro +++ b/site/src/components/Header.astro @@ -177,6 +177,7 @@ const starsLabel = stars >= 1000 ? `${(stars / 1000).toFixed(1)}k` : String(star const { value, ts } = JSON.parse(cached); if (Date.now() - ts < CACHE_TTL) { starsEl.textContent = formatStars(value); + return; // cache is fresh, skip API call } } diff --git a/site/src/components/StatsBar.astro b/site/src/components/StatsBar.astro index 748d245..eef3c02 100644 --- a/site/src/components/StatsBar.astro +++ b/site/src/components/StatsBar.astro @@ -1,9 +1,23 @@ --- import { getTotalRuleCount, getTotalExporterCount, data } from '../data/rules'; +import { GITHUB_API_REPO_URL } from '../data/site'; const totalRules = getTotalRuleCount(); const totalExporters = getTotalExporterCount(); const totalGroups = data.groups.length; + +let stars = 0; +try { + const res = await fetch(GITHUB_API_REPO_URL, { + headers: { 'Accept': 'application/vnd.github+json' } + }); + if (res.ok) { + const json = await res.json(); + stars = json.stargazers_count ?? 0; + } +} catch {} + +const starsLabel = stars >= 1000 ? `${(stars / 1000).toFixed(1)}k` : String(stars); ---
@@ -19,4 +33,46 @@ const totalGroups = data.groups.length;
{totalGroups}
categories
+ +
+ + {starsLabel} +
+
GitHub stars
+
+ +