Fix content type issue (#3424)

Fix content type issue
This commit is contained in:
Ramon de Klein
2024-08-14 01:49:06 +02:00
committed by GitHub
parent 3c34602f9e
commit 8d13be5e87

View File

@@ -313,6 +313,12 @@ func AuthenticationMiddleware(next http.Handler) http.Handler {
// FileServerMiddleware serves files from the static folder // FileServerMiddleware serves files from the static folder
func FileServerMiddleware(next http.Handler) http.Handler { func FileServerMiddleware(next http.Handler) http.Handler {
buildFs, err := fs.Sub(portal_ui.GetStaticAssets(), "build")
if err != nil {
panic(err)
}
spaFileHandler := wrapHandlerSinglePageApplication(requestBounce(http.FileServer(http.FS(buildFs))))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", globalAppName) // do not add version information w.Header().Set("Server", globalAppName) // do not add version information
switch { switch {
@@ -321,11 +327,7 @@ func FileServerMiddleware(next http.Handler) http.Handler {
case strings.HasPrefix(r.URL.Path, "/api"): case strings.HasPrefix(r.URL.Path, "/api"):
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
default: default:
buildFs, err := fs.Sub(portal_ui.GetStaticAssets(), "build") spaFileHandler.ServeHTTP(w, r)
if err != nil {
panic(err)
}
wrapHandlerSinglePageApplication(requestBounce(http.FileServer(http.FS(buildFs)))).ServeHTTP(w, r)
} }
}) })
} }
@@ -424,13 +426,10 @@ func handleSPA(w http.ResponseWriter, r *http.Request) {
} }
indexPageBytes = replaceLicense(indexPageBytes) indexPageBytes = replaceLicense(indexPageBytes)
mimeType := mimedb.TypeByExtension(filepath.Ext(r.URL.Path)) // it's important to force "Content-Type: text/html", because a previous
// handler may have already set the content-type to a different value.
if mimeType == "application/octet-stream" { // (i.e. the FileServer when it detected that it couldn't find the file)
mimeType = "text/html" w.Header().Set("Content-Type", "text/html")
}
w.Header().Set("Content-Type", mimeType)
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(indexPageBytes)) http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(indexPageBytes))
} }