Skip to content

Commit 0b34af2

Browse files
committed
Add noHttpCache to hugo server
Fixes #3897
1 parent c0370e0 commit 0b34af2

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

‎commands/server.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var (
3939
serverInterface string
4040
serverPort int
4141
serverWatch bool
42+
noHttpCache bool
4243
)
4344

4445
var serverCmd = &cobra.Command{
@@ -86,6 +87,7 @@ func init() {
8687
serverCmd.Flags().IntVarP(&serverPort, "port", "p", 1313, "port on which the server will listen")
8788
serverCmd.Flags().StringVarP(&serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind")
8889
serverCmd.Flags().BoolVarP(&serverWatch, "watch", "w", true, "watch filesystem for changes and recreate as needed")
90+
serverCmd.Flags().BoolVar(&noHttpCache, "noHttpCache", false, "prevent HTTP caching")
8991
serverCmd.Flags().BoolVarP(&serverAppend, "appendPort", "", true, "append port to baseURL")
9092
serverCmd.Flags().BoolVar(&disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild")
9193
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) {
205207

206208
httpFs := afero.NewHttpFs(c.Fs.Destination)
207209
fs := filesOnlyFs{httpFs.Dir(c.PathSpec().AbsPathify(c.Cfg.GetString("publishDir")))}
208-
fileserver := http.FileServer(fs)
210+
decorate := func(h http.Handler) http.Handler {
211+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
212+
if noHttpCache {
213+
w.Header().Set("Cache-Control", " no-store, no-cache, must-revalidate, max-age=0")
214+
w.Header().Set("Pragma", "no-cache")
215+
}
216+
h.ServeHTTP(w, r)
217+
})
218+
}
219+
220+
fileserver := decorate(http.FileServer(fs))
209221

210222
// We're only interested in the path
211223
u, err := url.Parse(c.Cfg.GetString("baseURL"))

0 commit comments

Comments
 (0)