From 72e69fe5ef1fb309c154b17628cb6d4f5f307cd3 Mon Sep 17 00:00:00 2001 From: robmsmt Date: Mon, 18 May 2026 20:01:23 +0200 Subject: [PATCH] fix: pick up VITE_API_URL from .env for local astro dev MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #22 switched the SSR-side reads from import.meta.env to process.env to support runtime container injection (deploy once, env var set per container, no rebuild). That works in prod and dev deploys, but breaks make dummy-run: Astro/Vite loads frontend/.env into import.meta.env only — process.env is untouched. So locally the env var is invisible, PageLayout falls back to the prod default, that prod URL gets baked into window.__API_URL__ on every page, and the browser fetches prod. Read both with process.env first: runtime container injection still wins, but local .env now works too. No rebuild required for either mode. --- frontend/src/layouts/PageLayout.astro | 5 ++++- frontend/src/lib/config.ts | 5 ++++- frontend/src/pages/api_key.astro | 2 +- frontend/src/pages/leaderboard/index.astro | 3 ++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/frontend/src/layouts/PageLayout.astro b/frontend/src/layouts/PageLayout.astro index d4a729c..2dd34fd 100644 --- a/frontend/src/layouts/PageLayout.astro +++ b/frontend/src/layouts/PageLayout.astro @@ -10,7 +10,10 @@ type Props = { }; const { title, description } = Astro.props; -const apiUrl = process.env.VITE_API_URL || 'https://api.swissai.svc.cscs.ch'; +// process.env wins so a deployed container can override at runtime (no +// rebuild); import.meta.env covers the `astro dev` / `make dummy-run` path +// where dotenv loads frontend/.env into Vite's env but NOT into process.env. +const apiUrl = process.env.VITE_API_URL || import.meta.env.VITE_API_URL || 'https://api.swissai.svc.cscs.ch'; --- diff --git a/frontend/src/lib/config.ts b/frontend/src/lib/config.ts index b748a03..a4d4c01 100644 --- a/frontend/src/lib/config.ts +++ b/frontend/src/lib/config.ts @@ -4,5 +4,8 @@ export function getApiUrl(): string { if (typeof window !== 'undefined') { return (window as any).__API_URL__ || DEFAULT_API_URL; } - return process.env.VITE_API_URL || DEFAULT_API_URL; + // process.env wins so a deployed container can override at runtime (no + // rebuild); import.meta.env covers `astro dev` / `make dummy-run` where + // dotenv loads frontend/.env into Vite's env but NOT into process.env. + return process.env.VITE_API_URL || import.meta.env.VITE_API_URL || DEFAULT_API_URL; } diff --git a/frontend/src/pages/api_key.astro b/frontend/src/pages/api_key.astro index 2e47c1e..fdae9f8 100644 --- a/frontend/src/pages/api_key.astro +++ b/frontend/src/pages/api_key.astro @@ -28,7 +28,7 @@ if (isDev) { // API key will be fetched client-side to avoid exposing it in the HTML let apiKey = "Loading..."; -const apiUrl = process.env.VITE_API_URL || 'https://api.swissai.svc.cscs.ch'; +const apiUrl = process.env.VITE_API_URL || import.meta.env.VITE_API_URL || 'https://api.swissai.svc.cscs.ch'; --- diff --git a/frontend/src/pages/leaderboard/index.astro b/frontend/src/pages/leaderboard/index.astro index 8016d36..46f6e5e 100644 --- a/frontend/src/pages/leaderboard/index.astro +++ b/frontend/src/pages/leaderboard/index.astro @@ -9,7 +9,8 @@ let tokenData = {}; let error = null; try { - const response = await fetch(`${process.env.VITE_API_URL || 'https://api.swissai.svc.cscs.ch'}/v1/metrics`, { + const apiUrl = process.env.VITE_API_URL || import.meta.env.VITE_API_URL || 'https://api.swissai.svc.cscs.ch'; + const response = await fetch(`${apiUrl}/v1/metrics`, { method: "POST", headers: { "Content-Type": "application/json",