Logo

dev-resources.site

for different kinds of informations.

Deploy Vite React project with React router without 404 on Github pages

Published at
1/9/2025
Categories
react
vite
github
Author
avithe1
Categories
3 categories in total
react
open
vite
open
github
open
Author
7 person written this
avithe1
open
Deploy Vite React project with React router without 404 on Github pages

Hello,

Yes, its pretty simple to publish a Vite React project on Github pages. You will find many articles that show how this is done. I am talking about the simple publish here and not about github actions.

I followed the exact steps mentioned, with every possible configuration that was explained, of course the project got published but I always got a blank page on the live URL with 404 errors when I inspected the console.

My URL structure was : https://<username>.github.io/<repo-name>

After some tinkering I realized that this issue was because I was using React Router in the project. The fix to this issue, I had to set up the base URL in two places.

  1. vite.config.ts (which I was already doing following the articles)
  2. BrowserRouter

Its the second point that took sometime to figure out. All that was needed to be done was <BrowserRouter basename="/repo-name">

This fixed the 404 issues I was facing when deploying to github pages.

To summarize :

  • Your Vite-React project repository should be committed to github.

  • Take a note/copy the Repository name as it is in the Settings>General tab on github, I will call this repo-name hereafter.

  • npm install gh-pages --save-dev

  • add the following to your package.json

"homepage" : https://<username>.github.io/<repo-name>
Enter fullscreen mode Exit fullscreen mode
"predeploy": "npm run build",
"deploy": "gh-pages -d dist"
Enter fullscreen mode Exit fullscreen mode
  • add the following to your vite.config.ts
export default defineConfig({
  base: "/repo-name", 
  plugins: [react()],
});
Enter fullscreen mode Exit fullscreen mode
  • add the following to you BrowserRouter
<BrowserRouter basename="/repo-name">
Enter fullscreen mode Exit fullscreen mode
  • run command npm run deploy

Wait for a few seconds , check the github repository Settings>Pages tab if needed.

Your website will be published to https://<username>.github.io/<repo-name>

Note : be careful with the forward slashes in the repo-name, I have used only /repo-name and not /repo-name/

This also changes the localhost URL to http://localhost:5173/repo-name

I hope this helps anyone who is stuck with a blank page and 404 asset/js files not found in console issue when publishing a Vite React project with React Router to Github pages. Thanks.

vite Article's
30 articles in total
Favicon
Creating a react game on AWS
Favicon
Mega Menu Breaks, CSS3
Favicon
Setup of React project with Vite and TailwindCSS
Favicon
Setting Up Dual Compilation (SSR + CSR) in ViteJS with vite-plugin-builder
Favicon
Load Environment Variables using dotenv-local
Favicon
Monorepo + microfrontend with module-federation+vite
Favicon
Help Improve Shademaker!
Favicon
Web Architecture: Monorepos, Micro-frontends, and Vite
Favicon
I Built a CaddyFile Generator Tool in Just 8 Hours – Here’s How It Went
Favicon
Unlocking the Power of Modern Web Architecture: Monorepos, Micro-Frontends, and Vite πŸš€βœ¨
Favicon
Build and Deploy a Monorepo WebSocket web application with Turbo, Express, and Vite on Render Using Docker
Favicon
Vike - Next.js & Nuxt alternative for unprecedented flexibility and dependability
Favicon
🌟 Introducing My Open-Source Resume: A Step Toward Transparency and Contribution πŸš€
Favicon
Lovable PDF Generation
Favicon
vite
Favicon
Testing Vue components with Vitest
Favicon
How can VoidZero be commercialized?
Favicon
HMR refreshes browser with every change
Favicon
Integrating React and Vite: How It Boosted My Development Speed
Favicon
[Boost]
Favicon
Deploying React Apps with Vite: The Complete Guide
Favicon
module css dans vite
Favicon
Deploy Vite React project with React router without 404 on Github pages
Favicon
Penetration Testing Tools: A Comprehensive Guide
Favicon
Lessons Learned Migrating from React CRA & Jest to Vite & Vitest
Favicon
Static React App Deployment with Vite
Favicon
Building Modern, Installable Web Apps with PWAs: The Ultimate Guide
Favicon
Vite: A quick guide to the next generation front-end building tool
Favicon
Vite: Future of Modern Build Tools
Favicon
Deploy Vite React App to GitHub Pages 4 Step:

Featured ones: