Deploy React To Github Pages

Deploy React To Github Pages

Reading time1 min
#React#Development#Web#GitHubPages#ReactDeployment

Mastering React Deployment on GitHub Pages: A Practical Guide for Seamless Publishing

Many developers underestimate GitHub Pages, viewing it as too basic for modern React apps. This guide flips that notion by showing how with precise configuration and smart tweaks, GitHub Pages can handle complex React deployments with elegance and reliability—no extra hosting fees required.

Deploying React apps to GitHub Pages offers a cost-effective, straightforward way to share live projects with the world, boosting developer credibility and portfolio strength. However, understanding the nuances of this deployment environment ensures smooth builds and helps you avoid common pitfalls such as routing errors and broken links.


Why Deploy React Apps on GitHub Pages?

Before we dive in, let's quickly recap why GitHub Pages is a great option:

  • Free hosting: No hosting charges or subscriptions.
  • Simple setup: Push your repository, then publish with minimal fuss.
  • Custom domains: Use your own domain if desired.
  • Automatic HTTPS: Secure your site with HTTPS by default.
  • Great for portfolios & demos: Perfect for showcasing projects publicly.

Step-by-Step Guide to Deploying React on GitHub Pages

1. Prepare Your React App

Start by creating a React app via Create React App (CRA), which simplifies many setup aspects:

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

If you already have a React app, just make sure it’s ready to be deployed.

2. Add homepage in package.json

GitHub Pages serves your app from a subpath in the URL (unless you are using a custom domain). This is critical because React needs to know where the root of your app resides.

Open your package.json and add the homepage field:

"homepage": "https://<your-github-username>.github.io/<repository-name>"

For example:

"homepage": "https://johndoe.github.io/my-react-app"

This helps CRA generate correct asset paths during build.

3. Install gh-pages Package

GitHub Pages can serve the content from your repository’s gh-pages branch. The gh-pages npm package automates publishing build files there.

Install it as a dev dependency:

npm install --save-dev gh-pages

4. Add Deployment Scripts in package.json

Extend your package.json scripts section:

"scripts": {
  "predeploy": "npm run build",
  "deploy": "gh-pages -d build",
  // ... other scripts like start, test
}
  • predeploy: Runs npm run build before pushing files.
  • deploy: Uses gh-pages to publish your production build.

5. Initialize Git Repository & Connect to GitHub

If you don’t have git initialized already:

git init
git remote add origin https://github.com/<your-github-username>/<repository-name>.git
git add .
git commit -m "Initial commit"
git push -u origin main

Make sure you push your code to the main/master branch or whatever branch you use.

6. Deploy Your App

Run:

npm run deploy

This will:

  • Build the app,
  • Create/update the gh-pages branch with build output,
  • Push it to GitHub.

7. Configure GitHub Repository Settings

Go to your repo on GitHub → Settings → Pages (or Settings → Code and automation → Pages on new UI).

Set the source branch to gh-pages and folder as /root.

After saving, GitHub will provide your published URL (which should match the homepage value).


Common Pitfalls and Solutions

Issue: Routing Doesn't Work / 404 Errors on Refresh

React Router’s default browser history mode requires server support for rewriting URLs back to index.html, but GitHub Pages is static without backend support.

Solutions:

  1. Use HashRouter instead of BrowserRouter in your app:
import { HashRouter } from 'react-router-dom';

function App() {
  return (
    <HashRouter>
      {/* Your routes here */}
    </HashRouter>
  );
}

HashRouter works by keeping routes after a hash (#) symbol – which Github Pages handles gracefully without special server config.

  1. Or set up custom redirects using a 404.html trick that redirects all unknown routes back to index.html (more complex but possible).

Example Recap: Full package.json Excerpt

{
  "name": "my-react-app",
  "version": "0.1.0",
  "private": true,
  "homepage": "https://johndoe.github.io/my-react-app",
  "dependencies": {
    "react": "^18.x",
    "react-dom": "^18.x",
    "react-router-dom": "^6.x"
    // Other dependencies...
  },
  "devDependencies": {
    "gh-pages": "^4.x"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build"
  }
}

Final Thoughts

Deploying React apps via GitHub Pages is a powerful tool for developers looking to quickly share live projects without extra costs or complex infrastructure. With careful configuration—especially setting the correct homepage path and using appropriate routing—you can enjoy reliable, professional deployments that elevate your portfolio and impress clients or employers alike.

Try this approach on your next project—it’s practical, elegant, and surprisingly robust!


If you found this guide useful, share it with fellow developers or drop questions below! Happy coding! 🚀