diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-09-22 18:13:21 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-09-22 18:13:21 +0300 |
commit | 0b34af216154367af7f53ce93d44e6b3d58c3f34 (patch) | |
tree | 2ab7987455031f05df67e9ca633f1692f998cd46 | |
parent | c0370e0ee3f12efe5f82b219f56b5c71f04f2329 (diff) |
Add noHttpCache to hugo server
Fixes #3897
-rw-r--r-- | commands/server.go | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/commands/server.go b/commands/server.go index 89d5c239e..b6983c704 100644 --- a/commands/server.go +++ b/commands/server.go @@ -39,6 +39,7 @@ var ( serverInterface string serverPort int serverWatch bool + noHttpCache bool ) var serverCmd = &cobra.Command{ @@ -86,6 +87,7 @@ func init() { serverCmd.Flags().IntVarP(&serverPort, "port", "p", 1313, "port on which the server will listen") serverCmd.Flags().StringVarP(&serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind") serverCmd.Flags().BoolVarP(&serverWatch, "watch", "w", true, "watch filesystem for changes and recreate as needed") + serverCmd.Flags().BoolVar(&noHttpCache, "noHttpCache", false, "prevent HTTP caching") serverCmd.Flags().BoolVarP(&serverAppend, "appendPort", "", true, "append port to baseURL") serverCmd.Flags().BoolVar(&disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild") serverCmd.Flags().BoolVar(&navigateToChanged, "navigateToChanged", false, "navigate to changed content file on live browser reload") @@ -205,7 +207,17 @@ func (c *commandeer) serve(port int) { httpFs := afero.NewHttpFs(c.Fs.Destination) fs := filesOnlyFs{httpFs.Dir(c.PathSpec().AbsPathify(c.Cfg.GetString("publishDir")))} - fileserver := http.FileServer(fs) + decorate := func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if noHttpCache { + w.Header().Set("Cache-Control", " no-store, no-cache, must-revalidate, max-age=0") + w.Header().Set("Pragma", "no-cache") + } + h.ServeHTTP(w, r) + }) + } + + fileserver := decorate(http.FileServer(fs)) // We're only interested in the path u, err := url.Parse(c.Cfg.GetString("baseURL")) |