Files
2026-08-06 18:26:45 +12:00

31 lines
746 B
Go

package main
import (
"io/fs"
"net/http"
"strings"
)
// spaHandler serves the embedded Svelte build, falling back to index.html
// for any path that isn't a real static asset — this is what lets the
// client-side router handle "deep" URLs like /cars/123 on a hard refresh.
func spaHandler(staticFS fs.FS) http.Handler {
fileServer := http.FileServer(http.FS(staticFS))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
if path == "" {
path = "index.html"
}
if _, err := fs.Stat(staticFS, path); err != nil {
indexReq := r.Clone(r.Context())
indexReq.URL.Path = "/"
fileServer.ServeHTTP(w, indexReq)
return
}
fileServer.ServeHTTP(w, r)
})
}