Ready to blast your React app into the GitHub galaxy? π This guideβs got all the witty steps to deploy your masterpiece with Vite, GitHub Pages, and a dash of React Router flair. Letβs roll! π
vite.config.js
π¬First, tell Vite where your app will live in the GitHub universe. Update vite.config.js
with your repo name for that perfect URL path. π
base: "/[REPO_NAME]/",
Time to automate the deployment magic! Create a .github/workflows/deploy.yml
file and paste this YAML goodness to build and deploy like a boss. πͺ
name: Deploy to Website
on:
push:
branches:
- main
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20" # Adjust to your Node vibe
- name: Install dependencies
uses: bahmutov/npm-install@v1
- name: Build project
run: npm run build
- name: Upload production-ready build files
uses: actions/upload-artifact@v4
with:
name: production-files
path: ./build
deploy:
name: Deploy
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: production-files
path: ./build
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./build
cname: www.terminaltales.com # Swap with your custom domain
Head to GitHub, create a new repo, and initialize your project like a code ninja. π₯· Run these commands to get your files up in the cloud:
git init && git add . && git commit -m "add: initial files"
git branch -M main
git remote add origin https://github.com/[USER]/[REPO_NAME]
git push -u origin main
Activate those GitHub Actions like youβre flipping on a spaceshipβs hyperdrive! π
Make your app a multi-page marvel with React Router. Install it with Bun and keep the party going! π
bun add react-router-dom
package.json
π‘Let the world know where your app lives. Add this to your package.json
for that polished URL touch. π
{
"homepage": "[PROJECT_URL]"
}
public/404.html
π§Create a public/404.html
file to catch stray visitors and redirect them smoothly. This scriptβs got your back! π¦ΈββοΈ
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>React Router</title>
<script type="text/javascript">
var pathSegmentsToKeep = 1;
var l = window.location;
l.replace(
l.protocol +
"//" +
l.hostname +
(l.port ? ":" + l.port : "") +
l.pathname
.split("/")
.slice(0, 1 + pathSegmentsToKeep)
.join("/") +
"/?/" +
l.pathname.slice(1).split("/").slice(pathSegmentsToKeep).join("/").replace(/&/g, "~and~") +
(l.search ? "&" + l.search.slice(1).replace(/&/g, "~and~") : "") +
l.hash
);
</script>
</head>
<body></body>
</html>
index.html
π§©Pop this script into the <head>
of your index.html
to keep React Routerβs URLs clean and tidy. Itβs like a digital broom! π§Ή
<script type="text/javascript">
(function (l) {
if (l.search[1] === "/") {
var decoded = l.search
.slice(1)
.split("&")
.map(function (s) {
return s.replace(/~and~/g, "&");
})
.join("?");
window.history.replaceState(null, null, l.pathname.slice(0, -1) + decoded + l.hash);
}
})(window.location);
</script>
One final push to send your changes into orbit. Run this to commit and deploy your masterpiece! π
git add -A && git commit -m "Building" && git push
Congrats, your React app is live on GitHub Pages! π Take a victory lap, tweak some styles, and keep building epic stuff. Youβre officially a deployment dynamo! ππΎ
Made with β€οΈ in Aotearoa.