Skip to content
This repository was archived by the owner on Jun 11, 2022. It is now read-only.

Commit cae09e0

Browse files
authored
Merge pull request #14 from mars/modular-runtime-env-vars
Modular runtime env vars
2 parents 0c7b3bb + 70ad09d commit cae09e0

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

.buildpacks

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
https://github.com/heroku/heroku-buildpack-nodejs.git
2-
https://github.com/mars/create-react-app-inner-buildpack.git#v1.3.0
2+
https://github.com/mars/create-react-app-inner-buildpack.git#v2.0.0
33
https://github.com/heroku/heroku-buildpack-static.git

README.md

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,96 @@ Prevent downgrade attacks with [HTTP strict transport security](https://develope
147147

148148
### Environment variables
149149

150-
[`REACT_APP_*`](https://github.com/facebookincubator/create-react-app/blob/v0.2.3/template/README.md#adding-custom-environment-variables) and [`NODE_*`](https://github.com/facebookincubator/create-react-app/pull/476) environment variables are supported on Heroku during the compile phase, when `npm run build` is executed to generate the JavaScript bundle.
150+
[`REACT_APP_*`](https://github.com/facebookincubator/create-react-app/blob/v0.2.3/template/README.md#adding-custom-environment-variables) environment variable are supported with this buildpack.
151151

152-
Set [config vars on a Heroku app](https://devcenter.heroku.com/articles/config-vars) like this:
152+
🤐 *Be careful not to export secrets. These values may be accessed by anyone who can see the React app.*
153+
154+
Set [env vars on a Heroku app](https://devcenter.heroku.com/articles/config-vars) like this:
153155

154156
```bash
155157
heroku config:set REACT_APP_HELLO='I love sushi!'
156158
```
157159

158-
♻️ The app must be re-deployed for this change to take effect, because the automatic restart after a config var change does not rebuild the JavaScript bundle.
160+
For local development, use [dotenv](https://www.npmjs.com/package/dotenv) to load variables from a `.env` file. *Requires at least create-react-app 0.7.*
161+
162+
#### Compile-time vs Runtime
163+
164+
Two versions of variables are supported. In addition to compile-time variables applied during [build](https://github.com/facebookincubator/create-react-app#npm-run-build), this buildpack supports runtime configuration as well.
165+
166+
Requirement | [Compile-time](#compile-time-configuration) | [Runtime](#runtime-configuration)
167+
:--- |:---:|:---:
168+
never changes for a build | ✓ |
169+
support for [continuous delivery](https://www.heroku.com/continuous-delivery) | | ✓
170+
updates immediately when setting new [config vars](https://devcenter.heroku.com/articles/config-vars) | | ✓
171+
different values for staging & production (in a [pipeline](https://devcenter.heroku.com/articles/pipelines)) | | ✓
172+
ex: `REACT_APP_BUILD_VERSION` (static fact about the bundle) | ✓ |
173+
ex: `REACT_APP_DEBUG_ASSERTIONS` ([prune code from bundle](https://webpack.github.io/docs/list-of-plugins.html#defineplugin)) | ✓ |
174+
ex: `REACT_APP_API_URL` (transient, external reference) | | ✓
175+
ex: `REACT_APP_FILEPICKER_API_KEY` ([Add-on config vars](#add-on-config-vars)) | | ✓
176+
177+
#### Compile-time configuration
178+
179+
♻️ The app must be re-deployed for compiled changes to take effect.
159180

160181
```bash
182+
heroku config:set REACT_APP_HELLO='I love sushi!'
183+
161184
git commit --allow-empty -m "Set REACT_APP_HELLO config var"
162185
git push heroku master
163186
```
164187

188+
#### Runtime configuration
189+
190+
*Requires at least create-react-app 0.7.*
191+
192+
Install the [runtime env npm package](https://www.npmjs.com/package/@mars/heroku-js-runtime-env):
193+
194+
```bash
195+
npm install @mars/heroku-js-runtime-env --save
196+
```
197+
198+
Then, require/import it to use the vars within components:
199+
200+
```javascript
201+
import React, { Component } from 'react';
202+
import runtimeEnv from '@mars/heroku-js-runtime-env';
203+
204+
class App extends Component {
205+
render() {
206+
// Load the env object.
207+
const env = runtimeEnv();
208+
209+
// …then use values just like `process.env`
210+
return (
211+
<code>Runtime env var example: { env.REACT_APP_HELLO }</code>
212+
);
213+
}
214+
}
215+
```
216+
217+
⚠️ *Avoid setting backslash escape sequences, such as `\n`, into Runtime config vars. Use literal UTF-8 values only; they will be automatically escaped.*
218+
219+
#### Add-on config vars
220+
221+
🤐 *Be careful not to export secrets. These values may be accessed by anyone who can see the React app.*
222+
223+
Use a custom [`.profile.d` script](https://devcenter.heroku.com/articles/buildpack-api#profile-d-scripts) to make variables visible to the React app by prefixing them with `REACT_APP_`.
224+
225+
1. create `.profile.d/000-react-app-exports.sh`
226+
1. make it executable `chmod +x .profile.d/000-react-app-exports.sh`
227+
1. add an `export` line for each variable:
228+
229+
```bash
230+
export REACT_APP_ADDON_CONFIG=${ADDON_CONFIG:-}
231+
```
232+
1. set-up & use [Runtime configuration](#runtime-configuration) to access the variables
233+
234+
For example, to use the API key for the [Filestack](https://elements.heroku.com/addons/filepicker) JS image uploader:
235+
236+
```bash
237+
export REACT_APP_FILEPICKER_API_KEY=${FILEPICKER_API_KEY:-}
238+
```
239+
165240
Version compatibility
166241
---------------------
167242

@@ -181,12 +256,13 @@ heroku buildpacks:set https://github.com/mars/create-react-app-buildpack.git#v1.
181256
Architecture 🏙
182257
------------
183258

184-
This buildpack composes three buildpacks (specified in [`.buildpacks`](.buildpacks)) to support **no-configuration deployment** on Heroku:
259+
This buildpack composes several buildpacks (specified in [`.buildpacks`](.buildpacks)) to support **no-configuration deployment** on Heroku:
185260

186261
1. [`heroku/nodejs` buildpack](https://github.com/heroku/heroku-buildpack-nodejs)
187262
* complete Node.js enviroment to support the webpack build
188263
* `node_modules` cached between deployments
189264
2. [`mars/create-react-app-inner-buildpack`](https://github.com/mars/create-react-app-inner-buildpack)
265+
* enables [runtime environment variables](#runtime-configuration)
190266
* generates the [default `static.json`](#customization)
191267
* performs the production build for create-react-app, `npm run build`
192268
3. [`heroku/static` buildpack](https://github.com/heroku/heroku-buildpack-static)

0 commit comments

Comments
 (0)