Deploying any React app on Herokuby@alex.gvozden
17,467 reads

Deploying any React app on Heroku

Read on Terminal Reader

Too Long; Didn't Read


People Mentioned

Mention Thumbnail

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Deploying any React app on Heroku
Alex Gvozden HackerNoon profile picture

@alex.gvozden

Alex Gvozden
react to story with heart

There’s no doubt Heroku is popular infrastructure platform. Times of running your own dedicated server are not gone but for development and low volume purposes using services like Heroku does make a lot of sense.

Recently I worked on React frontend for cool gaming startup, and yes, they used Heroku for servers. It was interesting to explore Heroku since my experience is mostly with Amazon AWS and dedicated servers.

Deploying static app should be easy there right? At least they make it sound like that.

Here is the article https://blog.heroku.com/deploying-react-with-zero-configuration I did not use create-react-app so it looked like I cannot use ZERO CONFIGURATION deployment option :)

But yea, being a dev, I dug under the hood and here’s what I’ve found.

There’s JSON for everything

First of all, Heroku uses something called Buildpacks to build your app on the server.

Create react app buildpack is configured to build static node server that serves React app. It is based on Static Buildpack which gave me a hint how we can configure our own repository to support Create React App buildpack.

Static buildpack uses static.json JSON config file to instruct server how to serve your application.

So basically, all you have to do to run your React app on Heroku server is…

1. Configure Heroku to use Create React App buildpack

image

2. Add static.json file to the root of my project repository

image

That’s it. Next time you push your repo that is connected to Heroku, app will build using Create React App buildpack, and it will start based on static.json file.

In this basic example we set a root of our server, dist/ in my case, and configure all routes to point to index.html.

For more configuration options please refer to Static buildpack docs on configuration.

If it’s broken on build…

On your Activity window you can track progress of builds.

image

Builds can break… like this :(

image

You can view build log and see what is it. I only had issues after installing some new dependencies, possibly if you use Yarn, which I do.

To remedy this issue delete yarn.lock from your repo and push again. Heroku will then proceed to build a project from scratch and it will succeed.

Hope it helps and follow me Alex Gvozden for more React and webdev stuff.

I am also available for React and ReactNative work on hourly or full time. Feel free to contact me at [email protected] if you want to discuss.

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa