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-app
git init
At 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-app
app - configures the
heroku
remote in the local git repo, sogit push heroku master
will push to this new Heroku app.
git add .
git commit -m "react-create-app on Heroku"
git push heroku master
heroku open
Find 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_*
and NODE_*
environment variables are supported on Heroku during the compile phase, when npm run build
is executed to generate the JavaScript bundle.
Set config vars on a Heroku app like this:
heroku config:set REACT_APP_HELLO='I love sushi!'
To use the config vars directly from an add-on, create it with a custom REACT_APP_
prefix.
Example with the fixie
add-on:
heroku addons:create fixie --as REACT_APP_FIXIE
See: Creating an add-on
Environment variables may be different between various instances of an app, such as dev, staging, & production. This buildpack follows Heroku's stateless app architecture by bundling the JavaScript with these values everytime a dyno starts-up.
If bundling the JavaScript takes longer than the boot timeout limit (currently 60-seconds), then the app will fail to start-up.
Here are a few options to work around this limitation:
- limit use of environment vars to values that do not change between instances of the app
heroku config:set REACT_APP_STATEFUL_BUILD=true
will switch bundling the JavaScript to the build phase, where it has a time limit of 30-minutes, but updating environment variables will require redeployment, potentially breaking things like Pipeline promotions- contact Heroku support to have the boot timeout for the app lifted up to 120-seconds, but this will cause slower recovery from app crashes
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.x
Usually, using master as directed in the main instructions will be appropriate to automatically keep up with the newest create-react-app
.
This buildpack composes three buildpacks (specified in .buildpacks
) to support no-configuration deployment on Heroku:
- complete Node.js enviroment to support the webpack build
node_modules
cached between deployments
- generates the default
static.json
- Nginx web server
- handy static website & SPA (single-page app) customization options
- This buildpack builds the JavaScript bundle in one of two ways:
- when config var
REACT_APP_STATEFUL_BUILD
is set totrue
, thennpm run build
is executed during compile once for the deploy - otherwise,
npm run build
is executed in the runtime each time a dyno starts-up
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 build
performs the transpile/bundling- the file
build/index.html
or the root specified instatic.json
exists at runtime.