diff --git a/internal/server/routes.go b/internal/server/routes.go index 14aedbd..28bf2c3 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -3,6 +3,8 @@ package server import ( "database/sql" "net/http" + "path/filepath" + "strings" "mal/api/anime" "mal/api/auth" @@ -22,6 +24,23 @@ type Config struct { PlaybackProxySecret string } +func withMimeTypes(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ext := strings.ToLower(filepath.Ext(r.URL.Path)) + switch ext { + case ".js": + w.Header().Set("Content-Type", "application/javascript") + case ".css": + w.Header().Set("Content-Type", "text/css") + case ".svg": + w.Header().Set("Content-Type", "image/svg+xml") + case ".json": + w.Header().Set("Content-Type", "application/json") + } + next.ServeHTTP(w, r) + }) +} + func NewRouter(cfg Config) http.Handler { mux := http.NewServeMux() @@ -40,7 +59,7 @@ func NewRouter(cfg Config) http.Handler { // Serve built frontend assets dist := http.FileServer(http.Dir("./dist")) - mux.Handle("/dist/", http.StripPrefix("/dist/", dist)) + mux.Handle("/dist/", http.StripPrefix("/dist/", withMimeTypes(dist))) mux.HandleFunc("/", animeHandler.HandleCatalog) mux.HandleFunc("/discover", animeHandler.HandleDiscover)