perf(website): cache GitHub release metadata in KV
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { createServerFn } from "@tanstack/react-start";
|
||||
import { getBlockingColdCache } from "./github-cache";
|
||||
|
||||
interface GitHubRepo {
|
||||
stargazers_count: number;
|
||||
@@ -11,30 +12,35 @@ function formatStars(count: number): string {
|
||||
}
|
||||
|
||||
const GITHUB_REPO_URL = "https://api.github.com/repos/getpaseo/paseo";
|
||||
const STARS_CACHE_KEY = "github-stars:v1";
|
||||
|
||||
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 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) throw new Error(`github repo ${res.status}`);
|
||||
|
||||
const repo = (await res.json()) as GitHubRepo;
|
||||
return formatStars(repo.stargazers_count);
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
const repo = (await res.json()) as GitHubRepo;
|
||||
return formatStars(repo.stargazers_count);
|
||||
}
|
||||
|
||||
function isStars(value: unknown): value is string {
|
||||
return typeof value === "string" && /^(\d+|\d+\.\d+k|\d+k)$/.test(value);
|
||||
}
|
||||
|
||||
export const getStarCount = createServerFn({ method: "GET" }).handler(async () => {
|
||||
const stars = await fetchStarCount();
|
||||
const stars = await getBlockingColdCache({
|
||||
key: STARS_CACHE_KEY,
|
||||
isValue: isStars,
|
||||
fetchFresh: fetchStarCount,
|
||||
});
|
||||
return { stars };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user