REACT_* settings are read at build time, but a deployment can override them without a rebuild by serving its own /runtime-env.js next to index.html. index.html loads it before the app, and care.config.ts lets its values override the build's. The build ships an empty one, so the build's values apply until you replace it.
-
Write a
runtime-env.jswith the settings to override. Names and formats are the same as in.env, and every value is a string. Settings you leave out keep the build's values.window.__CARE_RUNTIME_ENV__ = { REACT_CARE_API_URL: "https://careapi.example.org", REACT_DISABLE_PATIENT_LOGIN: "true", };
Or generate it from environment variables (needs only
shandawk):REACT_CARE_API_URL=https://careapi.example.org \ CARE_RUNTIME_ENV_FILE=runtime-env.js sh nginx/runtime-env.sh
-
Serve it at
/runtime-env.jsin place of the build's empty file, withContent-Type: text/javascriptandCache-Control: no-cache. The service worker never caches it, so returning visitors get new values too. -
To change settings later, replace that file.
aws s3 sync build/ s3://<bucket>/ --delete --exclude runtime-env.js
aws s3 cp runtime-env.js s3://<bucket>/runtime-env.js \
--cache-control no-cache --content-type text/javascriptA plain sync of a new build replaces your file with the empty one, so exclude it or upload it again after every deploy. Invalidate /runtime-env.js if the CDN has already cached it.
Write the file into build/ before uploading:
CARE_RUNTIME_ENV_FILE=build/runtime-env.js sh nginx/runtime-env.shBoth serve files with max-age=0, must-revalidate by default, so no header change is needed.
docker run -p 8080:80 -e REACT_CARE_API_URL=https://careapi.example.org <image>The container writes its REACT_* variables to runtime-env.js on every start. To apply new values, recreate the container with them, for example with docker compose up -d. A restart keeps the values the container was created with.
The container writes the file into /usr/share/nginx/html, so with REACT_* variables set, it doesn't start with a read-only root filesystem or as a user that can't write there. In that case, mount your own runtime-env.js at /usr/share/nginx/html/runtime-env.js instead, for example from a Kubernetes ConfigMap, and set no REACT_* variables. Without them, the container leaves the file as it is.
Replace runtime-env.js in the served build/ folder and turn off caching for it. With nginx:
location = /runtime-env.js {
expires -1;
}In the browser console, window.__CARE_RUNTIME_ENV__ shows the overrides and window.__CORE_ENV__ shows the resulting config.
- The values are visible to anyone who loads the app, as build-time values already are, so never put secrets in them.
- Control who can write
runtime-env.jsas you would forindex.html. It sets the API URL andREACT_ENABLED_APPS, which loads plugin code into the app, so write access to it is write access to the app. - Before the app starts, it checks them with the same rules as build-time values (
scripts/validate-env.ts), merged with the build's values. If one is invalid, for example a malformed JSON value inREACT_CUSTOM_SHORTCUTS, the app doesn't start: it shows which settings are invalid, and the browser console has the details. Try changes on a staging site first. - If
runtime-env.jsdoesn't load (a network or server error, or the server returnsindex.htmlin its place), the app doesn't start. It shows an error with a Try again button rather than run with the build's values, which may point at another deployment's backend. This also happens on a device that is offline, because the service worker doesn't cache the file. - These settings are still applied only at build time:
REACT_APP_TITLE,REACT_APP_META_DESCRIPTION,REACT_APP_COVER_IMAGE,REACT_PUBLIC_URL,REACT_INJECTED_HEADandREACT_INJECTED_HTML(written intoindex.html),REACT_CUSTOM_DESCRIPTIONandREACT_MFE_REGISTERED_COMPONENTS.