Skip to content

Commit 11a6a32

Browse files
committed
change github star fetching to shields API
1 parent d343ff8 commit 11a6a32

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

src/components/GithubStarButton.svelte

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
<script lang="ts">
2-
import { onMount } from "svelte";
32
import createLocaleStorage from "../lib/createLocaleStorage";
43
54
interface StarCountStorageData {
6-
value: number;
5+
value: string;
76
fetchedAt: number;
87
}
98
10-
interface GitHubApiResponse {
11-
stargazers_count?: number;
9+
interface ShieldsApiResponse {
10+
schemaVersion: number;
11+
label: string;
12+
message: string;
13+
color: string;
1214
}
1315
1416
const REPOSITORY_PATH = "matschik/component-party.dev";
15-
const STAR_COUNT_EXPIRES_IN_MS = 1000 * 60 * 2;
17+
const STAR_COUNT_EXPIRES_IN_MS = 1000 * 60 * 5; // Shields.io caches for 5-15 minutes
1618
17-
const starCountStorage = createLocaleStorage("github-star-count", {
18-
value: 0,
19+
const starCountStorage = createLocaleStorage("github-star-count-v2", {
20+
value: "0",
1921
fetchedAt: 0,
2022
});
2123
22-
let starCount: number = $state(0);
24+
let starCount: string = $state("0");
2325
let isFetchingStarCount: boolean = $state(false);
2426
2527
async function getRepoStarCount(): Promise<void> {
@@ -38,33 +40,36 @@
3840
3941
isFetchingStarCount = true;
4042
41-
// Github public API rate limit: 60 requests per hour
42-
const data: GitHubApiResponse = await fetch(
43-
`https://api.github.com/repos/${REPOSITORY_PATH}`,
44-
{
45-
headers: {
46-
Accept: "application/vnd.github.v3.star+json",
47-
Authorization: "",
43+
try {
44+
// Using Shields.io JSON endpoint - no rate limits, cached for 5-15 minutes
45+
const data: ShieldsApiResponse = await fetch(
46+
`https://img.shields.io/github/stars/${REPOSITORY_PATH}.json`,
47+
{
48+
headers: {
49+
Accept: "application/json",
50+
},
4851
},
49-
},
50-
)
51-
.finally(() => {
52-
isFetchingStarCount = false;
53-
})
54-
.then((r) => r.json());
52+
)
53+
.finally(() => {
54+
isFetchingStarCount = false;
55+
})
56+
.then((r) => r.json());
5557
56-
if (data.stargazers_count) {
57-
starCount = data.stargazers_count;
58-
starCountStorage.setJSON({
59-
value: starCount,
60-
fetchedAt: Date.now(),
61-
});
58+
if (data.message) {
59+
// Use the formatted string directly from Shields.io (e.g., "3.1k", "500")
60+
starCount = data.message;
61+
starCountStorage.setJSON({
62+
value: starCount,
63+
fetchedAt: Date.now(),
64+
});
65+
}
66+
} catch (error) {
67+
console.warn("Failed to fetch star count from Shields.io:", error);
68+
// Keep the existing cached value if available
6269
}
6370
}
6471
65-
onMount(() => {
66-
getRepoStarCount();
67-
});
72+
getRepoStarCount();
6873
6974
function onButtonClick(): void {
7075
starCountStorage.remove();
@@ -85,11 +90,11 @@
8590
></span>
8691
<span class="hidden sm:inline">Star</span>
8792
</span>
88-
{#if isFetchingStarCount || starCount !== 0}
93+
{#if isFetchingStarCount || starCount !== "0"}
8994
<div
9095
class="hidden h-full items-center justify-center px-3 sm:flex border-[#373b43] sm:border-l"
9196
>
92-
{#if isFetchingStarCount && starCount === 0}
97+
{#if isFetchingStarCount && starCount === "0"}
9398
<span
9499
class="iconify ph--spinner animate-spin size-4 mx-1"
95100
aria-hidden="true"

0 commit comments

Comments
 (0)