|
39 | 39 | serverInterface string |
40 | 40 | serverPort int |
41 | 41 | serverWatch bool |
| 42 | + noHttpCache bool |
42 | 43 | ) |
43 | 44 |
|
44 | 45 | var serverCmd = &cobra.Command{ |
@@ -86,6 +87,7 @@ func init() { |
86 | 87 | serverCmd.Flags().IntVarP(&serverPort, "port", "p", 1313, "port on which the server will listen") |
87 | 88 | serverCmd.Flags().StringVarP(&serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind") |
88 | 89 | 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") |
89 | 91 | serverCmd.Flags().BoolVarP(&serverAppend, "appendPort", "", true, "append port to baseURL") |
90 | 92 | serverCmd.Flags().BoolVar(&disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild") |
91 | 93 | 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) { |
205 | 207 |
|
206 | 208 | httpFs := afero.NewHttpFs(c.Fs.Destination) |
207 | 209 | 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)) |
209 | 221 |
|
210 | 222 | // We're only interested in the path |
211 | 223 | u, err := url.Parse(c.Cfg.GetString("baseURL")) |
|
0 commit comments