fix(gateway): serve pre-rendered directories before SPA fallback#32
fix(gateway): serve pre-rendered directories before SPA fallback#32olamide226 merged 1 commit intomainfrom
Conversation
The embedded-mode file server previously rewrote all extension-less paths to / unconditionally, so only root index.html was ever served. This broke frameworks that emit per-route directories with index.html (Next.js with trailingSlash, Gatsby, Hugo, etc.) — users always saw the landing page regardless of the URL. Apply a try_files-style lookup (equivalent to nginx try_files $uri $uri/ /index.html): check whether the requested path maps to a directory containing index.html on disk before falling back to the root. This keeps pure-SPA fallback intact (Vite, CRA) while correctly serving pre-rendered pages for SSG frameworks.
|
❌ Analysis Failed`Invalid request. "sha" wasn't supplied. - https://docs.github.com/rest/repos/contents#create-or-update-file-contents` Troubleshooting
Retry: |
There was a problem hiding this comment.
Pull request overview
This PR fixes the embedded-mode file server to support pre-rendered per-route directories (e.g., from Next.js with trailingSlash, Gatsby, Hugo) by implementing a try_files-style lookup before falling back to the SPA root index.html. Previously, all extension-less paths were unconditionally rewritten to /, breaking frameworks that emit dir/index.html for each route.
Changes:
- Added a filesystem check in
createFileServerto detect directories withindex.htmland serve them directly, falling back to rootindex.htmlonly when no matching directory exists - Added comprehensive tests covering pre-rendered directories, SPA fallback, static assets, 404s, and directory listing prevention
- Added test fixture files (
signin/index.html,style.css) to support the new tests
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
gateway/internal/server/server.go |
Added try_files-style directory check before SPA fallback in the file server handler |
gateway/internal/server/server_test.go |
Added 8 tests covering all routing scenarios for the SPA-aware file server |
gateway/testdata/static/signin/index.html |
Test fixture: pre-rendered page for /signin/ route |
gateway/testdata/static/style.css |
Test fixture: static CSS asset |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
/, so only the rootindex.htmlwas ever served — regardless of URLindex.html(Next.js withtrailingSlash, Gatsby, Hugo, etc.) were broken: users always saw the landing pagetry_files-style lookup (equivalent to nginxtry_files $uri $uri/ /index.html): check whether the requested path maps to a directory containingindex.htmlon disk before falling back to rootdir/index.htmlon disk are served directlyHow it works
/signin/signin/index.htmlexistsindex.html(wrong)signin/index.html/signinsignin/index.htmlexistsindex.html/signin/→signin/index.html/dashboardindex.htmlindex.html(unchanged)/app.js/missing.jsTest plan
TestFileServer_RootServesIndexHTML— root servesindex.htmlTestFileServer_StaticAssetServedDirectly— files with extensions served as-isTestFileServer_PrerenderedDirWithTrailingSlash—/signin/servessignin/index.htmlTestFileServer_PrerenderedDirWithoutTrailingSlash—/signinredirects to/signin/TestFileServer_SPAFallbackForUnknownRoute— unknown routes fall back to rootTestFileServer_SPAFallbackForTrailingSlashNoDir— trailing slash with no dir falls back (pure SPA)TestFileServer_MissingStaticAsset404— missing assets with extensions return 404TestFileServer_DirWithoutIndexHTMLFallsBack— dir withoutindex.htmlfalls back to root (no dir listing)go test ./...passes