diff options
author | Bjørn Erik Pedersen <[email protected]> | 2017-10-14 13:40:43 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2017-10-14 13:40:43 +0200 |
commit | 60bd332c1f68e49e6ac439047e7c660865189380 (patch) | |
tree | 49274b7ce5ff73e0f51d6ac9e20740e6e3765f04 /commands | |
parent | 6a30874f19610a38e846e120aac03c68e12f9b7b (diff) | |
download | hugo-60bd332c1f68e49e6ac439047e7c660865189380.tar.gz hugo-60bd332c1f68e49e6ac439047e7c660865189380.zip |
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
Diffstat (limited to 'commands')
-rw-r--r-- | commands/commandeer.go | 9 | ||||
-rw-r--r-- | commands/hugo.go | 33 | ||||
-rw-r--r-- | commands/server.go | 23 |
3 files changed, 50 insertions, 15 deletions
diff --git a/commands/commandeer.go b/commands/commandeer.go index 7de185d2f..d07a3d5bb 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -14,6 +14,7 @@ package commands import ( + "github.com/gohugoio/hugo/common/types" "github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/hugofs" @@ -21,8 +22,9 @@ import ( type commandeer struct { *deps.DepsCfg - pathSpec *helpers.PathSpec - configured bool + pathSpec *helpers.PathSpec + visitedURLs *types.EvictingStringQueue + configured bool } func (c *commandeer) Set(key string, value interface{}) { @@ -58,5 +60,6 @@ func newCommandeer(cfg *deps.DepsCfg) (*commandeer, error) { if err != nil { return nil, err } - return &commandeer{DepsCfg: cfg, pathSpec: ps}, nil + + return &commandeer{DepsCfg: cfg, pathSpec: ps, visitedURLs: types.NewEvictingStringQueue(10)}, nil } diff --git a/commands/hugo.go b/commands/hugo.go index 4acc5c4e6..23335f292 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -768,7 +768,12 @@ func (c *commandeer) rebuildSites(events []fsnotify.Event) error { if err := c.initSites(); err != nil { return err } - return Hugo.Build(hugolib.BuildCfg{PrintStats: !quiet, Watching: true}, events...) + visited := c.visitedURLs.PeekAllSet() + if !c.Cfg.GetBool("disableFastRender") { + // Make sure we always render the home page + visited["/"] = true + } + return Hugo.Build(hugolib.BuildCfg{PrintStats: !quiet, Watching: true, RecentlyVisited: visited}, events...) } // newWatcher creates a new watcher to watch filesystem events. @@ -986,6 +991,16 @@ func (c *commandeer) newWatcher(port int) error { } if len(dynamicEvents) > 0 { + doLiveReload := !buildWatch && !c.Cfg.GetBool("disableLiveReload") + onePageName := pickOneWriteOrCreatePath(dynamicEvents) + + if onePageName != "" && doLiveReload && !c.Cfg.GetBool("disableFastRender") { + p := Hugo.GetContentPage(onePageName) + if p != nil { + c.visitedURLs.Add(p.RelPermalink()) + } + + } c.Logger.FEEDBACK.Println("\nChange detected, rebuilding site") const layout = "2006-01-02 15:04 -0700" c.Logger.FEEDBACK.Println(time.Now().Format(layout)) @@ -994,21 +1009,15 @@ func (c *commandeer) newWatcher(port int) error { c.Logger.ERROR.Println("Failed to rebuild site:", err) } - if !buildWatch && !c.Cfg.GetBool("disableLiveReload") { - + if doLiveReload { navigate := c.Cfg.GetBool("navigateToChanged") - + // We have fetched the same page above, but it may have + // changed. var p *hugolib.Page if navigate { - - // It is probably more confusing than useful - // to navigate to a new URL on RENAME etc. - // so for now we use the WRITE and CREATE events only. - name := pickOneWriteOrCreatePath(dynamicEvents) - - if name != "" { - p = Hugo.GetContentPage(name) + if onePageName != "" { + p = Hugo.GetContentPage(onePageName) } } diff --git a/commands/server.go b/commands/server.go index b52e38c17..8c22d1d97 100644 --- a/commands/server.go +++ b/commands/server.go @@ -41,6 +41,8 @@ var ( liveReloadPort int serverWatch bool noHTTPCache bool + + disableFastRender bool ) var serverCmd = &cobra.Command{ @@ -94,6 +96,8 @@ func init() { 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") serverCmd.Flags().BoolVar(&renderToDisk, "renderToDisk", false, "render to Destination path (default is render to memory & serve from there)") + serverCmd.Flags().BoolVar(&disableFastRender, "disableFastRender", false, "enables full re-renders on changes") + serverCmd.Flags().String("memstats", "", "log memory usage to this file") serverCmd.Flags().String("meminterval", "100ms", "interval to poll memory usage (requires --memstats), valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".") @@ -120,6 +124,10 @@ func server(cmd *cobra.Command, args []string) error { c.Set("navigateToChanged", navigateToChanged) } + if cmd.Flags().Changed("disableFastRender") { + c.Set("disableFastRender", disableFastRender) + } + if serverWatch { c.Set("watch", true) } @@ -214,12 +222,27 @@ func (c *commandeer) serve(port int) { httpFs := afero.NewHttpFs(c.Fs.Destination) fs := filesOnlyFs{httpFs.Dir(c.PathSpec().AbsPathify(c.Cfg.GetString("publishDir")))} + + doLiveReload := !buildWatch && !c.Cfg.GetBool("disableLiveReload") + fastRenderMode := doLiveReload && !c.Cfg.GetBool("disableFastRender") + + if fastRenderMode { + jww.FEEDBACK.Println("Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender") + } + 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") } + + if fastRenderMode { + p := r.URL.Path + if strings.HasSuffix(p, "/") || strings.HasSuffix(p, "html") || strings.HasSuffix(p, "htm") { + c.visitedURLs.Add(p) + } + } h.ServeHTTP(w, r) }) } |