Prevent stale content in Fast Render Mode

We do that by re-render visited pages that is not already in the stack. This may potentially do some double work, but that small penalty should be well worth it.

Fixes #5281
This commit is contained in:
Bjørn Erik Pedersen 2018-10-17 09:28:04 +02:00
parent 083311d033
commit 4a366fcfee
8 changed files with 83 additions and 36 deletions

View file

@ -324,10 +324,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
fs.Destination = new(afero.MemMapFs) fs.Destination = new(afero.MemMapFs)
} }
doLiveReload := !c.h.buildWatch && !config.GetBool("disableLiveReload") if c.fastRenderMode {
fastRenderMode := doLiveReload && !config.GetBool("disableFastRender")
if fastRenderMode {
// For now, fast render mode only. It should, however, be fast enough // For now, fast render mode only. It should, however, be fast enough
// for the full variant, too. // for the full variant, too.
changeDetector := &fileChangeDetector{ changeDetector := &fileChangeDetector{

View file

@ -618,13 +618,20 @@ func (c *commandeer) buildSites() (err error) {
return c.hugo.Build(hugolib.BuildCfg{}) return c.hugo.Build(hugolib.BuildCfg{})
} }
func (c *commandeer) handleBuildErr(err error, msg string) {
c.buildErr = err
c.logger.ERROR.Printf("%s: %s", msg, err)
if !c.h.quiet && c.h.verbose {
herrors.PrintStackTrace(err)
}
}
func (c *commandeer) rebuildSites(events []fsnotify.Event) error { func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
defer c.timeTrack(time.Now(), "Total") defer c.timeTrack(time.Now(), "Total")
c.buildErr = nil c.buildErr = nil
visited := c.visitedURLs.PeekAllSet() visited := c.visitedURLs.PeekAllSet()
doLiveReload := !c.h.buildWatch && !c.Cfg.GetBool("disableLiveReload") if c.fastRenderMode {
if doLiveReload && !c.Cfg.GetBool("disableFastRender") {
// Make sure we always render the home pages // Make sure we always render the home pages
for _, l := range c.languages { for _, l := range c.languages {
@ -640,6 +647,15 @@ func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
return c.hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited}, events...) return c.hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited}, events...)
} }
func (c *commandeer) partialReRender(urls ...string) error {
c.buildErr = nil
visited := make(map[string]bool)
for _, url := range urls {
visited[url] = true
}
return c.hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited, PartialReRender: true})
}
func (c *commandeer) fullRebuild() { func (c *commandeer) fullRebuild() {
c.commandeerHugoState = &commandeerHugoState{} c.commandeerHugoState = &commandeerHugoState{}
err := c.loadConfig(true, true) err := c.loadConfig(true, true)
@ -907,15 +923,10 @@ func (c *commandeer) handleEvents(watcher *watcher.Batcher,
c.changeDetector.PrepareNew() c.changeDetector.PrepareNew()
if err := c.rebuildSites(dynamicEvents); err != nil { if err := c.rebuildSites(dynamicEvents); err != nil {
c.buildErr = err c.handleBuildErr(err, "Rebuild failed")
c.logger.ERROR.Printf("Rebuild failed: %s", err)
if !c.h.quiet && c.h.verbose {
herrors.PrintStackTrace(err)
}
} }
if doLiveReload { if doLiveReload {
if len(partitionedEvents.ContentEvents) == 0 && len(partitionedEvents.AssetEvents) > 0 { if len(partitionedEvents.ContentEvents) == 0 && len(partitionedEvents.AssetEvents) > 0 {
changed := c.changeDetector.changed() changed := c.changeDetector.changed()
if c.changeDetector != nil && len(changed) == 0 { if c.changeDetector != nil && len(changed) == 0 {

View file

@ -345,10 +345,22 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
w.Header().Set("Pragma", "no-cache") w.Header().Set("Pragma", "no-cache")
} }
if f.c.fastRenderMode { if f.c.fastRenderMode && f.c.buildErr == nil {
p := r.RequestURI p := r.RequestURI
if strings.HasSuffix(p, "/") || strings.HasSuffix(p, "html") || strings.HasSuffix(p, "htm") { if strings.HasSuffix(p, "/") || strings.HasSuffix(p, "html") || strings.HasSuffix(p, "htm") {
if !f.c.visitedURLs.Contains(p) {
// If not already on stack, re-render that single page.
if err := f.c.partialReRender(p); err != nil {
f.c.handleBuildErr(err, fmt.Sprintf("Failed to render %q", p))
if f.c.showErrorInBrowser {
http.Redirect(w, r, p, 301)
return
}
}
}
f.c.visitedURLs.Add(p) f.c.visitedURLs.Add(p)
} }
} }
h.ServeHTTP(w, r) h.ServeHTTP(w, r)

View file

@ -52,6 +52,13 @@ func (q *EvictingStringQueue) Add(v string) {
q.mu.Unlock() q.mu.Unlock()
} }
// Contains returns whether the queue contains v.
func (q *EvictingStringQueue) Contains(v string) bool {
q.mu.Lock()
defer q.mu.Unlock()
return q.set[v]
}
// Peek looks at the last element added to the queue. // Peek looks at the last element added to the queue.
func (q *EvictingStringQueue) Peek() string { func (q *EvictingStringQueue) Peek() string {
q.mu.Lock() q.mu.Lock()

View file

@ -36,6 +36,9 @@ func TestEvictingStringQueue(t *testing.T) {
queue.Add("a") queue.Add("a")
queue.Add("b") queue.Add("b")
assert.True(queue.Contains("a"))
assert.False(queue.Contains("foo"))
assert.Equal([]string{"b", "a"}, queue.PeekAll()) assert.Equal([]string{"b", "a"}, queue.PeekAll())
assert.Equal("b", queue.Peek()) assert.Equal("b", queue.Peek())
queue.Add("c") queue.Add("c")

View file

@ -368,6 +368,11 @@ type BuildCfg struct {
SkipRender bool SkipRender bool
// Use this to indicate what changed (for rebuilds). // Use this to indicate what changed (for rebuilds).
whatChanged *whatChanged whatChanged *whatChanged
// This is a partial re-render of some selected pages. This means
// we should skip most of the processing.
PartialReRender bool
// Recently visited URLs. This is used for partial re-rendering. // Recently visited URLs. This is used for partial re-rendering.
RecentlyVisited map[string]bool RecentlyVisited map[string]bool
} }

View file

@ -41,29 +41,31 @@ func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error {
conf.whatChanged = &whatChanged{source: true, other: true} conf.whatChanged = &whatChanged{source: true, other: true}
} }
for _, s := range h.Sites { if !config.PartialReRender {
s.Deps.BuildStartListeners.Notify() for _, s := range h.Sites {
} s.Deps.BuildStartListeners.Notify()
}
if len(events) > 0 { if len(events) > 0 {
// Rebuild // Rebuild
if err := h.initRebuild(conf); err != nil { if err := h.initRebuild(conf); err != nil {
return err
}
} else {
if err := h.init(conf); err != nil {
return err
}
}
if err := h.process(conf, events...); err != nil {
return err return err
} }
} else {
if err := h.init(conf); err != nil { if err := h.assemble(conf); err != nil {
return err return err
} }
} }
if err := h.process(conf, events...); err != nil {
return err
}
if err := h.assemble(conf); err != nil {
return err
}
if err := h.render(conf); err != nil { if err := h.render(conf); err != nil {
return err return err
} }
@ -226,8 +228,10 @@ func (h *HugoSites) assemble(config *BuildCfg) error {
} }
func (h *HugoSites) render(config *BuildCfg) error { func (h *HugoSites) render(config *BuildCfg) error {
for _, s := range h.Sites { if !config.PartialReRender {
s.initRenderFormats() for _, s := range h.Sites {
s.initRenderFormats()
}
} }
for _, s := range h.Sites { for _, s := range h.Sites {
@ -240,15 +244,23 @@ func (h *HugoSites) render(config *BuildCfg) error {
isRenderingSite := s == s2 isRenderingSite := s == s2
if err := s2.preparePagesForRender(isRenderingSite && i == 0); err != nil { if !config.PartialReRender {
return err if err := s2.preparePagesForRender(isRenderingSite && i == 0); err != nil {
return err
}
} }
} }
if !config.SkipRender { if !config.SkipRender {
if err := s.render(config, i); err != nil { if config.PartialReRender {
return err if err := s.renderPages(config); err != nil {
return err
}
} else {
if err := s.render(config, i); err != nil {
return err
}
} }
} }
} }

View file

@ -43,7 +43,7 @@ func (s *Site) renderPages(cfg *BuildCfg) error {
go pageRenderer(s, pages, results, wg) go pageRenderer(s, pages, results, wg)
} }
if len(s.headlessPages) > 0 { if !cfg.PartialReRender && len(s.headlessPages) > 0 {
wg.Add(1) wg.Add(1)
go headlessPagesPublisher(s, wg) go headlessPagesPublisher(s, wg)
} }