From 907a41cef170c13421d021b69a2b8fed4b8ba252 Mon Sep 17 00:00:00 2001 From: Rotimi Best Date: Fri, 12 Dec 2025 09:35:59 +0100 Subject: [PATCH] chore: move stars fetch to server --- apps/classroomio-com/.gitignore | 23 -------------- .../src/lib/Navigation/Navigation.svelte | 17 ++-------- apps/website/src/routes/+layout.svelte | 2 +- apps/website/src/routes/+layout.ts | 31 +++++++++++++++++-- 4 files changed, 32 insertions(+), 41 deletions(-) delete mode 100644 apps/classroomio-com/.gitignore diff --git a/apps/classroomio-com/.gitignore b/apps/classroomio-com/.gitignore deleted file mode 100644 index 7a9949ce..00000000 --- a/apps/classroomio-com/.gitignore +++ /dev/null @@ -1,23 +0,0 @@ -.DS_Store -/node_modules/ -node_modules -/src/node_modules/@sapper/ -yarn-error.log -.svelte-kit/ -.env -/dist/ -/cert/ -package-lock.json -.pnpm-store/* - -# Local Netlify folder -.netlify - -# Sentry Auth Token -.env.sentry-build-plugin - -/_supascripts/ -data.sql -changes.sql - -.svelte-kit \ No newline at end of file diff --git a/apps/website/src/lib/Navigation/Navigation.svelte b/apps/website/src/lib/Navigation/Navigation.svelte index d4894f52..c507b636 100644 --- a/apps/website/src/lib/Navigation/Navigation.svelte +++ b/apps/website/src/lib/Navigation/Navigation.svelte @@ -9,7 +9,8 @@ import { onMount } from 'svelte'; import { fly } from 'svelte/transition'; - let stars = 0; + export let stars: number; + let showDrawer = false; let showSolutions = false; let activeLink = ''; @@ -53,20 +54,6 @@ $: activeLink = page.url.pathname; $: activeHash = page.url.hash; $: isSolutionsActive = solutions.some((s) => activeHash.includes(s.key)); - - async function setStars() { - 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.error('Error fetching GitHub stars:', error); - } - } - - onMount(() => { - setStars(); - });
- + {#if page.status === 404} diff --git a/apps/website/src/routes/+layout.ts b/apps/website/src/routes/+layout.ts index ae403d6b..87a70e1e 100644 --- a/apps/website/src/routes/+layout.ts +++ b/apps/website/src/routes/+layout.ts @@ -2,13 +2,40 @@ import type { MetaTagsProps } from 'svelte-meta-tags'; export const prerender = true; -export function load({ url }) { +export async function load({ url }) { + const stars = await getStars(); + return { baseMetaTags: getBaseMetaTags(url), - url: url.pathname + url: url.pathname, + stars }; } +const starsCache = new Map(); +const CACHE_TIME = 1000 * 60 * 60 * 48; // 48 hours +const CACHE_KEY = 'github-stars'; + +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'); + + const response = await fetch('https://api.github.com/repos/classroomio/classroomio'); + const data = await response.json(); + const stars = data?.stargazers_count || 0; + + starsCache.set(CACHE_KEY, { stars, lastUpdated: now }); + + return stars; +} + function getBaseMetaTags(url: URL) { const metatags = Object.freeze({ title: 'ClassroomIO | The Open Source Learning Management System for Companies',