DEV Community

Emmanuel Obiezue
Emmanuel Obiezue

Posted on

Deploying a React application to GitHub Pages

Deploying a React application to GitHub Pages is a popular and straightforward way to host your static projects for free. GitHub Pages provides a convenient platform for sharing your work with others, whether it's a personal project, portfolio, or documentation site. Here's a step-by-step guide to deploying your React application to GitHub Pages.

Prerequisites

Before you begin, ensure that you have the following:

  1. Node.js and npm: Ensure that Node.js and npm are installed on your machine. You can check this by running node -v and npm -v in your terminal.

  2. React Application: You should have an existing React application created using create-react-app or a similar setup.

  3. GitHub Repository: A GitHub account with a repository where you want to deploy your React application. The repository can be either public or private.

Step 1: Install gh-pages Package

The gh-pages package makes it easy to deploy your React application to GitHub Pages. Install it by running the following command in your React project's root directory:

npm install --save-dev gh-pages
Enter fullscreen mode Exit fullscreen mode

Step 2: Update package.json

To automate the deployment process, you need to add a few fields to your package.json file.

  1. Add the homepage field: This specifies the URL of your GitHub Pages site. Replace username and repository-name with your GitHub username and repository name, respectively.
   "homepage": "https://username.github.io/repository-name",
Enter fullscreen mode Exit fullscreen mode
  1. Add predeploy and deploy scripts: These scripts will help you build and deploy your application with a single command.
   "scripts": {
     "predeploy": "npm run build",
     "deploy": "gh-pages -d build",
     // other scripts
   }
Enter fullscreen mode Exit fullscreen mode
  • predeploy: Runs the build script before deploying the app.
  • deploy: Uses gh-pages to publish the contents of the build directory to the gh-pages branch of your repository.

Step 3: Build Your Application

Run the predeploy script to build your application. This will generate an optimized production build in the build directory.

npm run build
Enter fullscreen mode Exit fullscreen mode

Step 4: Deploy to GitHub Pages

Deploy your application by running the deploy script:

npm run deploy
Enter fullscreen mode Exit fullscreen mode

This command will create or update the gh-pages branch in your repository and push the contents of the build directory to that branch. GitHub Pages will automatically serve your site from this branch.

Step 5: Access Your Deployed Site

Once the deployment is complete, you can access your site at the URL specified in the homepage field of your package.json file. It typically takes a few minutes for the site to become available.

Step 6: Custom Domain (Optional)

If you want to use a custom domain, create a CNAME file in the public directory of your React project and add your domain name (e.g., www.example.com). GitHub Pages will configure your custom domain automatically when you deploy your application.

Troubleshooting Tips

  • 404 Errors: If your React app uses client-side routing (e.g., with React Router), you may encounter 404 errors when navigating directly to a route. To fix this, create a 404.html file in your public directory with the following content:
   <script>
     window.location.href = "/repository-name" + window.location.pathname;
   </script>
Enter fullscreen mode Exit fullscreen mode
  • Cache Issues: Sometimes, GitHub Pages may cache old versions of your site. To resolve this, try clearing your browser cache or appending a query string (e.g., ?v=1) to the URL.

Conclusion

Deploying a React application to GitHub Pages is a simple and effective way to showcase your work. By following these steps, you can have your project live in just a few minutes. GitHub Pages is an excellent choice for hosting static sites, and with the help of the gh-pages package, the deployment process is smooth and hassle-free.

Top comments (0)