fix(website): cache GitHub stars on server for 3 hours (#730)
Move star count fetching from universal +layout.ts to server-only code so the GitHub API is not called on every page load or client navigation. Add an in-memory cache with a 3-hour TTL and in-flight request deduplication. On API errors (including rate limits), return the last successful value when available. Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: Rotimi Best <[email protected]>
This commit is contained in:
co-authored by
Cursor Agent
Rotimi Best
parent
ce0dd376e6
commit
1a9c240501
@@ -0,0 +1,68 @@
|
|||||||
|
const GITHUB_REPO_URL = 'https://api.github.com/repos/classroomio/classroomio';
|
||||||
|
const CACHE_TTL_MS = 1000 * 60 * 60 * 3;
|
||||||
|
|
||||||
|
const starsCache = {
|
||||||
|
stars: 0,
|
||||||
|
lastUpdated: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
let inFlightFetch: Promise<number> | null = null;
|
||||||
|
|
||||||
|
function isCacheFresh(now: number) {
|
||||||
|
return starsCache.lastUpdated > 0 && now - starsCache.lastUpdated < CACHE_TTL_MS;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getGithubStars(): Promise<number> {
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
if (isCacheFresh(now)) {
|
||||||
|
return starsCache.stars;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inFlightFetch) {
|
||||||
|
return inFlightFetch;
|
||||||
|
}
|
||||||
|
|
||||||
|
inFlightFetch = fetchGithubStars(now).finally(() => {
|
||||||
|
inFlightFetch = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return inFlightFetch;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchGithubStars(now: number): Promise<number> {
|
||||||
|
try {
|
||||||
|
const response = await fetch(GITHUB_REPO_URL, {
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/vnd.github+json',
|
||||||
|
'User-Agent': 'classroomio-website'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error('getGithubStars error: GitHub API returned', response.status);
|
||||||
|
|
||||||
|
if (starsCache.lastUpdated > 0) {
|
||||||
|
return starsCache.stars;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
const stars = typeof data?.stargazers_count === 'number' ? data.stargazers_count : 0;
|
||||||
|
|
||||||
|
starsCache.stars = stars;
|
||||||
|
starsCache.lastUpdated = now;
|
||||||
|
|
||||||
|
return stars;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('getGithubStars error:', error);
|
||||||
|
|
||||||
|
if (starsCache.lastUpdated > 0) {
|
||||||
|
return starsCache.stars;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { getGithubStars } from '$lib/server/github-stars';
|
||||||
|
|
||||||
|
export async function load() {
|
||||||
|
const stars = await getGithubStars();
|
||||||
|
|
||||||
|
return { stars };
|
||||||
|
}
|
||||||
@@ -3,50 +3,18 @@ import type { MetaTagsProps } from 'svelte-meta-tags';
|
|||||||
export const prerender = true;
|
export const prerender = true;
|
||||||
|
|
||||||
export async function load({ url }) {
|
export async function load({ url }) {
|
||||||
const stars = await getStars();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
baseMetaTags: getBaseMetaTags(url),
|
baseMetaTags: getBaseMetaTags(url),
|
||||||
url: url.pathname,
|
url: url.pathname
|
||||||
stars
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const starsCache = new Map<string, { stars: number; lastUpdated: number }>();
|
|
||||||
const CACHE_TIME = 1000 * 60 * 60 * 48; // 48 hours
|
|
||||||
const CACHE_KEY = 'github-stars';
|
|
||||||
const DEFAULT_META_TITLE = 'ClassroomIO | One Platform for Customer, Partner, and Employee Training';
|
const DEFAULT_META_TITLE = 'ClassroomIO | One Platform for Customer, Partner, and Employee Training';
|
||||||
const DEFAULT_META_DESCRIPTION =
|
const DEFAULT_META_DESCRIPTION =
|
||||||
'One platform for customer academies, partner certification, and employee training. Build courses with AI, publish under your domain, and track completions.';
|
'One platform for customer academies, partner certification, and employee training. Build courses with AI, publish under your domain, and track completions.';
|
||||||
const DEFAULT_OG_IMAGE_URL = 'https://brand.cdn.clsrio.com/og/classroomio-opengraph.jpg';
|
const DEFAULT_OG_IMAGE_URL = 'https://brand.cdn.clsrio.com/og/classroomio-opengraph.jpg';
|
||||||
const DEFAULT_OG_IMAGE_ALT = 'ClassroomIO platform for customer, partner, and employee education';
|
const DEFAULT_OG_IMAGE_ALT = 'ClassroomIO platform for customer, partner, and employee education';
|
||||||
|
|
||||||
async function getStars() {
|
|
||||||
const now = Date.now();
|
|
||||||
|
|
||||||
const cacheData = starsCache.get(CACHE_KEY);
|
|
||||||
if (cacheData && now - cacheData.lastUpdated < CACHE_TIME) {
|
|
||||||
console.log('Returning cached stars');
|
|
||||||
return cacheData.stars;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Fetching stars from GitHub');
|
|
||||||
|
|
||||||
let stars = 0;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch('https://api.github.com/repos/classroomio/classroomio');
|
|
||||||
const data = await response.json();
|
|
||||||
stars = data?.stargazers_count || 0;
|
|
||||||
} catch (error) {
|
|
||||||
console.log('error fetching stars', error);
|
|
||||||
}
|
|
||||||
|
|
||||||
starsCache.set(CACHE_KEY, { stars, lastUpdated: now });
|
|
||||||
|
|
||||||
return stars;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getBaseMetaTags(url: URL) {
|
function getBaseMetaTags(url: URL) {
|
||||||
const canonicalUrl = new URL(url.pathname, url.origin).href;
|
const canonicalUrl = new URL(url.pathname, url.origin).href;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user