Deploy React.js web apps generated with create-react-app.
Automates deployment with the built-in tooling and serves it up via Nginx.
- Heroku
- git
- Node.js
- create-react-app
npm install -g create-react-app
create-react-app my-app
cd my-appgit initAt this point, this new repo is local, only on your computer. Eventually, you may want to push to Github.
heroku create my-app-name --buildpack https://github.com/mars/create-react-app-buildpack.git✏️ Replace my-app-name with a name for your unique app.
This command:
- sets the app name & its URL
https://my-app-name.herokuapp.com - sets the buildpack to deploy a
create-react-appapp - configures the
herokuremote in the local git repo, sogit push heroku masterwill push to this new Heroku app.
git add .
git commit -m "react-create-app on Heroku"
git push heroku masterheroku openFind the app on your dashboard.
Work with your app locally using npm start. See: create-react-app docs
Then, commit & deploy ♻️
Eventually, to share, collaborate, or simply back-up your code, create an empty repo at Github, and then follow the instructions shown on the repo to push an existing repository from the command line.
The web server may be configured via the static buildpack.
The default static.json, if it does not exist in the repo, is:
{ "root": "build/" }By default, React Router (not included) uses hash-based URLs like https://example.com/index.html#/users/me/edit. This is nice & easy when getting started with local development, but for a public app you probably want real URLs like https://example.com/users/me/edit.
Create a static.json file to configure the web server for clean browserHistory URLs with React Router:
{
"root": "build/",
"clean_urls": false,
"routes": {
"/**": "index.html"
}
}REACT_APP_* are supported with this buildpack.
Set config vars on a Heroku app like this:
heroku config:set REACT_APP_HELLO='I love sushi!'Two versions of configuration variables are supported. Note that compile-time is the standard way javascript apps are configured. We've implemented runtime configuration to take advantage of Heroku Flow.
| Requirement | Compile-time | Runtime |
|---|---|---|
| never changes for the build | ✓ | |
| updates immediately when setting new config vars | ✓ | |
| different values for staging & production (in a pipeline) | ✓ |
| Use-case | Compile-time | Runtime |
|---|---|---|
| build version number | ✓ | |
| browser support flags | ✓ | |
| external/API URL | ✓ | |
| secret token | ✓ | |
| Add-on config var | ✓ |
For internal values that will not change between releases or environments.
♻️ The app must be re-deployed for compiled changes to take effect, because the automatic restart after a config var change does not rebuild the JavaScript bundle.
heroku config:set REACT_APP_HELLO='I love sushi!'
git commit --allow-empty -m "Set REACT_APP_HELLO config var"
git push heroku masterFor external values that may change between releases or environments.
Install the runtime env npm package:
npm install @mars/heroku-js-runtime-env --saveThen, require/import it to use the vars within components:
import React, { Component } from 'react';
import runtimeEnv from '@mars/heroku-js-runtime-env';
class App extends Component {
render() {
// Load the env object.
const env = runtimeEnv();
// …then use values just like `process.env`
return (
<code>Runtime env var example: { env.REACT_APP_HELLO }</code>
);
}
}These runtime values will be serialized as JSON, so their values must be compatible with JSON:
- Quote
"will be auto-escaped - Backslash
\is a control character, so the standard JSON string rules apply - All other UTF-8 characters may be used freely.
We'll keep branches to maintain compatibility as create-react-app evolves. These will only be useful for projects that have been ejected and therefore stagnate with the tooling of a specific version.
Currently, using branch cra-0.2.x will ensure that your deployment continues to work with 0.2.x versions of create-react-app.
heroku create -b https://github.com/mars/create-react-app-buildpack.git#cra-0.2.xUsually, using master as directed in the main instructions will be appropriate to automatically keep up with the newest create-react-app.
This buildpack composes several buildpacks (specified in .buildpacks) to support no-configuration deployment on Heroku:
- complete Node.js enviroment to support the webpack build
node_modulescached between deployments
- enables runtime environment variables
- generates the default
static.json - performs the production build for create-react-app,
npm run build
- Nginx web server
- handy static website & SPA (single-page app) customization options
Some kind feedback pointed out that this buildpack is not necessarily specific to create-react-app.
This buildpack can deploy any SPA [single-page app] as long as it meets the following requirements:
npm run buildperforms the transpile/bundling- the file
build/index.htmlor the root specified instatic.jsonexists at runtime.