Deploying a React app is a crucial step in the development process, allowing you to share your work with the world and gather valuable feedback. GitHub Pages offers a convenient and efficient way to host your static site, including your React applications. This blog will explore the steps to deploy your React app to GitHub Pages, ensuring your project is accessible to users and stakeholders.
React, a powerful JavaScript library for building user interfaces has become a go-to choice for developers creating dynamic and responsive web applications. With GitHub Pages, a hosting service that turns your GitHub repository into a website, you can showcase your React projects with minimal hassle.
In the following sections, I’ll guide you through the entire deployment process, from setting up your environment to pushing your React app live on GitHub Pages. Whether you're a seasoned developer or just starting, this guide will help you understand how to bring your React app to a broader audience.
Before we dive into the deployment process, there are a few prerequisites you'll need to ensure a smooth experience:
Ensure you have these prerequisites in place, as they will be the foundation upon which we deploy our React app to GitHub Pages.
We'll create a new React app using the create react app command. This command scaffolds a new React project with all the necessary files and configurations to get started. Open your terminal or command prompt and run the following command:
1npx create-react-app my-react-app 2
Replace my-react-app with the name you wish to give your React project. Once the process is complete, you'll have a new directory containing your React app's project files. This directory includes the src folder where your React components will live and other configuration files essential for the build process.
Before deploying our React app, we must prepare it for production. This involves building a production-ready version of the app using the react scripts build command. But first, we need to make a small adjustment to our package.json file.
Add a homepage property to package.json to specify the base URL for your project when it's deployed:
1"homepage": "https://<github-username>.github.io/<repo-name>/", 2
Replace <github-username>
with your GitHub username and <repo-name>
with the name of your GitHub repository. This step is crucial for ensuring your React app's paths work correctly on GitHub Pages.
Next, run the react scripts test command to make sure all tests pass and your code is ready for production:
1npm test 2
Once you're confident in your app's stability, build the production version:
1npm run build 2
This command creates an optimized build of your React app, which is necessary for deployment to GitHub Pages.
You must store your project in a GitHub repository to host your React app on GitHub Pages. If you haven't already, navigate to GitHub and create a new repository. Here's how to set it up:
Once your GitHub repository is ready, it's time to connect your local project to this remote repository. Open your terminal and navigate to your project directory. Run the following commands to initialize a local git repository and link it to your GitHub repo:
1git init 2git remote add origin https://github.com/github-username/repo-name.git 3
Replace <github-username>
and <repo-name>
with your actual GitHub username and the name of your repository. This will set up the connection between your local project and the GitHub remote repository.
The npm run build command you ran earlier generates a build directory with your compiled project files. To deploy your React app to GitHub Pages, you must push this directory to a particular branch called gh-pages.
To automate this process, you'll need to modify your package.json file by adding a pre-deploy script that builds the app and a deploy script that pushes the build to the gh-pages branch:
1"scripts": { 2 "predeploy": "npm run build", 3 "deploy": "gh-pages -d build" 4} 5
Make sure you have the gh-pages package installed, which is a tool that simplifies the deployment process to GitHub Pages:
1npm install --save-dev gh-pages 2
With these scripts in place, every time you run npm run deploy, the predeploy script will trigger the build process, and the deploy script will push the build to the gh-pages branch of your GitHub repository.
If your React app uses react-router for navigation, you must ensure that GitHub Pages handles client-side routing correctly. By default, GitHub Pages is designed to serve static files and might not support browser history API.
To address this, you can add a 404.html file with a script to redirect to your index.html file or use a HashRouter from react-router-dom instead of BrowserRouter. The HashRouter uses the hash portion of the URL (i.e., window.location.hash) to keep your UI in sync with the URL.
Now that everything is set up, deploying your React app to GitHub Pages is as simple as running the following command:
1npm run deploy 2
This command executes the predeploy and deploy scripts you added to your package.json. The gh-pages package creates the gh-pages branch (if it doesn't exist already) and pushes your build directory to it.
After the command finishes, navigate to the settings of your GitHub repository, scroll down to the GitHub Pages section, and you should see a link to your deployed app. Click on the link to view your React app live on GitHub Pages!
Once you've executed the npm deploy command, your React app should be live on GitHub Pages. To verify that the deployment was successful, follow these steps:
https://<github-username>.github.io/<repo-name>
. This is the URL where your React app is now accessible.Open the provided URL in your web browser to see your React app. Testing the app's functionality is important, ensuring all routes and resources load correctly. If you encounter any issues, such as 404 errors on page refresh, double-check your react-router configuration and the homepage property in your package.json.
As you continue to develop and improve your React app, you'll need to redeploy it to reflect the changes. Here's how to manage updates and redeploy to GitHub Pages:
1git add . 2git commit -m "Describe your changes here" 3
1git push origin master 2
1npm run deploy 2
Each time you run the deploy command, the updated version of your app will be pushed to the gh-pages branch, and GitHub Pages will serve the latest build.
GitHub Pages allows you to use a custom domain for your React app, providing a more professional web address. To configure a custom domain:
Once the DNS changes propagate, your React app will be accessible at your custom domain.
Deploying a React app to GitHub Pages can sometimes lead to issues, such as:
If you encounter any problems, review the deployment steps, check the GitHub Pages documentation, or search for similar issues on developer forums.
To maintain an organized and efficient deployment workflow, consider the following best practices:
Following these practices ensures a smooth deployment process and a stable production version of your React app.
GitHub Actions can be a powerful tool for automating their deployment workflow. You can set up a CI/CD pipeline that automatically builds and deploys your React app to GitHub Pages whenever you push changes to your repository. This advanced topic is beyond the scope of this article but is worth exploring for more complex projects.
Deploying your React app to GitHub Pages is a straightforward process with numerous benefits. It provides a quick way to showcase your work, test your app in a production environment, and gather user feedback.
By following the steps outlined in this guide, you can confidently deploy your React app to GitHub Pages. Share your deployment success stories and any additional tips you've discovered in the comments below, and help your fellow developers easily navigate the deployment process.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.