Skip to content

Deploy React JS App to GitHub Pages

Published:

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! ๐Ÿ˜Ž

Table of contents

Open Table of contents

๐Ÿ› ๏ธ Set the Stage in 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]/",

๐Ÿ› ๏ธ Craft a GitHub Workflow Like a Pro ๐Ÿ› ๏ธ

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

๐ŸŒ Create a GitHub Repo & Push It Real Good ๐Ÿ“ค

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

โš™๏ธ Flip the GitHub Workflow Switch ๐Ÿ”ง

Activate those GitHub Actions like youโ€™re flipping on a spaceshipโ€™s hyperdrive! ๐Ÿš€

๐Ÿ›ค๏ธ Add React Router for Smooth Navigation ๐Ÿ—บ๏ธ

Make your app a multi-page marvel with React Router. Install it with Bun and keep the party going! ๐ŸŽ‰

bun add react-router-dom

๐Ÿ  Set Your Homepage in package.json ๐Ÿก

Let the world know where your app lives. Add this to your package.json for that polished URL touch. ๐ŸŒ

{
  "homepage": "[PROJECT_URL]"
}

๐Ÿ›‘ Handle 404s Like a Champ with 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>

๐Ÿง  Add a Smart Script toย index.htmlย ๐Ÿงฉ

Pop this script into the 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>

๐Ÿ“ก Beam It Up to GitHub! ๐Ÿš€

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

git add -A && git commit -m "Building" && git push

๐ŸŽ‰ Houston, We Have Liftoff! ๐Ÿช

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! ๐Ÿ˜Ž๐Ÿ’พ


Share this post on:

Previous Post
My Easiest way to Start with React JS App
Next Post
Easily add Dark Mode & Button in React JS with Tailwind CSS