Deploying a Vite/React application on GitHub Pages is an exciting milestone, but the process can sometimes come with unexpected challenges, especially when dealing with images and assets. This blog post will walk you through the entire process, from the initial deployment to troubleshooting common problems and arriving at effective solutions.
Whether you're a beginner or someone with experience, this guide will help you avoid common pitfalls and successfully deploy your Vite/React app with all assets properly rendered.
Before we dive in, make sure you have the following:
First, ensure that your Vite/React application is properly initialized and works on localhost. You can create a basic Vite project as follows:
npm create vite@latest
Install dependencies:
npm install
Run the project locally to confirm everything works:
npm run dev
Once your project is ready, push it to your GitHub repository.
GitHub Pages expects the application to be served from a specific base URL, which is typically your repository name. Update the vite.config.js file to reflect this:
import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' export default defineConfig({ base: process.env.NODE_ENV === 'production' ? '/your-repo-name/' : '/', plugins: [react()], })
The base option ensures that the application uses the correct base path when deployed.
You’ll need to install the gh-pages package to handle the deployment.
npm install gh-pages --save-dev
In your package.json, add the following scripts to automate the deployment process:
"scripts": { "predeploy": "npm run build", "deploy": "gh-pages -d dist" }
Run the deployment:
npm run deploy
At this point, your project should be live at https://
After deploying, you might notice that the app loads a blank page. This is often caused by routing issues. By default, BrowserRouter from react-router-dom is used, which relies on server-side routing, but GitHub Pages serves static files and doesn't handle client-side routes.
Solution: Use HashRouter
To solve this, switch from BrowserRouter to HashRouter in your index.js or main.jsx file.
import { HashRouter } from 'react-router-dom'; createRoot(document.getElementById('root')).render();
HashRouter uses a hash symbol (#) in the URL to keep track of the navigation state, allowing GitHub Pages to properly serve different routes without returning a 404.
After fixing the blank page issue, another problem you may face is the app working on the homepage but showing a 404 error when navigating to other routes, especially if you directly access a route like /blogs or /projects.
This happens because GitHub Pages only knows how to serve the index.html file and doesn't recognize routes managed by React Router.
Solution: Handle 404 Errors in GitHub Pages Settings
To fix this, you need to create a 404.html file in your public/ folder. This file will ensure that GitHub Pages serves your index.html file for any routes it doesn’t recognize, allowing React Router to handle routing:
One of the most common deployment issues is images not loading correctly. While they work fine on localhost, you might notice broken image links after deploying to GitHub Pages.
For example, you might reference an image like this in your components:
Issue: Incorrect Image Paths
The problem here is that absolute paths (starting with /) don’t work well when the app is deployed in a subdirectory (e.g., /your-repo-name/). GitHub Pages tries to find the image at https://
Solution: Use BASE_URL
To fix this, move all you images into Public/Images, while that is optional, I would highly recommend you do so. The you will dynamically prepend the BASE_URL to the image paths based on the environment:
const BASE_URL = import.meta.env.BASE_URL;
This ensures that the correct path is used both in development (localhost) and in production (GitHub Pages).
After implementing the fixes for routing and image paths, rebuild and redeploy your application:
npm run build npm run deploy
Your Vite/React application should now be fully deployed with all images correctly rendered and all routes properly handled.
In summary:
With these steps, your Vite/React app should be live and functioning smoothly on GitHub Pages. Happy coding and deploying!
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3