From 313e584711ba2bd14768b6906ef4599e2cfe6f82 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 5 Jan 2023 04:27:50 +0400 Subject: [PATCH] Fix opensource star count shoing NaN --- astro.config.mjs | 2 +- src/lib/github.ts | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index 1ea3bee11..ee310e656 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -30,6 +30,6 @@ export default defineConfig({ filter: shouldIndexPage, serialize: serializeSitemap, }), - critters(), + // critters(), ], }); diff --git a/src/lib/github.ts b/src/lib/github.ts index 52eff3d6a..5cb614458 100644 --- a/src/lib/github.ts +++ b/src/lib/github.ts @@ -2,20 +2,33 @@ const formatter = Intl.NumberFormat('en-US', { notation: 'compact', }); +let starCount: number | undefined = undefined; + export async function countStars( repo = 'kamranahmedse/developer-roadmap' ): Promise { - const repoData = await fetch(`https://api.github.com/repos/${repo}`); - const json = await repoData.json(); + if (starCount) { + return starCount; + } - return json.stargazers_count * 1; + try { + const repoData = await fetch(`https://api.github.com/repos/${repo}`); + const json = await repoData.json(); + + starCount = json.stargazers_count * 1; + } catch (e) { + console.log('Failed to fetch stars', e); + starCount = 224000; + } + + return starCount; } export async function getFormattedStars( repo = 'kamranahmedse/developer-roadmap' ): Promise { if (import.meta.env.DEV) { - return '223k'; + return '224k'; } const stars = await countStars(repo);