DEV Community

Cover image for Deploying Your First React App to Production
Amr Saafan for Nile Bits

Posted on • Originally published at nilebits.com

Deploying Your First React App to Production

Putting your first React application live might be intimidating, particularly if you've never done it before. That being said, any developer creating contemporary web apps has to have this ability. With thorough instructions and a ton of code samples, we'll go over everything you should know in this tutorial to launch a React project. Additionally, we'll guide you through several deployment techniques utilizing platforms like Vercel, Netlify, GitHub Pages, and more.

What Is React?

Before moving on to deployment, let's talk a little bit about React. A well-liked JavaScript package called React is used to create user interfaces, especially for single-page applications (SPAs). Facebook developed it, allowing programmers to create expansive apps where data is updated without requiring a page reload. The component-based approach, which is the main focus of React, enables you to construct reusable user interface components that independently maintain state.

Preparing Your React App for Production

  1. Setting Up Your React Project

If you haven't created a React project yet, you can use the create-react-app command to get started quickly. Here’s how you can set up a new React project:

npx create-react-app my-react-app
cd my-react-app
npm start
Enter fullscreen mode Exit fullscreen mode

Once you run npm start, your application will be running in development mode. Before deploying to production, you'll want to ensure that your app is production-ready.

  1. Optimizing Your React App for Production

By default, React provides several optimizations when building for production. These include minification of JavaScript, optimized asset loading, and improved performance. You can build your app for production by running:

npm run build
Enter fullscreen mode Exit fullscreen mode

This command creates an optimized build in the build/ folder. It includes:

HTML, CSS, and JavaScript files optimized for performance.

Static assets like images and fonts.

Minified code to reduce the size of the application.

Source maps to help debug issues in production.

Deploying React App on GitHub Pages

GitHub Pages is an easy and free option to host static websites. Since React apps are typically SPAs, you can deploy them on GitHub Pages. Here’s how to deploy a React app on GitHub Pages:

  1. Install the gh-pages Package

First, install the gh-pages package as a development dependency:

npm install gh-pages --save-dev

Enter fullscreen mode Exit fullscreen mode
  1. Update package.json

In your package.json, add the following configurations:

{
  "homepage": "https://<your-username>.github.io/<your-repo-name>",
  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build"
  }
}
Enter fullscreen mode Exit fullscreen mode

Replace and with your GitHub username and repository name.

  1. Deploy the App

Push your code to the repository, and then run the following command to deploy your app:

npm run deploy

Enter fullscreen mode Exit fullscreen mode

GitHub Pages will now host your React app at https://.github.io/.

Deploying React App on Netlify

Netlify is another popular platform for deploying React applications. It simplifies the deployment process and offers features like automatic build, continuous deployment, and custom domain support. Here’s how to deploy a React app on Netlify:

  1. Build Your React App

Run the following command to build the app for production:

npm run build

Enter fullscreen mode Exit fullscreen mode
  1. Deploy the App via Netlify Dashboard

Go to Netlify and sign up for an account.

Click New Site from Git and choose your repository.

Configure the build settings. For React apps, use the following build command and publish directory:

Build command: npm run build

Publish directory: build/

Click Deploy and let Netlify do the work.

  1. Continuous Deployment

Netlify automatically redeploys your app whenever you push changes to your GitHub repository. This makes it easy to keep your production app up to date.

Deploying React App on Vercel

Vercel is another great platform for deploying React apps. It’s optimized for performance and offers a seamless integration with GitHub.

  1. Build Your React App

Like other platforms, you need to build the app first:

npm run build

Enter fullscreen mode Exit fullscreen mode
  1. Deploying with Vercel CLI

You can use the Vercel CLI for quick deployments. First, install the Vercel CLI globally:

npm install -g vercel

Enter fullscreen mode Exit fullscreen mode

Next, run the following command to deploy your app:

vercel
Enter fullscreen mode Exit fullscreen mode

Vercel will prompt you to configure your deployment, after which it will deploy your app to a custom URL.

Deploying React App on Heroku

Heroku is a cloud platform that supports server-side applications. If you need to deploy a full-stack React app with a backend, Heroku is a great option.

  1. Install the Heroku CLI

First, install the Heroku CLI:

curl https://cli-assets.heroku.com/install.sh | sh

  1. Create a Heroku App

Log in to Heroku and create a new app:

heroku login
heroku create
Enter fullscreen mode Exit fullscreen mode
  1. Deploy Your React App

Initialize a Git repository if you haven't done so:

git init

Enter fullscreen mode Exit fullscreen mode

Then, commit your code:

git add .
git commit -m "First commit"
Enter fullscreen mode Exit fullscreen mode

Deploy your app to Heroku:

git push heroku master

Enter fullscreen mode Exit fullscreen mode

Your React app is now live on Heroku.

Handling Routing in React for Production

When deploying React apps with client-side routing (e.g., using react-router), you might encounter issues where refreshing a page gives a 404 error. This happens because static hosts like GitHub Pages, Netlify, and Vercel don't handle routing on the client side.

To fix this issue, you can add a redirect rule:

For Netlify

Create a _redirects file in your public/ folder with the following content:

/*    /index.html   200

Enter fullscreen mode Exit fullscreen mode

This tells Netlify to redirect all traffic to index.html, allowing react-router to handle routing.

For Vercel

Vercel automatically handles client-side routing, so no extra configuration is required.

Optimizing React App Performance for Production

Once your app is deployed, you’ll want to ensure it performs optimally. Here are some best practices to follow:

  1. Code Splitting

Code splitting allows you to break your code into smaller bundles that can be loaded on demand. This reduces the initial load time. React makes it easy to implement code splitting using React.lazy() and Suspense:

const MyComponent = React.lazy(() => import('./MyComponent'));

function App() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <MyComponent />
    </Suspense>
  );
}
Enter fullscreen mode Exit fullscreen mode
  1. Lazy Loading Images

You can lazy load images to improve performance by using the loading attribute:

<img src="image.jpg" loading="lazy" alt="Lazy loaded image" />

Enter fullscreen mode Exit fullscreen mode
  1. Minify CSS and JavaScript

React’s build process automatically minifies your CSS and JavaScript. However, you can further optimize your stylesheets and scripts using tools like cssnano and terser.

  1. Use a CDN

Deploy your static assets (e.g., images, fonts) to a content delivery network (CDN) to reduce latency and improve load times.

Common Deployment Pitfalls and How to Avoid Them

  1. Forgetting to Build for Production

Always run npm run build before deploying your React app. The build process optimizes your app for production and ensures better performance.

  1. Issues with Environment Variables

When deploying, make sure your environment variables are correctly set. You can define environment variables in a .env file in the root of your project:

REACT_APP_API_URL=https://api.example.com

Enter fullscreen mode Exit fullscreen mode
  1. Routing Issues

If you’re using react-router, make sure your host supports client-side routing or implement redirect rules as mentioned earlier.

Conclusion

Congratulations! You’ve now learned how to deploy your first React app to production using various platforms, including GitHub Pages, Netlify, Vercel, and Heroku. By following the steps outlined in this guide, you can confidently deploy your React applications to production environments and ensure they perform optimally.

References

React Documentation

GitHub Pages

Netlify Documentation

Vercel Documentation

Heroku Documentation

Deploying a React app might seem challenging at first, but with practice, it becomes straightforward. Follow best practices, avoid common pitfalls, and leverage the right tools to make your deployment process smoother.

Top comments (0)