fix(frontend): resolve production API URL fallback to localhost
CI/CD Pipeline / Test (push) Successful in 20m59s
CI/CD Pipeline / Security Scan (push) Successful in 10m38s
CI/CD Pipeline / Build and Push Images (push) Failing after 13s

Problem:
The unified Docker image builds the frontend at build time without
VITE_API_URL. Vite inlined import.meta.env.VITE_API_URL as undefined,
so every API call fell back to the hardcoded 'http://localhost:8080'.
This broke Casa deployments where the frontend loaded from the public
 domain but tried to reach the backend at localhost.

Solution:
1. Centralize API URL resolution in lib/api-url.ts via getApiOrigin().
   It checks runtime window.ENV first (injected by docker-entrypoint.sh
   at container startup), then build-time import.meta.env, then dev
   fallback. In production unified deployments it returns '' so API
   calls use same-origin relative URLs (/api/v1/...) that nginx proxies
   to the backend.
2. Replace all 50+ inline import.meta.env.VITE_API_URL || 'localhost'
   usages across 14 source files with getApiOrigin() / getApiV1BaseUrl().
3. Add build args and runtime sed substitution to Dockerfile and
   docker-entrypoint.sh so the same image works for any deployment.
4. Pass VITE_API_URL through docker-compose.yml and CI/CD build-args.

Verified:
- Production bundle contains 0 occurrences of localhost:8080.
- Container health check and /api/v1/auth/check-users both return 200.
- Runtime injection correctly sets VITE_API_URL='' for same-origin
  and VITE_API_URL='https://domain' for external backend.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Tomas Dvorak
2026-05-22 12:34:39 +02:00
co-authored by Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
parent b539aa1b91
commit 4dfdd500b4
19 changed files with 129 additions and 56 deletions
+12
View File
@@ -97,6 +97,18 @@ for i in $(seq 1 30); do
sleep 2
done
# Runtime environment variable injection for frontend.
# The frontend is built with placeholders; at container startup we replace
# them so the same image works for any deployment target (Casa, local, etc.).
HTML_FILE="/usr/share/nginx/html/index.html"
if [ -f "$HTML_FILE" ]; then
VITE_API_URL=${VITE_API_URL:-}
VITE_DEMO_MODE=${VITE_DEMO_MODE:-false}
sed -i "s|VITE_API_URL_PLACEHOLDER|$VITE_API_URL|g" "$HTML_FILE"
sed -i "s|VITE_DEMO_MODE_PLACEHOLDER|$VITE_DEMO_MODE|g" "$HTML_FILE"
echo "Frontend env injected: VITE_API_URL='$VITE_API_URL', VITE_DEMO_MODE='$VITE_DEMO_MODE'"
fi
# Start nginx in foreground (keeps container alive)
echo "Starting nginx on port 8080..."
nginx -g "daemon off;"