This tutorial is derived from https://github.com/gitname/react-gh-pages.
To view this Vite + React app as a github page vist https://cesardgm.github.io/my-vite-app.
- Node.js and npm (Node Package Manager)
- Recommended versions: Node.js v20.11.1 or later, npm 10.2.4 or later
- Git (version 2.34.1 or later)
- GitHub account
- Log in to your GitHub account.
- Create a new public repository to host your project's source code.
- For this guide, we'll use
my-vite-appas the repository name. Replace this with your preferred name.
-
Create a new Vite app:
npm create vite@latest my-vite-app- Select
Reactas the framework - Choose
TypeScript + SWCas the variant
- Select
-
Navigate to the project directory and install dependencies:
cd my-vite-app npm install -
Install the
gh-pagespackage as a development dependency:npm install gh-pages --save-dev -
Update the
package.jsonfile:- Add a
homepageproperty:{ "name": "my-vite-app", "version": "0.0.0", "homepage": "https://{username}.github.io/my-vite-app", "private": true, - Add deployment scripts:
"scripts": { "predeploy": "npm run build", "deploy": "gh-pages -d build", "dev": "vite", "build": "tsc -b && vite build", "lint": "eslint .", "preview": "vite preview" },
- Add a
-
Modify the
vite.config.tsfile:import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react-swc'; export default defineConfig({ plugins: [react()], build: { outDir: 'build', }, base: "/my-vite-app/", });
-
Initialize Git and add the remote repository:
git init git remote add origin git@github.com:{username}/my-vite-app.git -
Build and deploy the app:
npm run build npm run deploy -
Configure GitHub Pages:
- Go to your repository on GitHub
- Navigate to Settings > Pages
- Set "Source" to "gh-pages" branch, folder "/ (root)"
- Click "Save"
-
Your app is now live at
https://{username}.github.io/my-vite-app -
Push the source code to GitHub:
git add . git commit -m "Initial commit" git push -u origin main
- Replace
{username}with your GitHub username throughout this guide. - Ensure your repository is public, or upgrade to GitHub Pro for private repository support.
- The
baseproperty invite.config.tsshould match your repository name. - Clear the cache in your browser to ensure you are loading the latest deployment.
By following these steps, you'll successfully deploy your Vite + React app to GitHub Pages with a custom domain.
-
Prepare a
favicon.icofile and place it at the root level of your repository. -
Domain Registration and DNS Configuration:
- Purchase a domain through AWS Route 53.
- Navigate to the hosted zone for your purchased domain.
- Don't delete any of the default records.
- Create a
Simple Recordof typeA:- Set
Aliasto No - Enter the following IP addresses for
Value/Route traffic to:185.199.108.153 185.199.109.153 185.199.110.153 185.199.111.153
- Set
-
GitHub Domain Verification:
- Go to your GitHub account settings > Pages (under Code, planning, and automation).
- Click "Add a verified domain" and enter your AWS Route 53 domain.
- Copy the provided TXT record subdomain and value.
- In AWS Route 53, create a TXT record:
- Subdomain: Replace
wwwwith the GitHub-provided value (typically_github-pages-challenge-{username}) - Value: Paste the GitHub-provided code
- Subdomain: Replace
- Create a CNAME record:
- Type: CNAME
- Value/Route traffic to:
{username}.github.io
- Return to GitHub and click "Verify".
-
Local Repository Configuration:
- Create a file named
CNAME(without any extension) in the root directory of your local repository. - Add a single line to the file:
www.{customDomain}.{extension}
- Create a file named
-
Update Vite Configuration:
- Modify
vite.config.ts:- Change the
baseproperty from/my-vite-app/to/
- Change the
- Modify
-
Rebuild and Redeploy:
npm run build npm run deploy -
GitHub Repository Settings:
- Navigate to your repository's settings > Pages (under Code and automation).
- Under "Custom domain", enter your verified domain.
- Click "Save" and enable "Enforce HTTPS".
-
Completion: Your Vite + React app is now deployed and accessible via your custom domain with HTTPS enforced.
- Replace
{username}with your GitHub username. - Replace
{customDomain}and{extension}with your actual domain name and top-level domain. - Ensure all DNS changes are propagated, which may take up to 48 hours.
- Regularly check and renew your domain registration and SSL certificate to maintain uninterrupted service.
By following these steps, you'll successfully configure your AWS Route 53 domain with GitHub Pages, providing a professional and secure hosting solution for your Vite + React application.