Move leaderboard fetching to server-side
All checks were successful
Deploy to Cloudflare Pages / deploy (push) Successful in 4m3s

- Create src/lib/api.ts with fetchLeaderboard function for server use
- Update page.tsx to async component that fetches data at build/request time
- Pass fetched data to ClientPage as initialAppData prop
- Remove client-side useEffect and state management from ClientPage
- Reduce client bundle size (4.07kb → 2.99kb)
- Improve security by keeping API credentials server-only
This commit is contained in:
2026-03-01 14:28:31 +01:00
parent a096075d8c
commit 2da1b150ed
3 changed files with 40 additions and 36 deletions

28
src/lib/api.ts Normal file
View File

@@ -0,0 +1,28 @@
import { AppData } from './types';
export async function fetchLeaderboard(): Promise<AppData | null> {
const API_BASE = process.env.NEXT_PUBLIC_API_BASE || '';
const SERVER_ID = process.env.NEXT_PUBLIC_SERVER_ID || '';
const ACCESS_TOKEN = process.env.NEXT_PUBLIC_ACCESS_TOKEN || '';
if (!API_BASE || !SERVER_ID || !ACCESS_TOKEN) {
console.warn('Missing API configuration');
return null;
}
try {
const res = await fetch(`${API_BASE}/server/${SERVER_ID}/leaderboard`, {
headers: {
"Access-Key": ACCESS_TOKEN
}
});
if (!res.ok) return null;
const data = await res.json();
// Treat empty leaderboard (no drivers) as null to fall back to defaults
if (!data || !data.drivers || data.drivers.length === 0) return null;
return data;
} catch (e) {
console.error("Failed to load leaderboard from API:", e);
return null;
}
}