File tree Expand file tree Collapse file tree 1 file changed +20
-6
lines changed
apps/framework-docs-v2/src/lib Expand file tree Collapse file tree 1 file changed +20
-6
lines changed Original file line number Diff line number Diff line change 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+ ) ;
You can’t perform that action at this time.
0 commit comments