import { createSignal, onSettled, Show, untrack } from "solid-js"; import { isServer } from "@solidjs/web"; import type { ProjectStats as Stats } from "../lib/project-stats"; /** * Homepage stats block (npm downloads, open issues, open PRs). * * Renders `initial` (the build-time copy from src/generated/stats.ts) so the prerendered page and * hydration agree, then asks `/api/stats` (worker/index.ts) for the live, day-cached copy and swaps * it in. If that request fails (no Worker behind the static site, GitHub down), the build-time * numbers stay. */ async function fetchLiveStats(): Promise { const res = await fetch("/api/stats"); if (!res.ok) throw new Error(`/api/stats responded ${res.status}`); return res.json(); } export function ProjectStats(props: { initial: Stats | null }) { const [stats, setStats] = createSignal(untrack(() => props.initial)); onSettled(() => { if (isServer) return; let active = true; fetchLiveStats().then( fresh => { if (active) setStats(fresh); }, () => {}, ); return () => { active = false; }; }); return ( {s => ( )} ); }