Skip to content

Commit 662bb7c

Browse files
committed
move default Content-Type from middleware to function
1 parent e6cb9bd commit 662bb7c

File tree

1 file changed

+30
-41
lines changed

1 file changed

+30
-41
lines changed

server.go

+30-41
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ func Run(ctx context.Context, site Site) error {
6161

6262
router := chi.NewRouter()
6363
router.Use(middleware.Logger)
64-
router.Use(setDefaultContentType) // Needs to come before compress.
6564
router.Use(middleware.Compress(5))
6665
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
6766
pageInfo := PageInfo{
@@ -563,6 +562,7 @@ func Run(ctx context.Context, site Site) error {
563562
return fmt.Errorf("getting static sub-embed: %w", err)
564563
}
565564
router.Get("/static/*", func(w http.ResponseWriter, r *http.Request) {
565+
setDefaultContentType(w, r)
566566
fs := http.StripPrefix("/static", http.FileServer(http.FS(sub)))
567567
fs.ServeHTTP(w, r)
568568
})
@@ -781,46 +781,35 @@ func shortHash(hash string) string {
781781
return hash
782782
}
783783

784-
// setDefaultContentType sets the Content-Type header for files with an
785-
// extension.
784+
// setDefaultContentType sets a default Content-Type header.
786785
// This is in order for the compress middleware to work correctly.
787786
// For explicit paths in the router, the Content-Type *should* be set manually.
788-
// Hence this is for files that are embedded, or otherwise not explicitly
789-
// defined.
790-
// It does not matter if the Content-Type is not set by this function: it just
791-
// means things like the compress middleware will not work.
792-
func setDefaultContentType(next http.Handler) http.Handler {
793-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
794-
// Ignore if Content-Type is already set.
795-
if r.Header.Get("Content-Type") != "" {
796-
next.ServeHTTP(w, r)
797-
return
798-
}
799-
// Ignore if the request is not for a file (i.e. it has an extension).
800-
ext := path.Ext(r.URL.Path)
801-
if ext == "" {
802-
next.ServeHTTP(w, r)
803-
return
804-
}
805-
// Automatically set Content-Type for files with an extension.
806-
switch ext {
807-
case ".css":
808-
w.Header().Set("Content-Type", "text/css")
809-
case ".js":
810-
w.Header().Set("Content-Type", "text/javascript")
811-
case ".json":
812-
w.Header().Set("Content-Type", "application/json")
813-
case ".xml":
814-
w.Header().Set("Content-Type", "application/xml")
815-
case ".png":
816-
w.Header().Set("Content-Type", "image/png")
817-
case ".svg":
818-
w.Header().Set("Content-Type", "image/svg+xml")
819-
case ".jpg", ".jpeg":
820-
w.Header().Set("Content-Type", "image/jpeg")
821-
default:
822-
// Ignore if the extension is not recognised.
823-
}
824-
next.ServeHTTP(w, r)
825-
})
787+
// Hence this is for files that are embedded, or where the Content-Type cannot
788+
// be explicitly set.
789+
func setDefaultContentType(w http.ResponseWriter, r *http.Request) {
790+
// Ignore if Content-Type is already set.
791+
if w.Header().Get("Content-Type") != "" {
792+
return
793+
}
794+
// Ignore if the request is not for a file (i.e. it has an extension).
795+
ext := path.Ext(r.URL.Path)
796+
// Automatically set Content-Type for files with an extension.
797+
switch ext {
798+
case ".css":
799+
w.Header().Set("Content-Type", "text/css")
800+
case ".js":
801+
w.Header().Set("Content-Type", "text/javascript")
802+
case ".json":
803+
w.Header().Set("Content-Type", "application/json")
804+
case ".xml":
805+
w.Header().Set("Content-Type", "application/xml")
806+
case ".png":
807+
w.Header().Set("Content-Type", "image/png")
808+
case ".svg":
809+
w.Header().Set("Content-Type", "image/svg+xml")
810+
case ".jpg", ".jpeg":
811+
w.Header().Set("Content-Type", "image/jpeg")
812+
default:
813+
// Ignore if the extension is not recognised.
814+
}
826815
}

0 commit comments

Comments
 (0)