This document describes the 404.html implementation for proper GitHub Pages SPA (Single Page Application) routing in the SGEX Workbench project.
- Source:
public/404.html - Build Output:
build/404.html(automatically copied duringnpm run build) - Deployment: Root of gh-pages branch
The 404.html file enables proper client-side routing for a React SPA deployed on GitHub Pages by:
- Handling direct URL access: When users navigate directly to a route (e.g.,
/sgex/dashboard/user/repo), GitHub Pages serves 404.html instead of the missing file - SPA routing conversion: The 404.html contains JavaScript that converts the 404 URL into a query parameter format that can be processed by the React app
- Preserving navigation state: Maintains the intended route so the React Router can handle it properly
- Current size: 4,987 bytes
- Minimum requirement: 512 bytes (for Internet Explorer compatibility)
- ✅ Status: Meets requirement
Based on the spa-github-pages pattern with SGEX-specific enhancements.
Handles different deployment scenarios:
- Landing page:
/sgex/(from deploy branch) - Main branch:
/sgex/main/dashboard/... - Feature branches:
/sgex/branch-name/dashboard/... - Standalone deployment:
/dashboard/...
- Uses
routeConfig.jsfor intelligent route detection - Distinguishes between branch names and DAK component names
- Supports dynamic branch deployments
Converts URLs like:
https://litlfred.github.io/sgex/dashboard/user/repo
Into:
https://litlfred.github.io/sgex/?/dashboard/user/repo
The React app then processes the query parameter ?/dashboard/user/repo to restore the intended route.
Implements intelligent fallback for non-existent branch deployments:
- Local Branch First: If accessing
/sgex/feature-branch/dashboard/user/repofails multiple times, first try/sgex/feature-branch/ - Main Deployment Last: Only if branch root also fails, fall back to
/sgex/ - Redirect Loop Prevention: Uses reduced threshold (2 attempts instead of 3) for faster fallback
- Context Preservation: Maintains user/repo context during fallback transitions
Fallback Sequence Example:
1. User accesses: /sgex/feature-branch/dashboard/user/repo (404)
2. First fallback: /sgex/feature-branch/ (404)
3. Final fallback: /sgex/ (main deployment)
Original URL: /sgex/dashboard/user/repo
404.html serves and redirects to: /sgex/?/dashboard/user/repo
React Router processes: /dashboard/user/repo
Original URL: /sgex/main/dashboard/user/repo
404.html serves and redirects to: /sgex/main/?/dashboard/user/repo
React Router processes: /dashboard/user/repo (within main branch context)
Original URL: /dashboard/user/repo
404.html serves and redirects to: /?/dashboard/user/repo
React Router processes: /dashboard/user/repo
- routeConfig.js: Provides configuration for route detection
- index.html: Contains corresponding SPA routing script to process redirected URLs
The 404.html relies on getSGEXRouteConfig() function from routeConfig.js to:
- Identify deployed branches (
isDeployedBranch()) - Validate DAK components (
isValidDAKComponent()) - Make intelligent routing decisions
- Modern browsers: Full support
- Internet Explorer: Requires >512 bytes (✅ Current: 4,987 bytes)
- All browsers: Graceful fallback if JavaScript is disabled
src/tests/404-routing.test.js: Validates file presence, size, and content- Build process automatically includes 404.html in output
- Build the project:
npm run build - Serve the build directory with a web server
- Navigate to non-existent routes
- Verify 404.html is served and redirects properly
- Check browser console for routing debug messages
Use /tmp/test-404-functionality.sh for comprehensive validation:
chmod +x /tmp/test-404-functionality.sh
/tmp/test-404-functionality.shThe 404.html includes console logging for development:
console.log('GitHub Pages Branch Deployment Routing:', {
branch: potentialBranch,
originalPath: l.pathname,
redirectPath: redirectPath,
routePath: routePath
});This helps developers understand how URLs are being processed during development.
- Update route configuration in
routes-config.json - No changes needed to 404.html (uses dynamic configuration)
- Update logic in 404.html if needed
- Update
routeConfig.jsconfiguration - Test with new deployment scenario
- ✅ File exists in
public/404.html - ✅ File is copied to
build/404.htmlduring build - ✅ File size >512 bytes (IE compatibility)
- ✅ Contains SPA routing script
- ✅ Uses getSGEXRouteConfig()
- ✅ Handles GitHub Pages deployment
- ✅ Handles branch deployments
- ✅ Handles standalone deployments
- ✅ Preserves query parameters and hash
- ✅ Includes debug logging
- ✅ Has automated tests
The 404.html only processes URL paths and doesn't execute any external code. It:
- Only redirects within the same origin
- Doesn't process external parameters
- Uses browser's native
location.replace()for redirection - Follows GitHub Pages security model
- Minimal JavaScript execution time
- No external dependencies beyond routeConfig.js
- Efficient path processing
- No network requests beyond the redirect