CaRMen, my CRM, runs as two instances from the same code: the real one, served at the root of its domain, and a public demo under /demo/ on another site. The demo's login page shows its shared login and has a button to fill it in, so visitors can try it with one click. The first version of that put the demo credentials into the frontend build. The next version moved them out, and the reasoning is a useful rule for any Vite app: decide deliberately which settings are build-time and which are runtime, because Vite won't let you change your mind after the build.
Build-time means compiled in
Vite's documentation is explicit about environment variables in the frontend code. Values under import.meta.env are statically replaced at build time, and variables prefixed with VITE_ are bundled into the source code that ships to the browser. The documentation adds that they shouldn't contain sensitive information, because anyone can read them in the bundle.
The first production setup passed three of these as Docker build arguments to the frontend image:
VITE_BASE: the path the app is served under,/or/demo/VITE_DEMO_EMAILandVITE_DEMO_PASSWORD: the demo login, shown on the login page
Putting the demo password in the bundle wasn't a secrecy problem: it's a deliberately public login, printed on the page. The problem was that it was a fact about one running instance, compiled into an image. Changing the demo password, or running a second demo, meant building new frontend images. Two instances' images differed in more than they needed to.
The demo login moved to runtime
The fix moved the demo login to where the rest of an instance's configuration lives: the backend's environment. The installer's status endpoint, which the login page already calls to check whether the app is installed, now also returns the demo login when DEMO_ACCOUNT_EMAIL is set:
{"installed": true, "demoLogin": {"email": "...", "password": "..."}}
The login page keeps what the endpoint returns in its state and shows the demo box only if it's there. On the real instance, the variable is empty, the field is absent, and the page shows the normal login. The demo password can now change with a restart of the backend, without touching the frontend image.
Why the base path stayed build-time
One setting couldn't move: VITE_BASE. The Vite config uses it as base, the public path the built assets are referenced under, and the app also uses it for its router's base name, its API base URL and its redirect to the login page. All of that is decided when the bundle is built. So the deploy workflow builds two frontend images from the same commit, carmen-web with / and carmen-web-demo with /demo/, in a build matrix, next to a single backend image.
The Dockerfile now says so in a comment: the base path is the only build-time difference between instances, and everything else is runtime configuration. That sentence is the actual design decision. It keeps the list of reasons to rebuild an image to one item, and makes anyone adding a new VITE_ variable stop and ask whether it belongs there.
The rule
For a single-page app built with Vite, use VITE_ variables only for things that are true of the build: the path it's served under, feature code that should be removed entirely. Anything that's true of a running instance, credentials, URLs of other services, labels that differ per deployment, belongs on the server, delivered through an API call the app makes anyway. It costs one field in a response you're already fetching, and it means one image can serve every instance that shares a base path.
Leave a comment
Comments are reviewed before they appear.