mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix(landing,docs): use build-time GitHub star count, drop per-page api.github.com fetch (#560)
Removes the client-side api.github.com fetch from the landing navbar and the docs theme; both now render a build-time star count (docs via a new VitePress data loader). deploy-docs.yml gets GITHUB_TOKEN plus a daily refresh cron, mirroring deploy-landing.yml. Fixes #555.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
// Build-time GitHub star count for the docs navbar. Fetched in Node during the
|
||||
// VitePress build (never from the browser), so the docs site makes no per-page
|
||||
// request to api.github.com. Mirrors the landing site's build-time getStarCount
|
||||
// (apps/landing/src/lib/stats.ts): sends an Authorization header when
|
||||
// GITHUB_TOKEN is set (CI) to lift the 60 req/hr unauthenticated limit, and
|
||||
// degrades to a maintained constant if the API is unreachable or rate-limited,
|
||||
// so a build never ships an empty count.
|
||||
import { defineLoader } from "vitepress";
|
||||
|
||||
const GITHUB_REPO = "snapotter-hq/SnapOtter";
|
||||
|
||||
// Fallback used when the upstream fetch fails. Keep roughly current so a
|
||||
// degraded build still shows a believable figure (matches STAR_FALLBACK in
|
||||
// apps/landing/src/lib/stats.ts).
|
||||
const STAR_FALLBACK = 1720;
|
||||
|
||||
export interface GitHubStarsData {
|
||||
/** Compact star count for display, e.g. "1.7k". */
|
||||
display: string;
|
||||
}
|
||||
|
||||
declare const data: GitHubStarsData;
|
||||
|
||||
export { data };
|
||||
|
||||
/** Compact integer formatting: 1720 -> "1.7k", 2_300_000 -> "2.3M". */
|
||||
function formatCompact(n: number): string {
|
||||
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;
|
||||
if (n >= 1_000) return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}k`;
|
||||
return n.toString();
|
||||
}
|
||||
|
||||
async function fetchStarCount(): Promise<number> {
|
||||
try {
|
||||
const token = process.env.GITHUB_TOKEN;
|
||||
const res = await fetch(`https://api.github.com/repos/${GITHUB_REPO}`, {
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
"User-Agent": "SnapOtter-Docs",
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
},
|
||||
});
|
||||
if (res.ok) {
|
||||
const json = await res.json();
|
||||
if (typeof json.stargazers_count === "number") return json.stargazers_count;
|
||||
}
|
||||
} catch {
|
||||
// Network/JSON failure: fall through to the fallback below.
|
||||
}
|
||||
return STAR_FALLBACK;
|
||||
}
|
||||
|
||||
export default defineLoader({
|
||||
async load(): Promise<GitHubStarsData> {
|
||||
return { display: formatCompact(await fetchStarCount()) };
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user