Skip to content

Commit 9c586f5

Browse files
committed
Fix GitHub Pages 404 - use dynamic base path
1 parent 6754d20 commit 9c586f5

File tree

4 files changed

+200
-1
lines changed

4 files changed

+200
-1
lines changed

.github/workflows/deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ jobs:
3232

3333
- name: Build
3434
run: npm run build
35+
env:
36+
VITE_BASE_PATH: ${{ github.event.repository.name }}
3537

3638
- name: Upload artifact
3739
uses: actions/upload-pages-artifact@v3

GITHUB_PAGES_FIX.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# GitHub Pages 404 Fix
2+
3+
## What I Fixed
4+
5+
The 404 error was caused by a mismatch between the base path in `vite.config.ts` and your actual repository name.
6+
7+
### Changes Made:
8+
9+
1. **Updated `vite.config.ts`**:
10+
- Changed from hardcoded `base: '/sales-tracker/'`
11+
- To dynamic: `base: process.env.VITE_BASE_PATH ? '/${process.env.VITE_BASE_PATH}/' : '/'`
12+
- This automatically uses your repository name
13+
14+
2. **Updated `.github/workflows/deploy.yml`**:
15+
- Added environment variable: `VITE_BASE_PATH: ${{ github.event.repository.name }}`
16+
- This passes the repo name to Vite during build
17+
18+
3. **Added `public/.nojekyll`**:
19+
- Prevents GitHub Pages from ignoring files starting with underscore
20+
- Required for Vite builds
21+
22+
## How to Deploy
23+
24+
### Commit and push these changes:
25+
26+
```bash
27+
git add .
28+
git commit -m "Fix GitHub Pages 404 error"
29+
git push origin main
30+
```
31+
32+
The GitHub Action will automatically:
33+
1. Build the app with the correct base path
34+
2. Deploy to GitHub Pages
35+
36+
## Verify Deployment
37+
38+
1. **Check GitHub Actions**:
39+
- Go to your repo → Actions tab
40+
- Wait for the "Deploy to GitHub Pages" workflow to complete (green checkmark)
41+
42+
2. **Check GitHub Pages Settings**:
43+
- Go to Settings → Pages
44+
- Should show: "Your site is live at https://YOUR_USERNAME.github.io/YOUR_REPO_NAME/"
45+
46+
3. **Access Your App**:
47+
- Click the URL or visit it directly
48+
- Should load without 404 errors
49+
50+
## Common Issues & Solutions
51+
52+
### Issue 1: Still getting 404 after deployment
53+
54+
**Check:**
55+
- Is the workflow completed successfully? (Green checkmark in Actions tab)
56+
- Is GitHub Pages enabled? (Settings → Pages → Source should be "GitHub Actions")
57+
- Wait 2-3 minutes after deployment completes
58+
59+
**Fix:**
60+
```bash
61+
# Clear browser cache or try incognito mode
62+
# Or force a new deployment:
63+
git commit --allow-empty -m "Trigger deployment"
64+
git push origin main
65+
```
66+
67+
### Issue 2: Assets (CSS/JS) not loading
68+
69+
**Symptoms:**
70+
- Page loads but looks broken
71+
- Console shows 404 for `.js` and `.css` files
72+
73+
**Fix:**
74+
This should be fixed by the changes above. If still happening:
75+
1. Check that `public/.nojekyll` file exists
76+
2. Verify `vite.config.ts` has the dynamic base path
77+
78+
### Issue 3: Wrong repository name in URL
79+
80+
**If your repo is named differently:**
81+
82+
Option A: Update `package.json`:
83+
```json
84+
"homepage": "https://YOUR_USERNAME.github.io/YOUR_ACTUAL_REPO_NAME"
85+
```
86+
87+
Option B: Rename your repository:
88+
1. Go to Settings → General
89+
2. Scroll to "Repository name"
90+
3. Rename to match the URL you want
91+
92+
### Issue 4: Using a custom domain
93+
94+
If you want to use a custom domain (e.g., `myapp.com`):
95+
96+
1. Add `CNAME` file to `public/` folder:
97+
```
98+
myapp.com
99+
```
100+
101+
2. Update `vite.config.ts`:
102+
```typescript
103+
base: process.env.VITE_BASE_PATH ? `/${process.env.VITE_BASE_PATH}/` : '/',
104+
```
105+
becomes:
106+
```typescript
107+
base: '/', // Root path for custom domain
108+
```
109+
110+
3. Configure DNS:
111+
- Add CNAME record pointing to `YOUR_USERNAME.github.io`
112+
113+
4. In GitHub Settings → Pages:
114+
- Enter your custom domain
115+
- Enable "Enforce HTTPS"
116+
117+
## Testing Locally
118+
119+
To test the production build locally:
120+
121+
```bash
122+
# Build with production settings
123+
npm run build
124+
125+
# Preview the build
126+
npx vite preview
127+
```
128+
129+
This will serve the built files at `http://localhost:4173` (or similar).
130+
131+
## Alternative: Deploy to Root Domain
132+
133+
If you want your app at `https://YOUR_USERNAME.github.io/` (root, not `/repo-name/`):
134+
135+
1. Rename your repository to `YOUR_USERNAME.github.io`
136+
2. Update `vite.config.ts`:
137+
```typescript
138+
base: '/',
139+
```
140+
3. Push changes
141+
142+
## Debugging Checklist
143+
144+
- [ ] GitHub Actions workflow completed successfully
145+
- [ ] GitHub Pages is enabled (Settings → Pages)
146+
- [ ] Source is set to "GitHub Actions" (not a branch)
147+
- [ ] `public/.nojekyll` file exists
148+
- [ ] `vite.config.ts` has dynamic base path
149+
- [ ] Waited 2-3 minutes after deployment
150+
- [ ] Tried in incognito/private browsing mode
151+
- [ ] Checked browser console for specific errors
152+
153+
## GitHub Pages Settings
154+
155+
Your Pages settings should look like this:
156+
157+
```
158+
Build and deployment
159+
├─ Source: GitHub Actions
160+
└─ Custom domain: (empty, unless using custom domain)
161+
162+
Your site is live at:
163+
https://YOUR_USERNAME.github.io/YOUR_REPO_NAME/
164+
```
165+
166+
## Need More Help?
167+
168+
1. **Check the Actions log**:
169+
- Go to Actions tab
170+
- Click on the latest workflow run
171+
- Check "Build" and "Deploy" steps for errors
172+
173+
2. **Check browser console**:
174+
- Open DevTools (F12)
175+
- Look for 404 errors
176+
- Note which files are missing
177+
178+
3. **Verify the build**:
179+
- Download the artifact from Actions
180+
- Check if `index.html` references correct paths
181+
182+
## Success Indicators
183+
184+
✅ GitHub Action shows green checkmark
185+
✅ Pages settings shows "Your site is live"
186+
✅ Visiting the URL loads the app
187+
✅ No 404 errors in browser console
188+
✅ App functions correctly (can configure API, trace tokens)
189+
190+
## Next Steps After Successful Deployment
191+
192+
1. **Configure your API key** in the deployed app
193+
2. **Test token tracing** with a known transaction
194+
3. **Share the URL** with others
195+
4. **Monitor Alchemy API usage** in your dashboard
196+
197+
Remember: Your Alchemy API key will be visible in the deployed JavaScript. Use a separate key with rate limits for production!

public/.nojekyll

Whitespace-only changes.

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import react from '@vitejs/plugin-react'
44
// https://vitejs.dev/config/
55
export default defineConfig({
66
plugins: [react()],
7-
base: '/sales-tracker/',
7+
base: process.env.VITE_BASE_PATH ? `/${process.env.VITE_BASE_PATH}/` : '/',
88
build: {
99
outDir: 'dist',
1010
sourcemap: true,

0 commit comments

Comments
 (0)