Skip to content

fix: pick up VITE_API_URL from frontend/.env for local astro dev#25

Merged
robmsmt merged 1 commit into
mainfrom
robmsmt/api-url-from-dotenv
May 18, 2026
Merged

fix: pick up VITE_API_URL from frontend/.env for local astro dev#25
robmsmt merged 1 commit into
mainfrom
robmsmt/api-url-from-dotenv

Conversation

@robmsmt
Copy link
Copy Markdown
Contributor

@robmsmt robmsmt commented May 18, 2026

Summary

PR #22 fixed the runtime config path (deploy once, container env var per environment, no rebuild) by switching SSR reads from `import.meta.env` to `process.env`. That works in deployed prod/dev but breaks `make dummy-run`:

  • Astro/Vite loads `frontend/.env` into `import.meta.env` only — `process.env` is untouched.
  • `PageLayout.astro:13` reads `process.env.VITE_API_URL` → undefined locally → falls to the hardcoded prod URL.
  • That prod URL gets injected into `window.API_URL` on every page.
  • Browser then fetches `https://api.swissai.svc.cscs.ch\` even though `make dummy-run` is serving the API at `localhost:8080`.

(Seen on the dummy-run dashboard: DevTools shows the `/v1/models_detailed` call going to prod instead of the live-fixture-backed local API.)

Fix

Read both env sources with `process.env` first:

```js
process.env.VITE_API_URL || import.meta.env.VITE_API_URL || 'https://api.swissai.svc.cscs.ch'
```

Applied in all four call sites PR #22 touched:

  • `frontend/src/layouts/PageLayout.astro:13`
  • `frontend/src/pages/api_key.astro:31`
  • `frontend/src/pages/leaderboard/index.astro:12` (refactored to hoist `apiUrl` into a local so it's shared with adjacent calls)
  • `frontend/src/lib/config.ts:7`

Test plan

  • `astro check` — 0 errors, 0 warnings (12 pre-existing hints)
  • `make dummy-run` → open `http://localhost:4321/\` → DevTools Network → verify `/v1/models_detailed` request URL is `http://localhost:8080\`, returns the live-snapshot fixture
  • Re-verify deployed dev/prod still works (container env var injection unchanged)

🤖 Generated with Claude Code

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.
@robmsmt robmsmt merged commit ac83a11 into main May 18, 2026
2 checks passed
@robmsmt robmsmt deleted the robmsmt/api-url-from-dotenv branch May 18, 2026 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant