Deploy React App to Heroku using Travis Continuous Integration
by John Vincent
Posted on April 15, 2018
Deploy, as an example, the React hot-cold-enzyme application to Heroku using Travis Continuous Integration.
Heroku is not a server and so a React app will need a server to serve up the static resources.
Nginx is a good choice which is what I use for production environments. See TaskMuncher Deployment for a detailed explanation.
When using Heroku, I prefer to deploy a React app with a Node Express server mostly it is very simple to build, deploy and troubleshoot.
Final Result
Development
Local: /Users/jv/Desktop/MyDevelopment/github/thinkful/hot-cold-enzyme
To build the app at Heroku it is necessary to provide package.json
in the root of the project.
Notice that Heroku requires these scripts to be able to install, test and run the application.
package.json
"scripts": {
"start": "cd server && node server.js",
"test": "cd client && npm install && npm run prod && npm test && cd ../server && npm install && LOG_LEVEL=info mocha ./test --exit",
"heroku-postbuild": "cd server && npm install && cd ../client && npm install --only=dev && npm install && npm run prod"
},
These scripts will instruct Travis/Heroku how to build the app.
Client App
client/package.json
"scripts": {
"test-help": "jest --help",
"test": "jest",
"start": "webpack-dev-server --hot --inline",
"help": "webpack --help",
"build": "webpack -p",
"prod": "NODE_ENV=production npm run build",
"eslint_versions": "npm info eslint-config-airbnb@latest peerDependencies"
},
Start the DevServer
cd client
npm start
Test App at http://localhost:8021
Make a production build
npm run prod
makes react app files in client/dist
Run the app from client/dist
Test Using Enzyme and Jest
cd client
npm test
Server App
server/package.json
"scripts": {
"nodemon": "nodemon server.js",
"start": "node server.js",
"test": "LOG_LEVEL=info mocha ./test --exit",
"esdoc": "./node_modules/.bin/esdoc -c ./esdoc.json"
},
Start the server
cd server
npm start
or, in development
npm run nodemon
Test the app using Node Express server with static resources being served from client/dist
Review Server
server/config/routes.js
Serve index.html
as /
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../../client/dist/index.html'));
});
server/config/middleware.express.js
Handle CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
if (req.method === 'OPTIONS') {
logger.debug('Request method = "OPTIONS"');
res.end();
} else {
next();
}
});
Handle static content
app.use(express.static(path.resolve(__dirname, '../../client/dist/')));
Travis CI
For details, see Continuous Integration with Travis CI
Use
- Github project: hot-cold-enzyme
- Heroku project: hot-cold-enzyme
Test Heroku App
To run the app on Heroku:
https://hot-cold-enzyme.herokuapp.com/
Heroku
- Add Integration testing to Blogging App
- Add Mongoose to blogging app
- Continuous Integration with Travis CI
- Create SpringBoot App and Deploy to Heroku
- Deploy Node Express App to Heroku using Travis Continuous Integration
- Deploy React App to Heroku using Travis Continuous Integration
- Deploy Static Website to Heroku
- Heroku Notes
- Integrating Mongoose into an Express app
- Integration testing in a Mongoose world
- Tests and CI for Blogging App
React
- Basic React
- Basic React Patterns
- Basic React Redux
- Basic React Redux App
- Basic React Testing with Jest and Enzyme
- Building and deploying MyTunes to johnvincent.io
- Building and deploying React Github Helper App to johnvincent.io
- Deploy React App to Heroku using Travis Continuous Integration
- Deploy TaskMuncher React App to AWS
- First time deploy TaskMuncher React App to Digital Ocean
- Gatsby and Client Only Components
- Gatsby Getting Started
- Gatsby React Icons
- Mac Visual Studio Code
- Material-UI
- Material-UI Pickers
- Material-UI Styling
- Optimizing TaskMuncher with Webpack 4
- Overview of React Gomoku
- Overview of React Hangman
- Overview of React Lights Out
- Overview of React Yahtzee
- React Material-UI
- React Production Issues
- React PropTypes
- React/Redux Node/Express Ecosystem
- Redux Dev Tools
- Responsive Material-UI
- Styling Material-UI components using Styled-Components
- TaskMuncher Performance
- Transforming Html with Webpack
- Update TaskMuncher for Lighthouse Findings
- Update TaskMuncher to be a Progressive Web App
- Update TaskMuncher to use React BrowserRouter
- Update TaskMuncher to Webpack v4, Babel v7, Material-UI v3
- Upgrading Babel and ESLint to use React Advanced Language Features
- Webpack Bundle Analyzer