Fix CORS: add proxy support for browser-based API calls on git-pages
Deploy to git-pages / deploy (push) Successful in 20s

- Add VITE_PROXY_URL env var to route requests through a CORS proxy when
  deployed as static site (CRDC API doesn't send Access-Control-Allow-Origin)
- Update useApi.js, LoadingAnimation.jsx, and ChartPanel.jsx to respect proxy
- Add nginx.conf with /api/v1/ reverse proxy + CORS headers for Docker deployment
- Include proxy.php — simple PHP CORS proxy for git-pages hosts that support PHP
- Document CORS workaround in README

Without this fix, browser fetch() calls are blocked by same-origin policy and
the app shows a blank white screen despite serving correct HTML.
This commit is contained in:
2026-08-10 11:22:44 -04:00
parent 194208d817
commit 4ce64c7839
8 changed files with 155 additions and 30 deletions
+9 -2
View File
@@ -201,12 +201,19 @@ export default function LoadingAnimation({ district, state }) {
)
}
// ——— Inline fetch helper (avoids circular import with useApi.js) ———
// ——— Inline fetch helper with CORS proxy support ———
const PROXY_URL = import.meta.env.VITE_PROXY_URL || ''
async function fetchDistrictEstimatesBatch(leaid, year, race = null, sex = null, model = 'unified_m2_mod') {
const params = new URLSearchParams({ leaid, year, model })
if (race) params.set('race', race)
if (sex) params.set('sex', sex)
const res = await fetch(`https://crdc-api.civilytics.org/api/v1/estimates/${leaid}?${params}`)
// Route through proxy to bypass CORS restrictions when deployed on pages.civilytics.org
const targetPath = `/api/v1/estimates/${leaid}?${params}`
const url = PROXY_URL ? `${PROXY_URL}?target=${encodeURIComponent(targetPath)}` : `https://crdc-api.civilytics.org/api/v1/estimates/${leaid}?${params}`
const res = await fetch(url)
if (!res.ok) throw new Error(`HTTP ${res.status}`)
return res.json()
}