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,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 async function load({ url }) {
|
||||
const stars = await getStars();
|
||||
|
||||
return {
|
||||
baseMetaTags: getBaseMetaTags(url),
|
||||
url: url.pathname,
|
||||
stars
|
||||
url: url.pathname
|
||||
};
|
||||
}
|
||||
|
||||
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_DESCRIPTION =
|
||||
'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_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) {
|
||||
const canonicalUrl = new URL(url.pathname, url.origin).href;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user