|
| 1 | +# GitHub Pages 404 Error Fix |
| 2 | + |
| 3 | +## Issue Description |
| 4 | + |
| 5 | +When deploying to GitHub Pages in demo mode, navigation functions (like `navigateToRegister()`) were causing 404 errors by trying to access `https://martinmeer.github.io/` instead of the correct path `https://martinmeer.github.io/my-car-tech-tracker-front/`. |
| 6 | + |
| 7 | +## Root Cause |
| 8 | + |
| 9 | +The problem was in the `NavigationService` URL construction using JavaScript's `URL` constructor: |
| 10 | + |
| 11 | +```typescript |
| 12 | +// ❌ WRONG - Strips base path when hash is in first parameter |
| 13 | +const url = new URL('/#/register', 'https://martinmeer.github.io/my-car-tech-tracker-front'); |
| 14 | +// Results in: https://martinmeer.github.io/#/register |
| 15 | + |
| 16 | +// ✅ CORRECT - Concatenate strings then create URL |
| 17 | +let url = `${baseUrl}/#/register`; |
| 18 | +const urlObj = new URL(url); // For adding query params if needed |
| 19 | +``` |
| 20 | + |
| 21 | +### Why This Happens |
| 22 | + |
| 23 | +When using `new URL(path, base)`: |
| 24 | +- If `path` starts with `/`, it **replaces** the path from the base URL |
| 25 | +- A hash `#` in the path makes it a fragment, which further complicates resolution |
| 26 | +- Result: `new URL('/#/login', 'https://example.com/my-app')` → `https://example.com/#/login` |
| 27 | + |
| 28 | +## Files Fixed |
| 29 | + |
| 30 | +### 1. Marketing Site Navigation |
| 31 | +**File:** `apps/marketing-site/src/services/NavigationService.ts` |
| 32 | + |
| 33 | +**Changes:** |
| 34 | +- `navigateToLogin()` - Fixed URL construction |
| 35 | +- `navigateToRegister()` - Fixed URL construction |
| 36 | +- `navigateToAccount()` - Fixed URL construction |
| 37 | +- `navigateToMarketing()` - Fixed URL construction |
| 38 | + |
| 39 | +### 2. Main App Navigation |
| 40 | +**File:** `apps/main-app/src/services/NavigationService.ts` |
| 41 | + |
| 42 | +**Changes:** |
| 43 | +- `navigateToLogin()` - Fixed URL construction |
| 44 | +- `navigateToMarketing()` - Fixed URL construction |
| 45 | + |
| 46 | +### 3. Configuration Comments |
| 47 | +**Files:** |
| 48 | +- `apps/marketing-site/src/config/app.config.js` |
| 49 | +- `apps/main-app/src/config/app.config.js` |
| 50 | + |
| 51 | +**Changes:** |
| 52 | +- Added clear comments explaining demo mode configuration |
| 53 | +- Clarified that the placeholder API URL enables localStorage/demo mode |
| 54 | + |
| 55 | +## How Demo Mode Works |
| 56 | + |
| 57 | +The app is configured to run in **demo mode** (without backend) when deployed to GitHub Pages: |
| 58 | + |
| 59 | +1. **API URL Check:** The `ConfigService.shouldUseBackend()` checks if API_URL contains `'your-api-domain.com'` or `'localhost'` |
| 60 | +2. **Demo Detection:** If it finds these placeholders, it returns `false` → uses localStorage |
| 61 | +3. **No Backend Needed:** All data is stored in browser localStorage, perfect for demos |
| 62 | + |
| 63 | +### To Enable Real Backend |
| 64 | + |
| 65 | +If you want to connect to a real backend API in production: |
| 66 | + |
| 67 | +1. Update the API_URL in config files: |
| 68 | +```javascript |
| 69 | +production: { |
| 70 | + MARKETING_URL: 'https://martinmeer.github.io/my-car-tech-tracker-front', |
| 71 | + MAIN_APP_URL: 'https://martinmeer.github.io/my-car-tech-tracker-front/app', |
| 72 | + API_URL: 'https://your-actual-backend-api.com/api' // Your real backend |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +2. Deploy your backend and ensure CORS is configured |
| 77 | +3. Rebuild and redeploy both apps |
| 78 | + |
| 79 | +## Testing the Fix |
| 80 | + |
| 81 | +After deploying the fixed version: |
| 82 | + |
| 83 | +1. **Navigate to marketing site:** `https://martinmeer.github.io/my-car-tech-tracker-front/` |
| 84 | +2. **Click "Register" or "Login"** - Should navigate correctly |
| 85 | +3. **Register a new user** - Should work with localStorage |
| 86 | +4. **Navigate to main app** - Should redirect correctly to `/app` |
| 87 | + |
| 88 | +## Deployment |
| 89 | + |
| 90 | +To deploy the fix: |
| 91 | + |
| 92 | +```bash |
| 93 | +# Build both apps |
| 94 | +npm run build |
| 95 | + |
| 96 | +# Or deploy via GitHub Actions (automatically triggered on push to main) |
| 97 | +git add . |
| 98 | +git commit -m "Fix navigation URL construction for GitHub Pages deployment" |
| 99 | +git push origin main |
| 100 | +``` |
| 101 | + |
| 102 | +The GitHub Actions workflow will automatically: |
| 103 | +1. Build both apps with `NODE_ENV=production` |
| 104 | +2. Deploy marketing site to root |
| 105 | +3. Deploy main app to `/app` subdirectory |
| 106 | +4. Create `.nojekyll` file to prevent Jekyll processing |
| 107 | + |
| 108 | +## Verification Checklist |
| 109 | + |
| 110 | +After deployment, verify: |
| 111 | +- [ ] Marketing site loads at root URL |
| 112 | +- [ ] Main app loads at `/app` URL |
| 113 | +- [ ] Navigation between pages works without 404 errors |
| 114 | +- [ ] Registration works (saves to localStorage) |
| 115 | +- [ ] Login works (uses localStorage) |
| 116 | +- [ ] Data persists across page reloads |
| 117 | +- [ ] Browser console shows no 404 errors |
| 118 | +- [ ] ConfigService debug info shows `Using Backend: false` |
| 119 | + |
| 120 | +## Debug Tips |
| 121 | + |
| 122 | +If issues persist: |
| 123 | + |
| 124 | +1. **Check browser console:** |
| 125 | +```javascript |
| 126 | +// In browser console |
| 127 | +ConfigService.logDebugInfo(); |
| 128 | +// Should show: Using Backend: false |
| 129 | +``` |
| 130 | + |
| 131 | +2. **Check localStorage:** |
| 132 | +```javascript |
| 133 | +// In browser console |
| 134 | +console.log(localStorage.getItem('auth_token')); |
| 135 | +console.log(localStorage.getItem('user_data')); |
| 136 | +``` |
| 137 | + |
| 138 | +3. **Check GitHub Actions:** |
| 139 | + - Go to repository → Actions tab |
| 140 | + - Verify "Deploy to GitHub Pages" workflow succeeded |
| 141 | + - Check build logs for errors |
| 142 | + |
| 143 | +4. **Check GitHub Pages settings:** |
| 144 | + - Repository → Settings → Pages |
| 145 | + - Source should be: **GitHub Actions** (not "Deploy from a branch") |
| 146 | + |
| 147 | +## Additional Notes |
| 148 | + |
| 149 | +- Both apps run independently in demo mode |
| 150 | +- No backend server required for GitHub Pages deployment |
| 151 | +- Perfect for demonstrations, prototypes, or portfolio showcases |
| 152 | +- When ready for production, simply update the API_URL and deploy a backend |
| 153 | + |
0 commit comments