Skip to content

Commit 66ee998

Browse files
committed
caching gh stars
1 parent eee902b commit 66ee998

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

apps/framework-docs-v2/src/lib/github-stars.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
import { unstable_cache } from "next/cache";
2+
13
/**
24
* Fetches the GitHub star count for the 514-labs/moose repository.
3-
* Uses Next.js fetch cache with 1 hour revalidation to minimize API calls.
5+
* Uses Next.js unstable_cache to ensure only one API call during build,
6+
* even when multiple pages are generated in parallel.
47
*/
5-
export async function getGitHubStars(): Promise<number | null> {
8+
async function fetchGitHubStars(): Promise<number | null> {
69
try {
710
const response = await fetch(
811
"https://api.github.com/repos/514-labs/moose",
912
{
10-
next: {
11-
// Cache for 1 hour (3600 seconds)
12-
revalidate: 3600,
13-
},
1413
headers: {
1514
// GitHub API requires a user-agent
1615
"User-Agent": "MooseDocs",
16+
// Optional: Add Authorization header with token to increase rate limit
17+
// Authorization: `token ${process.env.GITHUB_TOKEN}`,
1718
},
1819
},
1920
);
@@ -36,3 +37,16 @@ export async function getGitHubStars(): Promise<number | null> {
3637
return null;
3738
}
3839
}
40+
41+
/**
42+
* Cached version that ensures only one API call during build.
43+
* Cache is shared across all page generations.
44+
*/
45+
export const getGitHubStars = unstable_cache(
46+
async () => fetchGitHubStars(),
47+
["github-stars"],
48+
{
49+
revalidate: 3600, // Cache for 1 hour
50+
tags: ["github-stars"],
51+
},
52+
);

0 commit comments

Comments
 (0)