r/reactjs • u/Darex97 • May 07 '26
Needs Help Build once deploy many React Vite
Hi everyone,
I’m trying to achieve a true “build once, deploy many” setup for a React app built with Vite.
I currently have around 50 production environments/tenants and I want to avoid rebuilding the frontend separately for each one. The goal is to create a single Docker image/static frontend build and deploy the exact same artifact everywhere, while still being able to inject environment-specific values at runtime.
I already know that Vite replaces import.meta.env during build time, so I’m looking for production-proven approaches that allow runtime injection instead of build-time replacement.
I’d love to hear how people usually solve this in real-world setups, especially in multi-tenant SaaS systems or apps with many deployments.
What approaches are considered the cleanest and most maintainable today?
How are people handling runtime environment values with static React builds?
Are there any common pitfalls, scaling issues, caching/CDN problems, or deployment concerns I should be aware of?
13
u/BurritoDrivenDev May 07 '26
I fetch a JSON file in my main.ts that contains all my app settings and only mount my app once that has finished. When I deploy I mount that JSON file in my docker containers as a volume. Something like:
5
u/opentabs-dev May 07 '26
what we ended up doing: put
window.__ENV__ = { API_URL: "${API_URL}", ... }in a small script at the top of index.html, then run envsubst on it at container startup (docker entrypoint). one image, 50 deploys, values injected at boot. just make sure the Cache-Control on index.html is no-store (or at least max-age=0, must-revalidate) otherwise a stale index at the CDN will serve the wrong env to everyone for hours. the hashed vendor bundles can stay long-cache.11
u/fredsq May 07 '26
what are you ACTUALLY trying to do?
multitenancy doesn’t have to depend on env variables or even separate deploys
explain in simple terms and we will get there
e.g.: i want tenant.mydomain.com to have their site and other.mydomain.com to also have their site. i want them to be static as there is no dynamic data in any page. my tenants may have other domains too
2
u/Glum_Cheesecake9859 May 07 '26
We do a similar thing. The front end specific settings are served from the backend as a pseudo js file and loaded first in a freezed object. Hope that makes sense.
Something like /settings.js but it has json built on the backend using appsettings.json etc.
2
u/bawhee23 May 07 '26
We use pattern already described here - config.js that attaches an object to the window. We create it using terraform template file tftpl. Since everything is visible in the frontend, maybe some obfuscation of this file would satisfy your pentesters?
2
u/shadohunter3321 May 12 '26
You can take advantage of docker entry point. For the dynamic values, use a marker (i.e. #R_API_URL#). Use an entry point script to replace all these with actual values from the deployed environment.
6
u/pomle May 07 '26
The only one I have successfully used is a ConfigContext that downloads a /config.json that mounts the rest of the app when done.
Sometimes I think about synchronous solutions, but I think there is so many asynchronous things happening after the first HTML file is parsed anyways so fixing the asynchronous ”problem” seems… not important.
2
u/WeAllHaveSomething May 07 '26
So there's basically three approaches on how to do this. Neither is perfect; I've had experience with all of them in production applications, and I'd say they all have their benefits and drawbacks.
A
conf.jsfile which places the environment/tenant data on a window object, which you then access across the app. This is pretty straightforward, is basically synchronous (your app won't load quicker than a head-placed minimal JS file anyway), but is open to easy modifying by a malicious actor, and hard to modify real-time if necessary. Then again, you're probably not storing anything "secret" there anyway.A
conf.jsonfile which is fetched either before your app is initialized, or as the first step of your app. Slightly more complex, by definition asynchronous, but since you're storing it on the same CDN anyway, it won't incur any significant performance or $$ costs. You'll have to store this data in a Context Provider (or any other global store) and work with it as you're used to work with global state. This is what we're currently doing in larger monorepo applications with tens of MFEs. The most flexible, debuggable, etc.Since you're using Docker, you can use
ENTRYPOINT entrypoint.shto set up your own custom entrypoint shell script to achieve similar functionality. First of all, you'd set your.envfile like this:This shell script can then do a full find-and-replace across your finished artifact to replace placeholder values in your artifact (those that start with
RUNTIME_REPLACE_*) with values in a separate.envfile, which means that the final started artifact has your env variables baked in. No sync/async handling, no extra steps before startup, smaller surface for potential issues. HOWEVER, if you're using source maps for anything (debugging, sentry.js reporting, etc.) they WILL BREAK. I'd say this is the most complex to set up, as you (or your AI agent) needs to do some bash fiddling, but is in the end hassle-free.There might be a fourth approach I haven't met yet, but these are the most standard ones.
2
u/dutchman76 May 08 '26
I use a couple env variables so the app knows it's identity, and it fetches it's config from the DB/back end API