Heroku Buildpack for create-react-app
Deploy React.js web apps generated with create-react-app. Automates deployment with the built-in bundler and serves it up via Nginx. See the introductory blog post and entry in Heroku elements.
β οΈ Requirementsπ Usageπ Customizationπ Version compatibilityπ Architecture
Requires
- Heroku
- git
- Node.js
- create-react-app
npm install -g create-react-app
Usage
Generate a React app
create-react-app my-app
cd my-appMake it a git repo
git initAt this point, this new repo is local, only on your computer. Eventually, you may want to push to Github.
Create the Heroku app
heroku create $my-app-name --buildpack https://github.com/mars/create-react-app-buildpack.git$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.
Commit & deploy β»οΈ
git add .
git commit -m "react-create-app on Heroku"
git push heroku masterVisit the app's public URL in your browser
heroku openVisit the Heroku Dashboard for the app
Find the app on your dashboard.
Continue Development
Work with your app locally using npm start. See: create-react-app docs
Then, commit & deploy
Push to Github
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.
Customization
Web server
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/" }Routing clean URLs
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"
}
}HTTPS-only
Enforce secure connections by automatically redirecting insecure requests to https://, in static.json:
{
"https_only": true
}Prevent downgrade attacks with HTTP strict transport security. Add HSTS "headers" to static.json:
{
"headers": {
"/**": {
"Strict-Transport-Security": "max-age=7776000"
}
}
}max-ageis the number of seconds to enforce HTTPS since the last connection; the example is 90-days
Environment variables
REACT_APP_* environment variables are supported with this buildpack.
Set vars on Heroku
heroku config:set REACT_APP_HELLO='I love sushi!'Set vars for local dev
Requires at least create-react-app 0.7. Earlier versions only support Compile-time.
Create a .env file that sets a variable per line:
REACT_APP_API_URL=http://api.example.com
REACT_APP_CLIENT_ID=XyzxYzxyZCompile-time vs Runtime
Two versions of variables are supported. In addition to compile-time variables applied during build the app supports variables set at runtime, applied as each web dyno starts-up.
| Requirement | Compile-time | Runtime |
|---|---|---|
| never changes for a build | β | |
| support for continuous delivery | β | |
| updates immediately when setting new config vars | β | |
| different values for staging & production (in a pipeline) | β | |
ex: REACT_APP_BUILD_VERSION (static fact about the bundle) |
β | |
ex: REACT_APP_DEBUG_ASSERTIONS (prune code from bundle) |
β | |
ex: REACT_APP_API_URL (transient, external reference) |
β | |
ex: REACT_APP_FILEPICKER_API_KEY (Add-on config vars) |
β |
Compile-time configuration
heroku config:set REACT_APP_HELLO='I love sushi!'
git commit --allow-empty -m "Set REACT_APP_HELLO config var"
git push heroku masterRuntime configuration
Requires at least create-react-app 0.7.
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>
);
}
}\n, into Runtime config vars. Use literal UTF-8 values only; they will be automatically escaped.
Add-on config vars
Use a custom .profile.d script to make variables visible to the React app by prefixing them with REACT_APP_.
- create
.profile.d/000-react-app-exports.sh - make it executable
chmod +x .profile.d/000-react-app-exports.sh add an
exportline for each variable:export REACT_APP_ADDON_CONFIG=${ADDON_CONFIG:-}
- set-up & use Runtime configuration to access the variables
For example, to use the API key for the Filestack JS image uploader:
export REACT_APP_FILEPICKER_API_KEY=${FILEPICKER_API_KEY:-}Version compatibility
This buildpack will never intentionally cause previously deployed apps to become undeployable. Using master as directed in the main instructions will always deploy an app with the most recent version of this buildpack.
Releases are tagged, so you can lock an app to a specific version, if that kind of determinism pleases you:
heroku buildpacks:set https://github.com/mars/create-react-app-buildpack.git#v1.2.1v1.2.1 with the desired release tag.
Architecture π
This buildpack composes several buildpacks (specified in .buildpacks) to support no-configuration deployment on Heroku:
heroku/nodejsbuildpack- complete Node.js enviroment to support the webpack build
node_modulescached between deployments
mars/create-react-app-inner-buildpack- enables runtime environment variables
- generates the default
static.json - performs the production build for create-react-app,
npm run build
heroku/staticbuildpack- Nginx web server
- handy static website & SPA (single-page app) customization options
General-purpose SPA deployment
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.