@@ -61,7 +61,6 @@ func Run(ctx context.Context, site Site) error {
61
61
62
62
router := chi .NewRouter ()
63
63
router .Use (middleware .Logger )
64
- router .Use (setDefaultContentType ) // Needs to come before compress.
65
64
router .Use (middleware .Compress (5 ))
66
65
router .Get ("/" , func (w http.ResponseWriter , r * http.Request ) {
67
66
pageInfo := PageInfo {
@@ -563,6 +562,7 @@ func Run(ctx context.Context, site Site) error {
563
562
return fmt .Errorf ("getting static sub-embed: %w" , err )
564
563
}
565
564
router .Get ("/static/*" , func (w http.ResponseWriter , r * http.Request ) {
565
+ setDefaultContentType (w , r )
566
566
fs := http .StripPrefix ("/static" , http .FileServer (http .FS (sub )))
567
567
fs .ServeHTTP (w , r )
568
568
})
@@ -781,46 +781,35 @@ func shortHash(hash string) string {
781
781
return hash
782
782
}
783
783
784
- // setDefaultContentType sets the Content-Type header for files with an
785
- // extension.
784
+ // setDefaultContentType sets a default Content-Type header.
786
785
// This is in order for the compress middleware to work correctly.
787
786
// 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
+ }
826
815
}
0 commit comments