Learning React and created a basic Tenzies game access here
This guide provides instructions for deploying your Vite application to GitHub Pages, both with and without a custom domain.
- A Vite React project
- Node.js and npm installed
- A GitHub repository for your project
- Create a branch for GitHub Pages to deploy to
npm install --save-dev vite-plugin-gh-pages
Add these scripts to your package.json
:
{
"scripts": {
"build": "vite build",
"predeploy": "npm run build",
"deploy": "vite-gh-pages"
}
}
This method deploys your app to https://<username>.github.io/<repository-name>
.
Update your vite.config.js
:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import ghPages from "vite-plugin-gh-pages";
export default defineConfig({
plugins: [react(), ghPages()],
base: "/<repository-name>/", // Replace with your repository name
});
npm run deploy
Your app will be available at https://<username>.github.io/<repository-name>
This method deploys your app to your custom domain (e.g., https://app.yourdomain.com
).
{
"name": "tenzies-learning-react",
"private": true,
"version": "0.0.0",
"homepage": "https://<subdomain>.<your-domain>/",
"type": "module",
"scripts": {
"predeploy": "npm run build",
"deploy": "vite-gh-pages",
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
}
}
// vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import { ghPages } from "vite-plugin-gh-pages";
import fs from "node:fs";
import path from "node:path";
export default defineConfig({
plugins: [
react(),
ghPages({
// Add hook to persist CNAME file during deployment
onBeforePublish: ({ outDir }) => {
const CNAME = path.join(outDir, "CNAME");
fs.writeFileSync(CNAME, "<subdomain>.<your-domain>"); // Replace with your domain
},
}),
],
base: "/", // Base URL for custom domain
});
This configuration ensures your custom domain persists after each deployment by automatically creating a CNAME file during the build process.
-
In GitHub Organization Settings:
- Go to your profile photo > "Your organizations"
- Click "Settings" next to your organization
- Navigate to "Security" > "Verified and approved domains"
- Click "Add a domain"
- Enter your domain and follow prompts
-
Add DNS TXT Record:
- Type: TXT
- Host/Name: @ or as specified by GitHub
- Value: [GitHub-provided verification string]
- Verify using:
dig <your-TXT-record> +nostats +nocomments +nocmd TXT
Add CNAME record in your domain provider:
- Type: CNAME
- Name:
<your-subdomain>
- Value:
<your-github-username>.github.io
- TTL: 1/2 Hour
- Go to repository Settings > Pages
- Enter your custom domain
- Wait for DNS verification
- Enable "Enforce HTTPS"
Add to your index.html
:
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self';
style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;
font-src 'self' https://fonts.gstatic.com;
img-src 'self' data: https:;
script-src 'self' 'unsafe-inline' 'unsafe-eval';"
/>
-
404 Errors
- Check relative asset paths
- Verify base URL in vite.config.js
- Check build output files
-
DNS Issues
- Allow up to 24 hours for DNS propagation
- Verify CNAME records
- Use GitHub's DNS check feature
-
Asset Loading
- Use relative file paths
- Check Content Security Policy
- Verify build output structure
To update the deployed site:
- Make changes to your code
- Commit and push to main branch
- Run
npm run deploy