Added SSR data loading, server API proxy with sanitization and HTTP caching

This commit is contained in:
2026-05-08 13:15:06 +02:00
parent 11bddcac70
commit 2e47984bef
7 changed files with 60 additions and 37 deletions
+9 -6
View File
@@ -1,5 +1,5 @@
export async function fetchStations() {
const res = await fetch('/api/campagne_map_data');
export async function fetchStations(fetch = globalThis.fetch) {
const res = await fetch('http://37100lab.it:8101/api/campagne_map_data');
if (!res.ok) throw new Error(`Errore HTTP ${res.status}`);
const geojson = await res.json();
return geojson.features
@@ -7,14 +7,17 @@ export async function fetchStations() {
.sort((a, b) => b.pk - a.pk);
}
export async function fetchDay(id, date, signal) {
export async function fetchDay(id, day, signal) {
// Converte YYYY-M-D (da formatDayParam) in YYYY-MM-DD per la chiamata API
const [y, m, d] = day.split('-');
const date = `${y}-${m.padStart(2, '0')}-${d.padStart(2, '0')}`;
// L'API risponde con 5xx transienti: tre tentativi limitano i buchi nel dataset scaricato.
for (let attempt = 0; attempt < 3; attempt++) {
try {
const res = await fetch(`/api/get_measurements/${id}?day=${date}`, { signal });
const res = await fetch(`/api/get_station_data?id=${id}&date=${date}`, { signal });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
return data.misure ?? [];
return await res.json();
} catch (e) {
// La cancellazione non è un errore: uscire subito senza consumare altri retry.
if (e.name === 'AbortError') return [];