Fix caching to prevent build-time rendering

This commit is contained in:
Markus Thielker 2025-02-26 14:15:27 +01:00
parent 05bd6822db
commit a3e2f442a0

View file

@ -21,7 +21,7 @@ const calculateAge = (birthdate: Date): number => {
return age; return age;
}; };
const getRepos = async (username: string) => { const getRepos = async (username: string, options: RequestInit = {}) => {
let repos: Repository[] = []; let repos: Repository[] = [];
let page = 1; let page = 1;
let hasNextPage = true; let hasNextPage = true;
@ -31,6 +31,7 @@ const getRepos = async (username: string) => {
headers: { headers: {
Accept: 'application/vnd.github+json', Accept: 'application/vnd.github+json',
}, },
...options,
}); });
const data = await response.json(); const data = await response.json();
@ -46,11 +47,12 @@ const getRepos = async (username: string) => {
return repos; return repos;
}; };
const getLanguages = async (repo: Repository) => { const getLanguages = async (repo: Repository, options: RequestInit = {}) => {
const response = await fetch(repo.languages_url, { const response = await fetch(repo.languages_url, {
headers: { headers: {
Accept: 'application/vnd.github+json', Accept: 'application/vnd.github+json',
}, },
...options,
}); });
return await response.json(); return await response.json();
}; };
@ -59,7 +61,7 @@ const accumulateLanguages = async (repos: Repository[]) => {
const languages: { [key: string]: number } = {}; const languages: { [key: string]: number } = {};
for (const repo of repos) { for (const repo of repos) {
const repoLanguages = await getLanguages(repo); const repoLanguages = await getLanguages(repo, { next: { revalidate: 3600 } });
for (const lang in repoLanguages) { for (const lang in repoLanguages) {
languages[lang] = (languages[lang] || 0) + repoLanguages[lang]; languages[lang] = (languages[lang] || 0) + repoLanguages[lang];
} }
@ -80,11 +82,11 @@ const accumulateStars = (repos: Repository[]) => {
export default async function Home() { export default async function Home() {
const profileResponse = await fetch('https://api.github.com/users/markusthielker'); const profileResponse = await fetch('https://api.github.com/users/markusthielker', { next: { revalidate: 3600 } });
const profile = await profileResponse.json() as PublicUser; const profile = await profileResponse.json() as PublicUser;
const username = 'markusthielker'; const username = 'markusthielker';
const repos = await getRepos(username); const repos = await getRepos(username, { next: { revalidate: 3600 } });
const languages = await accumulateLanguages(repos); const languages = await accumulateLanguages(repos);
const stars = accumulateStars(repos); const stars = accumulateStars(repos);
const totalBytes = Object.values(languages).reduce((sum, bytes) => sum + bytes, 0); const totalBytes = Object.values(languages).reduce((sum, bytes) => sum + bytes, 0);