chore: move stars fetch to server
This commit is contained in:
@@ -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();
|
||||
});
|
||||
</script>
|
||||
|
||||
<div
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<MetaTags {...metaTags} />
|
||||
|
||||
<div class="overflow-hidden bg-white">
|
||||
<Navigation />
|
||||
<Navigation stars={data.stars} />
|
||||
|
||||
<PageTransition url={data.url}>
|
||||
{#if page.status === 404}
|
||||
|
||||
@@ -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<string, { stars: number; lastUpdated: number }>();
|
||||
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',
|
||||
|
||||
Reference in New Issue
Block a user