Fix SPA routing: serve index.html for non-file paths
All checks were successful
CI / Go tests & lint (push) Successful in 8s
CI / Frontend tests & type-check (push) Successful in 39s
CI / Go tests & lint (pull_request) Successful in 9s
CI / Frontend tests & type-check (pull_request) Successful in 40s

Replace bare http.FileServer with an SPA-aware handler that tries to
serve the requested static file and falls back to index.html when
the path doesn't match a real file. This lets React Router handle
client-side routes like /schedules on page reload.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-08 19:10:07 -03:00
parent 0af53c9b55
commit c78a35377a

View File

@@ -94,8 +94,25 @@ func New(db *sql.DB, jwtSecret string, staticDir string) http.Handler {
}) })
}) })
// Serve static React app for all other routes // Serve static React app for all other routes, with SPA fallback
r.Handle("/*", http.FileServer(http.Dir(staticDir))) r.Handle("/*", spaHandler(staticDir))
return r return r
} }
// spaHandler serves static files from dir, falling back to index.html for
// paths that don't match a file on disk (so client-side routing works).
func spaHandler(dir string) http.HandlerFunc {
fs := http.Dir(dir)
fileServer := http.FileServer(fs)
return func(w http.ResponseWriter, r *http.Request) {
// Try to open the requested path as a static file.
if f, err := fs.Open(r.URL.Path); err == nil {
f.Close()
fileServer.ServeHTTP(w, r)
return
}
// Not a real file — serve index.html and let React Router handle it.
http.ServeFile(w, r, dir+"/index.html")
}
}