DEV Community

Cover image for Deploying Vite / React App to GitHub Pages
Rashid Shamloo
Rashid Shamloo

Posted on • Edited on

Deploying Vite / React App to GitHub Pages

Just follow these simple steps:

1. Install the gh-pages package (ctrl+~ to open the terminal in VS Code)

npm install gh-pages --save-dev
Enter fullscreen mode Exit fullscreen mode

2. In the package.json file add these lines before "build": "vite build",

"predeploy": "npm run build",
"deploy": "gh-pages -d dist",
Enter fullscreen mode Exit fullscreen mode

3. In the vite.config.js file add this line before plugins: [react()],

base: "/YOUR_REPOSITORY_NAME",
Enter fullscreen mode Exit fullscreen mode

Change YOUR_REPOSITORY_NAME to the name of your GitHub repository.

4. In terminal type

npm run deploy
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ You now have a gh-pages branch in your repository and your app is deployed (you can check it under Settings -> Pages )

P.S. To update your app deployment, just run the npm run deploy command again.

Latest comments (56)

Collapse
 
tene profile image
TenE

I think it works without config we need just base:"URL starter ex: docs,app"

Collapse
 
hashim_baloch_ profile image
Hashim Baloch

Hey man! I tried everything written above and below in comments but still I always get that white screen on deployment and dist. The build and deploy are always successful but I can't get rid of the white screen as the app doesn't loads up. This is my repo. Could you help?

This is the browser console for local build:
`Refused to apply style from '127.0.0.1:5500/assets/index-BtomFT...' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.Understand this errorAI
:5500/assets/index-BsCh7obV.js:1

   Failed to load resource: the server responded with a status of 404 (Not Found)Understand this errorAI
Enter fullscreen mode Exit fullscreen mode

index.html:41 Live reload enabled.
index.html:1 Refused to apply style from '127.0.0.1:5500/assets/index-BtomFT...' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.`

Deployed site console:
`Failed to load resource: the server responded with a status of 404 ()Understand this errorAI
index-BsCh7obV.js:1

   Failed to load resource: the server responded with a status of 404 ()Understand this errorAI
Enter fullscreen mode Exit fullscreen mode

24browsecSiteContentScript.js:1 browsecSiteContentScript: data is not for auth
(anonymous) @ browsecSiteContentScript.js:1
Show 1 more frame
Show lessUnderstand this warningAI
index-BtomFTFd.css:1

   Failed to load resource: the server responded with a status of 404 ()`
Enter fullscreen mode Exit fullscreen mode
Collapse
 
xjqiu28 profile image
Xin Jin Qiu

Hi, when I follow the instructions and it says published. I get a white screen afterwards with only these errors:

Naturalization-Test/:7
GET xjqiu28.github.io/assets/index-D4N... net::ERR_ABORTED 404 (Not Found)
Naturalization-Test/:8
GET xjqiu28.github.io/assets/index-bd1... net::ERR_ABORTED 404 (Not Found)

for those two lines, here is my code:
<script type="module" crossorigin src="/assets/index-D4NM0QCT.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-bd1VVfxY.css">

I cant seem to find the answer or solution

Collapse
 
boondesign profile image
BOON IMAGE DESIGN

you not yet commit , possibility

Collapse
 
xjqiu28 profile image
Xin Jin Qiu

Here is the file structure for the items:

Image description

Collapse
 
majdsayedtaha profile image
Majdsayedtaha • Edited

when I set the base: "/YOUR_REPOSITORY_NAME", it works! because the Vite config file doesn't have this attribute.
Thnx a lot!

Collapse
 
alihamza126 profile image
Ali Hamza

help ful

Collapse
 
mikixit profile image
MikixIT - Michael

Thanks!

Collapse
 
royamosavipour profile image
Roya

hello How can we do the deployment for the files that have db.json

Collapse
 
joequint22 profile image
joequint22 • Edited

Keep getting a white screen when deploying my vite + react js + typescript application. I believe it wants me to fix the errors within my code but the issue is importing react. Not sure how to go about this, the website runs fine localhost but i get these errors when deploying
Image description

Collapse
 
lewhunt profile image
Lewis Hunt • Edited

Have you got the homepage parameter set in package.json? I have it pointing to my github site url:
"homepage": "https://lewhunt.github.io/react-tv-player",
then the base value in vite.config is set to the folder:
base: "/react-tv-player/",

Collapse
 
joequint22 profile image
joequint22

I actually got it to work with the base value in vite.config is set to the folder:
base: "./",

So assuming youve modified the package.json as well, it should work** USING VERCEL.COM**

emphasis on the . /

Collapse
 
jchooch profile image
joe

This has all worked for me well, except I can only access pages in my site via the home page. If I copy a URL and paste it into a new web browser, I get a 404 page. I suppose it is because the index.html file is not being served in that case. How do I fix this issue? I would like to be able to access any page of my site by its URL! Thank you so much in advance.

Collapse
 
rashidshamloo profile image
Rashid Shamloo • Edited

If you're using React Router, you can use the Hash Router instead of the Browser Router.

There is also another workaround that I personally use in my projects: spa-github-pages

Collapse
 
tanishgl profile image
Tanish Goyal

Hi! When using Hash Router, do we need to modify base path in vite.config.js to 'repo-name/#' ? For some reason, I can't test my app locally when using hash router. I manually add /#/route-path infront of the base URL for testing and then it works.

Thread Thread
 
rashidshamloo profile image
Rashid Shamloo

There's no need to change the base to something else for the hash router to work so there should be another cause for your problem (using instead of is a common culprit).

Thread Thread
 
tanishgl profile image
Tanish Goyal

Hi, thanks for the information.

Collapse
 
fauza profile image
fauza

Hi!, this is my first time deploying my project using Github pages, everything looks good but when I try to visit my site, I only got white screen, and then I try to see at console browser and I got this error:
'Failed to load resource: the server responded with a status of 404 () /src/main.jsx:1'
what should I do?

Collapse
 
rashidshamloo profile image
Rashid Shamloo

"There is no general fix for the 404 error. it depends on your project and why you're getting the error. if you link to your project I can help further."

Collapse
 
fauza profile image
fauza

thanks for the information bro, but I have found the solution