react-gh-pages
I love building quick static sites in React for small one-off projects, and I love hosting them publicly for others to interact with them. What I don’t love though, is having to pay to host free hobby sites.
One of the greatest npm libraries I’ve found for this is react-gh-pages.
It’s ridiculously easy to use; however, the README is a tiny bit outdated, at least for Vite, so I wanted to write up a quick guide as a note to self (and others!) for the process I’ve found that works.
Process
-
Create your React Vite App As Normal
-
Run the following command:
npm install gh-pages --save-dev -
Open up your
package.jsonand add the following line:https://{username}.github.io/{repo-name} -
In the
scriptssection ofpackage.jsonadd the following lines:"predeploy": "npm run build", "deploy": "gh-pages -d dist" -
In your
vite.config.ts, addbase: /{repo-name}/to yourdefineConfigarguments -
Add your remote reository git remote add origin https://github.com/{username}/{repo-name}.git and ensure this aligns with step 3.
-
Run
npm run deploy -
Your remote repository should now have a
gh-pagesbranch. Open your GH Repo Settings and open the Pages settings. Configure your Build and deployment settings like so:
-
Navigate to
https://{username}.github.io/{repo-name}and you should now have a deployed static site!