From a883948c4fa6d6de9ef2912709b42655c4cead83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 4 Apr 2017 18:05:19 +0200 Subject: [PATCH] Register all media types when in server mode Fixes #3274 --- commands/server.go | 9 ++++----- hugolib/site.go | 10 ++++++++++ media/mediaType.go | 2 -- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/commands/server.go b/commands/server.go index ae51d075d..0e719e17d 100644 --- a/commands/server.go +++ b/commands/server.go @@ -24,8 +24,6 @@ import ( "strings" "time" - "mime" - "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/spf13/hugo/config" @@ -95,9 +93,6 @@ func init() { serverCmd.RunE = server - mime.AddExtensionType(".json", "application/json; charset=utf-8") - mime.AddExtensionType(".css", "text/css; charset=utf-8") - } func server(cmd *cobra.Command, args []string) error { @@ -168,6 +163,10 @@ func server(cmd *cobra.Command, args []string) error { return err } + for _, s := range Hugo.Sites { + s.RegisterMediaTypes() + } + // Watch runs its own server as part of the routine if serverWatch { watchDirs := c.getDirList() diff --git a/hugolib/site.go b/hugolib/site.go index 7b5d0d156..aefc8c940 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -18,6 +18,7 @@ import ( "fmt" "html/template" "io" + "mime" "net/url" "os" "path/filepath" @@ -610,6 +611,15 @@ type whatChanged struct { other bool } +// RegisterMediaTypes will register the Site's media types in the mime +// package, so it will behave correctly with Hugo's built-in server. +func (s *Site) RegisterMediaTypes() { + for _, mt := range s.mediaTypesConfig { + // The last one will win if there are any duplicates. + mime.AddExtensionType("."+mt.Suffix, mt.Type()+"; charset=utf-8") + } +} + // reBuild partially rebuilds a site given the filesystem events. // It returns whetever the content source was changed. func (s *Site) reProcess(events []fsnotify.Event) (whatChanged, error) { diff --git a/media/mediaType.go b/media/mediaType.go index bc54986be..f64f51253 100644 --- a/media/mediaType.go +++ b/media/mediaType.go @@ -177,5 +177,3 @@ func DecodeTypes(maps ...map[string]interface{}) (Types, error) { return m, nil } - -// TODO(bep) output mime.AddExtensionType