From 722ff22328962f9cd08f621dcf23e105320985de Mon Sep 17 00:00:00 2001 From: svemagie <869694+svemagie@users.noreply.github.com> Date: Sun, 8 Mar 2026 05:44:36 +0100 Subject: [PATCH] Fix GitHub widget/data endpoints to prefer /github/api routes --- _data/githubActivity.js | 35 +++++++++++-------- _data/githubStarred.js | 30 +++++++++++----- _includes/components/widgets/github-repos.njk | 6 ++-- changelog.njk | 2 +- starred.njk | 17 +++++++-- theme/_data/githubActivity.js | 35 +++++++++++-------- theme/_data/githubStarred.js | 30 +++++++++++----- .../components/widgets/github-repos.njk | 6 ++-- theme/changelog.njk | 2 +- theme/starred.njk | 17 +++++++-- 10 files changed, 120 insertions(+), 60 deletions(-) diff --git a/_data/githubActivity.js b/_data/githubActivity.js index d8a19a7..0693786 100644 --- a/_data/githubActivity.js +++ b/_data/githubActivity.js @@ -16,21 +16,28 @@ const FALLBACK_FEATURED_REPOS = process.env.GITHUB_FEATURED_REPOS?.split(",").fi * Fetch from Indiekit's public GitHub API endpoint */ async function fetchFromIndiekit(endpoint) { - try { - const url = `${INDIEKIT_URL}/githubapi/api/${endpoint}`; - console.log(`[githubActivity] Fetching from Indiekit: ${url}`); - const data = await EleventyFetch(url, { - duration: "15m", - type: "json", - }); - console.log(`[githubActivity] Indiekit ${endpoint} success`); - return data; - } catch (error) { - console.log( - `[githubActivity] Indiekit API unavailable for ${endpoint}: ${error.message}` - ); - return null; + const urls = [ + `${INDIEKIT_URL}/github/api/${endpoint}`, + `${INDIEKIT_URL}/githubapi/api/${endpoint}`, + ]; + + for (const url of urls) { + try { + console.log(`[githubActivity] Fetching from Indiekit: ${url}`); + const data = await EleventyFetch(url, { + duration: "15m", + type: "json", + }); + console.log(`[githubActivity] Indiekit ${endpoint} success via ${url}`); + return data; + } catch (error) { + console.log( + `[githubActivity] Indiekit API unavailable for ${endpoint} at ${url}: ${error.message}` + ); + } } + + return null; } /** diff --git a/_data/githubStarred.js b/_data/githubStarred.js index 659d181..ca5cf71 100644 --- a/_data/githubStarred.js +++ b/_data/githubStarred.js @@ -12,16 +12,28 @@ const INDIEKIT_URL = process.env.SITE_URL || "https://example.com"; export default async function () { try { - const url = `${INDIEKIT_URL}/githubapi/api/starred/all`; - const response = await EleventyFetch(url, { - duration: "15m", - type: "json", - }); + const urls = [ + `${INDIEKIT_URL}/github/api/starred/all`, + `${INDIEKIT_URL}/githubapi/api/starred/all`, + ]; - return { - totalCount: response.totalCount || 0, - buildDate: new Date().toISOString(), - }; + for (const url of urls) { + try { + const response = await EleventyFetch(url, { + duration: "15m", + type: "json", + }); + + return { + totalCount: response.totalCount || 0, + buildDate: new Date().toISOString(), + }; + } catch (error) { + console.log(`[githubStarred] Could not fetch ${url}: ${error.message}`); + } + } + + throw new Error("No GitHub starred endpoint responded"); } catch (error) { console.log(`[githubStarred] Could not fetch starred count: ${error.message}`); return { diff --git a/_includes/components/widgets/github-repos.njk b/_includes/components/widgets/github-repos.njk index 3adf963..3fd7b46 100644 --- a/_includes/components/widgets/github-repos.njk +++ b/_includes/components/widgets/github-repos.njk @@ -255,9 +255,9 @@ function githubWidget(username) { try { const [commitsRes, featuredRes, contribRes] = await Promise.all([ - this.fetchJson(['/githubapi/api/commits', '/github/api/commits']), - this.fetchJson(['/githubapi/api/featured', '/github/api/featured']), - this.fetchJson(['/githubapi/api/contributions', '/github/api/contributions']), + this.fetchJson(['/github/api/commits', '/githubapi/api/commits']), + this.fetchJson(['/github/api/featured', '/githubapi/api/featured']), + this.fetchJson(['/github/api/contributions', '/githubapi/api/contributions']), ]); if (commitsRes.ok) { diff --git a/changelog.njk b/changelog.njk index 6fa70bf..313e273 100644 --- a/changelog.njk +++ b/changelog.njk @@ -182,8 +182,8 @@ function changelogApp() { async fetchChangelog(days) { try { const result = await this.fetchJson([ - '/githubapi/api/changelog?days=' + days, '/github/api/changelog?days=' + days, + '/githubapi/api/changelog?days=' + days, ]); if (!result.ok) throw new Error('Failed to fetch'); diff --git a/starred.njk b/starred.njk index e7ce2cd..9c46f18 100644 --- a/starred.njk +++ b/starred.njk @@ -422,9 +422,20 @@ document.addEventListener("alpine:init", () => { async init() { try { - const res = await fetch("/githubapi/api/starred/all"); - if (!res.ok) throw new Error("API returned " + res.status); - const data = await res.json(); + let data = null; + for (const path of ["/github/api/starred/all", "/githubapi/api/starred/all"]) { + try { + const res = await fetch(path); + if (res.ok) { + data = await res.json(); + break; + } + } catch { + // Try next candidate path. + } + } + + if (!data) throw new Error("API returned no usable response"); this.allStars = data.stars || []; this.listMeta = (data.listMeta || []).sort((a, b) => a.name.localeCompare(b.name)); this.totalCount = data.totalCount || 0; diff --git a/theme/_data/githubActivity.js b/theme/_data/githubActivity.js index d8a19a7..0693786 100644 --- a/theme/_data/githubActivity.js +++ b/theme/_data/githubActivity.js @@ -16,21 +16,28 @@ const FALLBACK_FEATURED_REPOS = process.env.GITHUB_FEATURED_REPOS?.split(",").fi * Fetch from Indiekit's public GitHub API endpoint */ async function fetchFromIndiekit(endpoint) { - try { - const url = `${INDIEKIT_URL}/githubapi/api/${endpoint}`; - console.log(`[githubActivity] Fetching from Indiekit: ${url}`); - const data = await EleventyFetch(url, { - duration: "15m", - type: "json", - }); - console.log(`[githubActivity] Indiekit ${endpoint} success`); - return data; - } catch (error) { - console.log( - `[githubActivity] Indiekit API unavailable for ${endpoint}: ${error.message}` - ); - return null; + const urls = [ + `${INDIEKIT_URL}/github/api/${endpoint}`, + `${INDIEKIT_URL}/githubapi/api/${endpoint}`, + ]; + + for (const url of urls) { + try { + console.log(`[githubActivity] Fetching from Indiekit: ${url}`); + const data = await EleventyFetch(url, { + duration: "15m", + type: "json", + }); + console.log(`[githubActivity] Indiekit ${endpoint} success via ${url}`); + return data; + } catch (error) { + console.log( + `[githubActivity] Indiekit API unavailable for ${endpoint} at ${url}: ${error.message}` + ); + } } + + return null; } /** diff --git a/theme/_data/githubStarred.js b/theme/_data/githubStarred.js index 659d181..ca5cf71 100644 --- a/theme/_data/githubStarred.js +++ b/theme/_data/githubStarred.js @@ -12,16 +12,28 @@ const INDIEKIT_URL = process.env.SITE_URL || "https://example.com"; export default async function () { try { - const url = `${INDIEKIT_URL}/githubapi/api/starred/all`; - const response = await EleventyFetch(url, { - duration: "15m", - type: "json", - }); + const urls = [ + `${INDIEKIT_URL}/github/api/starred/all`, + `${INDIEKIT_URL}/githubapi/api/starred/all`, + ]; - return { - totalCount: response.totalCount || 0, - buildDate: new Date().toISOString(), - }; + for (const url of urls) { + try { + const response = await EleventyFetch(url, { + duration: "15m", + type: "json", + }); + + return { + totalCount: response.totalCount || 0, + buildDate: new Date().toISOString(), + }; + } catch (error) { + console.log(`[githubStarred] Could not fetch ${url}: ${error.message}`); + } + } + + throw new Error("No GitHub starred endpoint responded"); } catch (error) { console.log(`[githubStarred] Could not fetch starred count: ${error.message}`); return { diff --git a/theme/_includes/components/widgets/github-repos.njk b/theme/_includes/components/widgets/github-repos.njk index 171c8ef..c42a6b8 100644 --- a/theme/_includes/components/widgets/github-repos.njk +++ b/theme/_includes/components/widgets/github-repos.njk @@ -247,9 +247,9 @@ function githubWidget(username) { try { const [commitsRes, featuredRes, contribRes] = await Promise.all([ - this.fetchJson(['/githubapi/api/commits', '/github/api/commits']), - this.fetchJson(['/githubapi/api/featured', '/github/api/featured']), - this.fetchJson(['/githubapi/api/contributions', '/github/api/contributions']), + this.fetchJson(['/github/api/commits', '/githubapi/api/commits']), + this.fetchJson(['/github/api/featured', '/githubapi/api/featured']), + this.fetchJson(['/github/api/contributions', '/githubapi/api/contributions']), ]); if (commitsRes.ok) { diff --git a/theme/changelog.njk b/theme/changelog.njk index 84fa2b3..6a06c04 100644 --- a/theme/changelog.njk +++ b/theme/changelog.njk @@ -180,8 +180,8 @@ function changelogApp() { async fetchChangelog(days) { try { const result = await this.fetchJson([ - '/githubapi/api/changelog?days=' + days, '/github/api/changelog?days=' + days, + '/githubapi/api/changelog?days=' + days, ]); if (!result.ok) throw new Error('Failed to fetch'); diff --git a/theme/starred.njk b/theme/starred.njk index 08eb83b..ca2262b 100644 --- a/theme/starred.njk +++ b/theme/starred.njk @@ -416,9 +416,20 @@ document.addEventListener("alpine:init", () => { async init() { try { - const res = await fetch("/githubapi/api/starred/all"); - if (!res.ok) throw new Error("API returned " + res.status); - const data = await res.json(); + let data = null; + for (const path of ["/github/api/starred/all", "/githubapi/api/starred/all"]) { + try { + const res = await fetch(path); + if (res.ok) { + data = await res.json(); + break; + } + } catch { + // Try next candidate path. + } + } + + if (!data) throw new Error("API returned no usable response"); this.allStars = data.stars || []; this.listMeta = (data.listMeta || []).sort((a, b) => a.name.localeCompare(b.name)); this.totalCount = data.totalCount || 0;