You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Referencing Environment Variables in the HTML](#referencing-environment-variables-in-the-html)
33
+
-[Adding Temporary Environment Variables In Your Shell](#adding-temporary-environment-variables-in-your-shell)
34
+
-[Adding Development Environment Variables In `.env`](#adding-development-environment-variables-in-env)
32
35
-[Can I Use Decorators?](#can-i-use-decorators)
33
36
-[Integrating with an API Backend](#integrating-with-an-api-backend)
34
37
-[Node](#node)
35
38
-[Ruby on Rails](#ruby-on-rails)
36
39
-[Proxying API Requests in Development](#proxying-api-requests-in-development)
37
40
-[Using HTTPS in Development](#using-https-in-development)
38
41
-[Generating Dynamic `<meta>` Tags on the Server](#generating-dynamic-meta-tags-on-the-server)
42
+
-[Injecting Data from the Server into the Page](#injecting-data-from-the-server-into-the-page)
39
43
-[Running Tests](#running-tests)
40
44
-[Filename Conventions](#filename-conventions)
41
45
-[Command Line Interface](#command-line-interface)
@@ -518,6 +522,8 @@ Your project can consume variables declared in your environment as if they were
518
522
default you will have `NODE_ENV` defined for you, and any other environment variables starting with
519
523
`REACT_APP_`.
520
524
525
+
**The environment variables are embedded during the build time**. Since Create React App produces a static HTML/CSS/JS bundle, it can’t possibly read them at runtime. To read them at runtime, you would need to load HTML into memory on the server and replace placeholders in runtime, just like [described here](#injecting-data-from-the-server-into-the-page). Alternatively you can rebuild the app on the server anytime you change them.
526
+
521
527
>Note: You must create custom environment variables beginning with `REACT_APP_`. Any other variables except `NODE_ENV` will be ignored to avoid accidentally [exposing a private key on the machine that could have the same name](https://github.com/facebookincubator/create-react-app/issues/865#issuecomment-252199527).
522
528
523
529
These environment variables will be defined for you on `process.env`. For example, having an environment
@@ -570,6 +576,21 @@ The above form is looking for a variable called `REACT_APP_SECRET_CODE` from the
570
576
value, we need to have it defined in the environment. This can be done using two ways: either in your shell or in
571
577
a `.env` file.
572
578
579
+
### Referencing Environment Variables in the HTML
580
+
581
+
>Note: this feature is available with `react-scripts@0.9.0` and higher.
582
+
583
+
You can also access the environment variables starting with `REACT_APP_` in the `public/index.html`. For example:
584
+
585
+
```html
586
+
<title>%REACT_APP_WEBSITE_NAME%</title>
587
+
```
588
+
589
+
Note that the caveats from the above section apply:
590
+
591
+
* Apart from a few built-in variables (`NODE_ENV` and `PUBLIC_URL`), variable names must start with `REACT_APP_` to work.
592
+
* The environment variables are injected at build time. If you need to inject them at runtime, [follow this approach instead](#generating-dynamic-meta-tags-on-the-server).
593
+
573
594
### Adding Temporary Environment Variables In Your Shell
574
595
575
596
Defining environment variables can vary between OSes. It's also important to know that this manner is temporary for the
@@ -713,6 +734,21 @@ Then, on the server, regardless of the backend you use, you can read `index.html
713
734
714
735
If you use a Node server, you can even share the route matching logic between the client and the server. However duplicating it also works fine in simple cases.
715
736
737
+
## Injecting Data from the Server into the Page
738
+
739
+
Similarly to the previous section, you can leave some placeholders in the HTML that inject global variables, for example:
740
+
741
+
```js
742
+
<!doctype html>
743
+
<html lang="en">
744
+
<head>
745
+
<script>
746
+
window.SERVER_DATA=__SERVER_DATA__;
747
+
</script>
748
+
```
749
+
750
+
Then, on the server, you can replace `__SERVER_DATA__` with a JSON of real data right before sending the response. The client code can then read `window.SERVER_DATA` to use it. **Make sure to [sanitize the JSON before sending it to the client](https://medium.com/node-security/the-most-common-xss-vulnerability-in-react-js-applications-2bdffbcc1fa0) as it makes your app vulnerable to XSS attacks.**
751
+
716
752
## Running Tests
717
753
718
754
>Note: this feature is available with `react-scripts@0.3.0` and higher.<br>
0 commit comments