fix: windows app blank ui issue
This commit is contained in:
parent
8dbc9e7b68
commit
c02bf766fd
|
|
@ -7,7 +7,9 @@ import (
|
|||
"embed"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -15,6 +17,13 @@ import (
|
|||
//go:embed app/dist
|
||||
var appFS embed.FS
|
||||
|
||||
func init() {
|
||||
mime.AddExtensionType(".js", "application/javascript; charset=utf-8")
|
||||
mime.AddExtensionType(".css", "text/css; charset=utf-8")
|
||||
mime.AddExtensionType(".woff2", "font/woff2")
|
||||
mime.AddExtensionType(".svg", "image/svg+xml")
|
||||
}
|
||||
|
||||
// appHandler returns an HTTP handler that serves the React SPA.
|
||||
// It tries to serve real files first, then falls back to index.html for React Router.
|
||||
func (s *Server) appHandler() http.Handler {
|
||||
|
|
@ -24,11 +33,19 @@ func (s *Server) appHandler() http.Handler {
|
|||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
p := strings.TrimPrefix(r.URL.Path, "/")
|
||||
if _, err := fsys.Open(p); err == nil {
|
||||
// Serve the file directly
|
||||
|
||||
if file, err := fsys.Open(p); err == nil {
|
||||
file.Close()
|
||||
|
||||
// Ensure proper Content-Type headers
|
||||
if contentType := mime.TypeByExtension(filepath.Ext(p)); contentType != "" {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
|
||||
fileServer.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Fallback – serve index.html for unknown paths so React Router works
|
||||
data, err := fs.ReadFile(fsys, "index.html")
|
||||
if err != nil {
|
||||
|
|
@ -39,6 +56,8 @@ func (s *Server) appHandler() http.Handler {
|
|||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
http.ServeContent(w, r, "index.html", time.Time{}, bytes.NewReader(data))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue