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:
@@ -1,26 +1,17 @@
|
||||
<script setup lang="ts">
|
||||
import { useData } from "vitepress";
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { computed } from "vue";
|
||||
import { normalizeLocale, t } from "../i18n/ui.mjs";
|
||||
import { data as githubStars } from "./github-stars.data.ts";
|
||||
|
||||
const { lang } = useData();
|
||||
const locale = computed(() => normalizeLocale(lang.value));
|
||||
const starLabel = computed(() => t(locale.value, "github.star"));
|
||||
|
||||
const stars = ref<string | null>(null);
|
||||
// Resolved at build time (see github-stars.data.ts), so no per-page request to
|
||||
// api.github.com is made from the browser.
|
||||
const stars = githubStars.display;
|
||||
const repo = "https://github.com/snapotter-hq/snapotter";
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const res = await fetch("https://api.github.com/repos/snapotter-hq/snapotter");
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
const count = data.stargazers_count;
|
||||
stars.value = count >= 1000 ? `${(count / 1000).toFixed(1)}k` : String(count);
|
||||
} catch {
|
||||
// Stars count won't show if fetch fails
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -33,7 +24,7 @@ onMounted(async () => {
|
||||
<span class="github-btn-label">{{ starLabel }}</span>
|
||||
</a>
|
||||
<a
|
||||
v-if="stars !== null"
|
||||
v-if="stars"
|
||||
:href="`${repo}/stargazers`"
|
||||
target="_blank"
|
||||
rel="noopener"
|
||||
|
||||
@@ -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