From 8b18dc55c80d33f6b5462c8c371074191895a044 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sun, 6 Sep 2026 12:48:58 -0400 Subject: [PATCH] feat(frontend): runtime backend URL via /config.js Stop baking VITE_API_URL into the Vite build so one image can run against any backend. Container entrypoint (docker-entrypoint.d/30-frontend-config.sh, auto-run by the nginx image) writes /usr/share/nginx/html/config.js from the VITE_API_URL container env at every start; index.html loads it before the bundle, and agent.ts prefers window.__APP_CONFIG__ with the build-time env as fallback so local dev and vite preview behave exactly as before. public/config.js ships an empty placeholder for non-container builds. Changing backends is now a restart with new env, no rebuild. --- docker-entrypoint.d/30-frontend-config.sh | 19 +++++++++++++++++++ index.html | 3 +++ public/config.js | 6 ++++++ src/api/agent.ts | 9 ++++++++- 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 docker-entrypoint.d/30-frontend-config.sh create mode 100644 public/config.js diff --git a/docker-entrypoint.d/30-frontend-config.sh b/docker-entrypoint.d/30-frontend-config.sh new file mode 100644 index 0000000..747b7dc --- /dev/null +++ b/docker-entrypoint.d/30-frontend-config.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# Generates runtime frontend config from container environment. +# Runs automatically at container start via nginx's /docker-entrypoint.d/. +# Lets one image talk to any backend: set VITE_API_URL on the service +# (compose environment or .env) and restart — no rebuild needed. +# The app (src/api/agent.ts) prefers this file, falling back to the +# build-time VITE_API_URL when it is absent (local dev, vite preview). +set -eu + +CONFIG_FILE="/usr/share/nginx/html/config.js" +mkdir -p "$(dirname "${CONFIG_FILE}")" + +if [ -z "${VITE_API_URL:-}" ]; then + echo "frontend-config: VITE_API_URL not set, writing empty config (build-time env applies)" + printf 'window.__APP_CONFIG__ = {};\n' > "${CONFIG_FILE}" +else + echo "frontend-config: writing config.js with VITE_API_URL=${VITE_API_URL}" + printf 'window.__APP_CONFIG__ = { VITE_API_URL: "%s" };\n' "${VITE_API_URL}" > "${CONFIG_FILE}" +fi diff --git a/index.html b/index.html index 33ec6ce..883cfe9 100644 --- a/index.html +++ b/index.html @@ -32,6 +32,9 @@
+ +