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

Latest commit

 

History

History
207 lines (138 loc) · 7.27 KB

README.md

File metadata and controls

207 lines (138 loc) · 7.27 KB

Heroku Buildpack for create-react-app

Deploy React.js web apps generated with create-react-app.

Automates deployment with the built-in tooling and serves it up via Nginx.

Requires

Usage

Generate a React app

create-react-app my-app
cd my-app

Make it a git repo

git init

At 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

✏️ 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, so git push heroku master will push to this new Heroku app.

Commit & deploy ♻️

git add .
git commit -m "react-create-app on Heroku"
git push heroku master

Visit the app's public URL in your browser

heroku open

Visit 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"
  }
}

Environment variables

Set config vars on a Heroku app like this:

heroku config:set REACT_APP_HELLO='I love sushi!'

Compile-time configuration

For variables that will not change between environments, such as:

  • version number
  • commit sha or number
  • browser support flags

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.

♻️ The app must be re-deployed for compiled changed to take effect, because the automatic restart after a config var change does not rebuild the JavaScript bundle.

git commit --allow-empty -m "Set REACT_APP_HELLO config var"
git push heroku master

Runtime configuration

For variables that may change between releases or environments:

  • Heroku add-on config vars
  • URLs to APIs
  • secret tokens

Any environment variable is accessible at runtime, not just REACT_APP_*.

Add script element to index.html to capture environment variable values:

  <head>
    <!-- Existing head elements come first -->
    <script type="text/javascript">
      react_app_env = {};
      react_app_env.HELLO = '{{REACT_APP_HELLO}}';
    </script>
  </head>

Then, use these globals within the React app.

const hello = react_app_env.HELLO;

Globals are normally considered dirty, so you may build up a more acceptable pattern for using these values in an app, such as:

  • create a module to read the global values and make them available via require
  • create a higher order component [HOC] that makes the values available via props

Version compatibility

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.

Architecture 🏙

This buildpack composes several buildpacks (specified in .buildpacks) to support no-configuration deployment on Heroku:

  1. heroku/nodejs buildpack
  • complete Node.js enviroment to support the webpack build
  • node_modules cached between deployments
  1. mars/create-react-app-inner-buildpack
  • generates the default mustache_templates.conf
  • generates the default static.json
  • performs the production build for create-react-app, npm run build
  1. heroku/heroku-buildpack-mustache
  1. heroku/static buildpack

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: