Deploying a React app built with Vite to GitHub Pages involves several steps utilizing the gh-pages package: install gh-pages.
npm install gh-pages
Configure package.json: Add the following scripts to your package.json file to automate the build and deployment process:
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d dist"
}
The predeploy script ensures your app is built (npm run build) before deployment, and deploy uses gh-pages to publish the contents of the dist folder to the gh-pages branch.
Configure vite.config.js: Add a base option to your vite.config.js file, setting it to your GitHub repository's name (prefixed with a slash). This ensures correct asset paths on GitHub Pages.
JavaScript
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
base: "/YOUR_REPOSITORY_NAME/",
});
Initialize Git and Push to GitHub: If you haven't already, initialize a Git repository, commit your changes, and push them to your GitHub repository.
git init
git add .
git commit -m "Initial commit"
git remote add origin YOUR_REPOSITORY_URL
git push -u origin main
Deploy: Run the deploy script to build your app and push it to the gh-pages branch:
npm run deploy
Configure GitHub Pages:
Navigate to your GitHub repository on the web.
Go to Settings > Pages.
Under "Build and deployment", select Branch as the source, and choose gh-pages as the branch.
Click Save.
Your Vite React app should now be accessible at https://YOUR_USERNAME.github.io/YOUR_REPOSITORY_NAME/.
Comments
Post a Comment