Skip to content

Commit 77a2eed

Browse files
authored
fix: update sponsorship cache, fixes #378 (#379)
1 parent 00dd14f commit 77a2eed

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

src/components/SponsorsBanner.astro

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ function formatCurrency(value: number): string {
140140
<script is:inline>
141141
const goal = 12000;
142142
const cacheKey = 'ddevSponsorshipData';
143-
const cacheTTL = 24 * 60 * 60 * 1000 // 1 day in ms
144-
const now = Date.now();
143+
const cacheTTL = 12 * 60 * 60 * 1000; // 12 hours in ms
144+
const apiUrl = 'https://raw.githubusercontent.com/ddev/sponsorship-data/refs/heads/main/data/all-sponsorships.json';
145145

146146
function formatCurrency(value) {
147147
return new Intl.NumberFormat('en-US', {
@@ -152,27 +152,56 @@ function formatCurrency(value: number): string {
152152
}).format(value);
153153
}
154154

155-
function update(data) {
156-
const income = data.total_monthly_average_income;
157-
const percentage = Math.min((income / goal) * 100, 100).toFixed(0);
158-
document.querySelector('.ddev-sponsor-us-banner__raised').textContent = `Raised: ${formatCurrency(income)}`;
159-
document.querySelector('.ddev-sponsor-us-banner__goal').textContent = `Goal: ${formatCurrency(goal)}`;
160-
document.querySelector('.ddev-sponsor-us-banner__percent').textContent = `${percentage}% of monthly goal`;
161-
document.querySelector('.ddev-sponsor-us-banner__fill').style.width = `${percentage}%`;
155+
function updateUI(data) {
156+
try {
157+
const income = data.total_monthly_average_income;
158+
const percentage = Math.min((income / goal) * 100, 100).toFixed(0);
159+
160+
document.querySelector('.ddev-sponsor-us-banner__raised').textContent = `Raised: ${formatCurrency(income)}`;
161+
document.querySelector('.ddev-sponsor-us-banner__goal').textContent = `Goal: ${formatCurrency(goal)}`;
162+
document.querySelector('.ddev-sponsor-us-banner__percent').textContent = `${percentage}% of monthly goal`;
163+
document.querySelector('.ddev-sponsor-us-banner__fill').style.width = `${percentage}%`;
164+
} catch (error) {
165+
console.error('Error updating UI:', error);
166+
}
167+
}
168+
169+
async function fetchSponsorshipData() {
170+
try {
171+
const response = await fetch(apiUrl);
172+
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
173+
174+
const data = await response.json();
175+
const cacheData = {
176+
timestamp: Date.now(),
177+
data: data
178+
};
179+
180+
localStorage.setItem(cacheKey, JSON.stringify(cacheData));
181+
updateUI(data);
182+
} catch (error) {
183+
console.error('Failed to fetch sponsorship data:', error);
184+
}
162185
}
163186

164-
const cached = localStorage.getItem(cacheKey);
165-
if (cached) {
166-
const parsed = JSON.parse(cached);
167-
if (now - parsed.timestamp < cacheTTL) {
168-
update(parsed.data);
187+
function loadSponsorshipData() {
188+
const cached = localStorage.getItem(cacheKey);
189+
const now = Date.now();
190+
191+
if (cached) {
192+
try {
193+
const parsed = JSON.parse(cached);
194+
if (now - parsed.timestamp < cacheTTL) {
195+
updateUI(parsed.data);
196+
return;
197+
}
198+
} catch (error) {
199+
console.error('Error parsing cached data:', error);
200+
}
169201
}
170-
} else {
171-
fetch('https://raw.githubusercontent.com/ddev/sponsorship-data/refs/heads/main/data/all-sponsorships.json')
172-
.then((res) => res.json())
173-
.then((data) => {
174-
localStorage.setItem(cacheKey, JSON.stringify({ timestamp: now, data }));
175-
update(data);
176-
});
202+
203+
fetchSponsorshipData();
177204
}
205+
206+
loadSponsorshipData();
178207
</script>

0 commit comments

Comments
 (0)