Preview builds for branch deployments were failing with the error:
routeConfig.js:1 Uncaught SyntaxError: Unexpected token '<' (at routeConfig.js:1:1)
And the message:
Branch Deployment Not Found
The branch "copilot-fix-1016" does not appear to be deployed.
Even though the branch was deployed to GitHub Pages at:
https://github.com/litlfred/sgex/blob/gh-pages/copilot-fix-1016/index.html
The error occurred because routeConfig.js was returning HTML (containing < character) instead of JavaScript. This happened due to inconsistent path resolution.
In public/index.html, the route configuration script was loaded using a relative path:
<script src="./routeConfig.js"></script>While all other assets used absolute paths with PUBLIC_URL:
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />When a branch deployment is built with PUBLIC_URL="/sgex/copilot-fix-1016/", the relative path ./routeConfig.js could resolve differently depending on the URL format:
Scenario 1: URL without trailing slash
- URL:
https://litlfred.github.io/sgex/copilot-fix-1016 - Browser extracts base:
/sgex/ - Relative path resolves to:
/sgex/routeConfig.js❌ (file doesn't exist at this path!) - Result: 404 HTML page returned instead of JavaScript
Scenario 2: URL with trailing slash
- URL:
https://litlfred.github.io/sgex/copilot-fix-1016/ - Browser extracts base:
/sgex/copilot-fix-1016/ - Relative path resolves to:
/sgex/copilot-fix-1016/routeConfig.js✅ - Result: Correct file loaded
Scenario 3: Using PUBLIC_URL (the fix)
- Built path:
/sgex/copilot-fix-1016/routeConfig.js(absolute) - Always resolves correctly regardless of URL format ✅
Changed public/index.html line 53:
<!-- Before -->
<script src="./routeConfig.js"></script>
<!-- After -->
<script src="%PUBLIC_URL%/routeConfig.js"></script>This makes routeConfig.js consistent with all other assets, using an absolute path that always resolves correctly.
Added missing LandingPage component case to src/services/componentRouteService.js:
case 'LandingPage':
LazyComponent = React.lazy(() => import('../components/LandingPage'));
break;This fixes the console warning: Unknown component LandingPage, using fallback
After the fix, building with PUBLIC_URL="/sgex/test-branch/" produces:
<script src="/sgex/test-branch/routeConfig.js"></script>All assets now use consistent absolute paths:
href="/sgex/test-branch/favicon.ico"href="/sgex/test-branch/logo192.png"src="/sgex/test-branch/routeConfig.js"← Now fixed!src="/sgex/test-branch/static/js/main.xxxxx.js"
Created comprehensive tests in src/tests/RouteConfigPathFix.test.js:
- ✅ Verifies
index.htmlusesPUBLIC_URLfor routeConfig.js - ✅ Verifies all asset paths use
PUBLIC_URLconsistently - ✅ Documents why relative paths fail with different URL formats
- ✅ Verifies
LandingPagecomponent is properly registered - ✅ All 26 related tests pass
- Branch deployments will now load correctly regardless of URL format
- Preview builds will work consistently for all branches
- Route configuration will load properly on first page load
- No breaking changes to existing functionality
The public/404.html file also has <script src="./routeConfig.js"></script> with a relative path, but this is correct because:
- 404.html is always served from the root (
/404.html) - The relative path
./routeConfig.jsalways resolves to/routeConfig.js - The root
routeConfig.jsis deployed by the landing page deployment
So no change was needed for 404.html.
| File | Lines Changed | Description |
|---|---|---|
public/index.html |
1 line | Changed routeConfig.js to use PUBLIC_URL |
src/services/componentRouteService.js |
3 lines | Added LandingPage component case |
src/tests/RouteConfigPathFix.test.js |
94 lines (new) | Comprehensive test coverage |
This fix resolves the preview builds failing issue and ensures that branch deployments work correctly for:
- Feature branches
- Fix branches
- All copilot-generated branches
- Manual branch deployments
The fix is minimal, surgical, and follows the existing pattern used by all other assets in the application.