Skip to content

Commit d573f63

Browse files
authored
Fix 404 Error on projects after hosted on github pages - build-and-deploy.yml
1 parent ae7847a commit d573f63

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

.github/workflows/build-and-deploy.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ jobs:
1616
with:
1717
node-version: '18'
1818

19-
- name: Build all React projects
19+
- name: Install json tool for config edits
20+
run: npm install -g json
21+
22+
- name: Build all Vite React projects
2023
run: |
2124
projects=(
2225
"01. Tic-Tac-Toe"
@@ -35,15 +38,21 @@ jobs:
3538
rm -rf output
3639
mkdir -p output
3740
41+
repo_name="${GITHUB_REPOSITORY#*/}"
42+
3843
for project in "${projects[@]}"; do
39-
echo "Building $project"
44+
echo "========================"
45+
echo "📦 Building $project"
46+
echo "========================"
47+
4048
cd "$project"
4149
npm install
4250
43-
# Add homepage for GitHub Pages
44-
repo_name="${GITHUB_REPOSITORY#*/}"
45-
homepage="https://$GITHUB_REPOSITORY_OWNER.github.io/$repo_name/$project"
46-
npx json -I -f package.json -e "this.homepage=\"$homepage\""
51+
# Inject Vite config with correct base path
52+
base="/$repo_name/$project/"
53+
echo "import { defineConfig } from 'vite';" > vite.config.js
54+
echo "import react from '@vitejs/plugin-react';" >> vite.config.js
55+
echo "export default defineConfig({ base: '$base', plugins: [react()] });" >> vite.config.js
4756
4857
npm run build
4958
cd ..
@@ -61,7 +70,7 @@ jobs:
6170
git remote add origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
6271
git checkout -b docs
6372
64-
touch .nojekyll # Avoid GitHub Pages ignoring folders starting with underscore
73+
touch .nojekyll
6574
git add .
6675
git commit -m "Deploy React projects to GitHub Pages [docs branch]"
6776
git push --force origin docs

0 commit comments

Comments
 (0)