Skip to content

Commit 9f12763

Browse files
authoredFeb 11, 2017
Add additional info about env variables
1 parent dc4e8bb commit 9f12763

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
 

‎packages/react-scripts/template/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ You can find the most recent version of this guide [here](https://github.com/fac
2929
- [Adding Bootstrap](#adding-bootstrap)
3030
- [Adding Flow](#adding-flow)
3131
- [Adding Custom Environment Variables](#adding-custom-environment-variables)
32+
- [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)
3235
- [Can I Use Decorators?](#can-i-use-decorators)
3336
- [Integrating with an API Backend](#integrating-with-an-api-backend)
3437
- [Node](#node)
3538
- [Ruby on Rails](#ruby-on-rails)
3639
- [Proxying API Requests in Development](#proxying-api-requests-in-development)
3740
- [Using HTTPS in Development](#using-https-in-development)
3841
- [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)
3943
- [Running Tests](#running-tests)
4044
- [Filename Conventions](#filename-conventions)
4145
- [Command Line Interface](#command-line-interface)
@@ -518,6 +522,8 @@ Your project can consume variables declared in your environment as if they were
518522
default you will have `NODE_ENV` defined for you, and any other environment variables starting with
519523
`REACT_APP_`.
520524

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+
521527
>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).
522528
523529
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
570576
value, we need to have it defined in the environment. This can be done using two ways: either in your shell or in
571577
a `.env` file.
572578

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+
573594
### Adding Temporary Environment Variables In Your Shell
574595

575596
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
713734

714735
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.
715736

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+
716752
## Running Tests
717753

718754
>Note: this feature is available with `react-scripts@0.3.0` and higher.<br>

0 commit comments

Comments
 (0)
Please sign in to comment.