Add GitHub star counter to website header

This commit is contained in:
Mohamed Boudra
2026-04-12 12:53:21 +07:00
parent 44cc7a3771
commit 698a18e88f
3 changed files with 63 additions and 7 deletions
+40
View File
@@ -0,0 +1,40 @@
import { createServerFn } from "@tanstack/react-start";
interface GitHubRepo {
stargazers_count: number;
}
function formatStars(count: number): string {
if (count < 1000) return String(count);
const k = count / 1000;
return `${k % 1 === 0 ? k.toFixed(0) : k.toFixed(1)}k`;
}
const GITHUB_REPO_URL = "https://api.github.com/repos/getpaseo/paseo";
async function fetchStarCount(): Promise<string> {
try {
const res = await fetch(GITHUB_REPO_URL, {
headers: {
Accept: "application/vnd.github+json",
"User-Agent": "paseo-website",
},
cf: {
cacheEverything: true,
cacheTtl: 60,
cacheKey: "github-repo-stars",
},
} as RequestInit);
if (!res.ok) return "";
const repo = (await res.json()) as GitHubRepo;
return formatStars(repo.stargazers_count);
} catch {
return "";
}
}
export const getStarCount = createServerFn({ method: "GET" }).handler(async () => {
const stars = await fetchStarCount();
return { stars };
});