React GitHub Pages
Introduction
GitHub Pages provides an excellent free hosting solution for your React applications. It allows you to easily share your projects with others without the need for a dedicated hosting service. In this guide, we'll walk through the process of deploying a React application to GitHub Pages, from setting up your repository to configuring your project and performing the actual deployment.
GitHub Pages is particularly useful for:
- Portfolio projects
- Demo applications
- Documentation sites
- Small to medium-sized web applications
Prerequisites
Before we begin, make sure you have:
- A GitHub account
- Git installed on your computer
- Node.js and npm installed
- A React application created (using Create React App or a similar tool)
Setting Up Your GitHub Repository
First, you need a GitHub repository to host your project:
- Log in to your GitHub account
- Create a new repository or use an existing one
- Push your React project code to the repository
For personal projects, consider naming your repository username.github.io
(replacing "username" with your GitHub username) to make it available at https://username.github.io/
Installing the GitHub Pages Package
To deploy your React app to GitHub Pages, you'll need to install the gh-pages
package, which simplifies the deployment process.
npm install gh-pages --save-dev
or if you're using Yarn:
yarn add gh-pages --dev
Configuring Your React App for GitHub Pages
1. Update your package.json
file
Add the following fields to your package.json
:
{
"homepage": "https://username.github.io/repository-name",
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
// ...existing scripts
}
}
Replace username
with your GitHub username and repository-name
with your repository name.
2. Configuring React Router (if applicable)
If your application uses React Router, you'll need to configure it to work with GitHub Pages. GitHub Pages serves your app from a subdirectory, so you need to adjust your router accordingly.
For React Router v6:
import { HashRouter } from "react-router-dom";
function App() {
return (
<HashRouter>
{/* Your routes */}
</HashRouter>
);
}
Alternatively, you can use BrowserRouter
with a basename
prop:
import { BrowserRouter } from "react-router-dom";
function App() {
return (
<BrowserRouter basename="/repository-name">
{/* Your routes */}
</BrowserRouter>
);
}
Building and Deploying Your React App
Now that your project is configured, you can deploy it to GitHub Pages:
npm run deploy
This command will:
- Run the build script to create a production build of your app
- Create a new branch called
gh-pages
in your repository - Push the contents of the
build
folder to this branch - Configure GitHub Pages to serve your site from this branch
Setting Up GitHub Pages in Repository Settings
After running the deploy command, you need to configure your GitHub repository to serve the site:
- Go to your GitHub repository
- Click on "Settings"
- Scroll down to the "GitHub Pages" section
- In the "Source" dropdown, select the
gh-pages
branch - Click "Save"
After a few minutes, your site will be available at the URL you specified in the homepage
field of your package.json
file.
Troubleshooting Common Issues
404 Errors
If you're experiencing 404 errors when navigating your deployed application:
- Check that your React Router configuration is correct
- Verify that asset paths in your code are relative, not absolute
- Ensure the
homepage
field inpackage.json
is correctly set
Custom Domain Configuration
If you want to use a custom domain with your GitHub Pages site:
- Go to your repository settings
- In the GitHub Pages section, enter your custom domain
- Create a CNAME record with your domain registrar pointing to
username.github.io
- Add a file named
CNAME
to thepublic
folder of your React app containing your domain name
www.yourdomain.com
Real-World Example: Portfolio Site
Let's walk through a complete example of deploying a simple React portfolio site to GitHub Pages:
- Create a new React application:
npx create-react-app my-portfolio
cd my-portfolio
- Add a simple portfolio component:
// src/components/Portfolio.js
import React from 'react';
function Portfolio() {
return (
<div className="portfolio">
<h1>Jane Doe - Web Developer</h1>
<div className="projects">
<div className="project">
<h2>Project 1</h2>
<p>A React application for task management</p>
</div>
<div className="project">
<h2>Project 2</h2>
<p>An e-commerce site built with React and Node.js</p>
</div>
</div>
</div>
);
}
export default Portfolio;
- Update your
App.js
:
// src/App.js
import './App.css';
import Portfolio from './components/Portfolio';
function App() {
return (
<div className="App">
<Portfolio />
</div>
);
}
export default App;
- Initialize Git and create a GitHub repository:
git init
git add .
git commit -m "Initial commit"
- Connect to GitHub:
git remote add origin https://github.com/username/my-portfolio.git
git branch -M main
git push -u origin main
- Install
gh-pages
and configurepackage.json
:
npm install gh-pages --save-dev
Update package.json
:
{
"name": "my-portfolio",
"homepage": "https://username.github.io/my-portfolio",
"version": "0.1.0",
"private": true,
"dependencies": {
// ...dependencies
},
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
// ...rest of package.json
}
- Deploy to GitHub Pages:
npm run deploy
-
Configure GitHub repository settings to use the
gh-pages
branch. -
Visit your live portfolio at
https://username.github.io/my-portfolio
The Deployment Process Visualized
Here's a visual representation of how the GitHub Pages deployment process works:
Summary
Deploying a React application to GitHub Pages is a straightforward process that provides free hosting for your projects. By following these steps, you can quickly share your React applications with others:
- Create a GitHub repository for your project
- Install the
gh-pages
package - Configure your
package.json
with the correct homepage and scripts - Run the deploy script
- Configure GitHub Pages in your repository settings
GitHub Pages is perfect for personal projects, portfolios, and demonstration applications. However, for more complex applications that require backend services, you might need to consider more robust hosting solutions.
Additional Resources
Exercises
- Create a simple React application and deploy it to GitHub Pages
- Add React Router to your application and configure it for GitHub Pages
- Create a portfolio website using React and host it on GitHub Pages
- Configure a custom domain for your GitHub Pages site
- Create a GitHub Action that automatically deploys your React app whenever you push to the main branch
By mastering GitHub Pages deployment for your React applications, you have a powerful and free way to share your projects with the world and showcase your development skills.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)