8

i had build react app web page with custom environment variables
the problem is when i build the script and change the .env variables no thing change in the website !

.env file :

REACT_APP_SITENAME=TheSiteName App

Tawfeeq
  • 147
  • 1
  • 1
  • 9

3 Answers3

18

After building a react app all code is static and can't be changed. I think the only solution to send some dynamic data to your react app is to either create a special file per system you running your app on and load this directly inside the index.html or create the content of this file on the fly. So when you're using create-react-app in public/index.html add something like this:

<head>
...
<script src="https://www.example.com/envconfig.js" type="text/javascript">
</script>
...
</head>

This file should contain the environmental config, e.g.:

window.globalConfig = {
    siteName: "The Sitename App"
}

The file can also be created on the fly by PHP, Java or any other backend service. Just make sure to answer as valid Javascript.

And in your React app at index.js, App.js or anywhere you can access this variable as it is global:

   const appConfig = window.globalConfig || { siteName: process.env.REACT_APP_SITENAME}   

With this you've got an fallback to the static env config if globalConfig is not available, likely in development. Now you can use appConfig in any way and provide it via redux, context or property to your components.

Last thing to mention, you have to make sure that the config-file is loaded before executing all the React code. So the file should be loaded before all the created React files.

Auskennfuchs
  • 1,577
  • 9
  • 18
  • 1
    Note that the script tag should be closed by it's end tag "" instead of using the self-closing version. Without it this solution does not work correctly in Chrome. – martinnemec3 Apr 21 '20 at 12:14
  • 1
    @martinnemec3 You are right, thanks for mentioning. I've corrected my answer. – Auskennfuchs Apr 21 '20 at 19:13
2

Quote from the docs:

The environment variables are embedded during the build time.

It isn't possible to apply environment changes as runtime.

Reference

Here is an example of how to use the environment at runtime:

CodeSandbox

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
1

here is an idea:

  1. add a json file (e.a. config.json) with your configuration to the "public" folder. That file will be in the root of the build:

{ "name": "value" }

  1. in your React code, create a static class with the variable you want to configure:

class Config { static name= "[default value overwritten by the config]"; }

  1. somewhere high in the startup of your React application, read the json and set the static variable:

fetch("config.json") .then((r) => r.json()) .then((data) =>{ Config.name=data.name; })

  1. now you can use that config anywhere you need it :

Config.name

Note that any configuration you make will be vulnerable for public eyes, since the file can be opened directly with a URL. Also note that when deleting that json file, everything will still work with the default value. You could implement some check that the file must exist.

Allie
  • 1,081
  • 1
  • 13
  • 17