From 2b05a50f8f89e4e2f5f7a8ea7ae7993a2c0f3524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 5 Jun 2024 09:21:45 +0200 Subject: [PATCH] Misc remote HTTP/content adapter enhancements * Recover from server errors * Improve go adapter rebuilds when adding new content See #12502 Fixes #12570 --- commands/hugobuilder.go | 5 ++++- hugolib/content_map.go | 15 +++++++++++++++ hugolib/hugo_sites_build.go | 6 +++++- .../pagesfromgotmpl_integration_test.go | 9 +++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/commands/hugobuilder.go b/commands/hugobuilder.go index 99bd8a04a90..d77f1f178d8 100644 --- a/commands/hugobuilder.go +++ b/commands/hugobuilder.go @@ -345,6 +345,7 @@ func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*wa for { select { case changes := <-c.r.changesFromBuild: + c.errState.setBuildErr(nil) unlock, err := h.LockBuild() if err != nil { c.r.logger.Errorln("Failed to acquire a build lock: %s", err) @@ -356,7 +357,9 @@ func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*wa c.r.logger.Errorln("Error while watching:", err) } if c.s != nil && c.s.doLiveReload { - if c.changeDetector == nil || len(c.changeDetector.changed()) > 0 { + doReload := c.changeDetector == nil || len(c.changeDetector.changed()) > 0 + doReload = doReload || c.showErrorInBrowser && c.errCount() > 0 + if doReload { livereload.ForceRefresh() } } diff --git a/hugolib/content_map.go b/hugolib/content_map.go index 6ab945209ec..55c96c9a053 100644 --- a/hugolib/content_map.go +++ b/hugolib/content_map.go @@ -386,6 +386,21 @@ func (m *pageMap) addPagesFromGoTmplFi(fi hugofs.FileMetaInfo, buildConfig *Buil pt.AddChange(n.GetIdentity()) } else { pt.AddChange(u.GetIdentity()) + // New content not in use anywhere. + // To make sure that these gets listed in any site.RegularPages ranges or similar + // we could invalidate everything, but first try to collect a sample set + // from the surrounding pages. + var surroundingIDs []identity.Identity + ids := h.pageTrees.collectIdentitiesSurrounding(pi.Base(), 10) + if len(ids) > 0 { + surroundingIDs = append(surroundingIDs, ids...) + } else { + // No surrounding pages found, so invalidate everything. + surroundingIDs = []identity.Identity{identity.GenghisKhan} + } + for _, id := range surroundingIDs { + pt.AddChange(id) + } } } diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go index fe05f5174ea..12eb6a5f8d7 100644 --- a/hugolib/hugo_sites_build.go +++ b/hugolib/hugo_sites_build.go @@ -244,9 +244,13 @@ func (h *HugoSites) process(ctx context.Context, l logg.LevelLogger, config *Bui } if len(events) > 0 { - // This is a rebuild + // This is a rebuild triggered from file events. return h.processPartialFileEvents(ctx, l, config, init, events) } else if len(config.WhatChanged.Changes()) > 0 { + // Rebuild triggered from remote events. + if err := init(config); err != nil { + return err + } return h.processPartialRebuildChanges(ctx, l, config) } return h.processFull(ctx, l, config) diff --git a/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go b/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go index f351cbb9871..3c50f87f7ea 100644 --- a/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go +++ b/hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go @@ -80,6 +80,7 @@ Pfile Content {{ $.AddPage (dict "kind" "page" "path" "p2" "title" "p2title" "dates" $dates "content" $contentHTML ) }} {{ $.AddPage (dict "kind" "page" "path" "p3" "title" "p3title" "dates" $dates "content" $contentMarkdownDefault "draft" false ) }} {{ $.AddPage (dict "kind" "page" "path" "p4" "title" "p4title" "dates" $dates "content" $contentMarkdownDefault "draft" $data.draft ) }} +ADD_MORE_PLACEHOLDER {{ $resourceContent := dict "value" $dataResource }} @@ -279,6 +280,14 @@ func TestPagesFromGoTmplRemovePage(t *testing.T) { b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p3title:/docs/p3|p4title:/docs/p4|pfile:/docs/pfile|$") } +func TestPagesFromGoTmplAddPage(t *testing.T) { + t.Parallel() + b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic) + b.EditFileReplaceAll("content/docs/_content.gotmpl", "ADD_MORE_PLACEHOLDER", `{{ $.AddPage (dict "kind" "page" "path" "page_added" "title" "page_added_title" "dates" $dates "content" $contentHTML ) }}`).Build() + b.AssertFileExists("public/docs/page_added/index.html", true) + b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p2title:/docs/p2|p3title:/docs/p3|p4title:/docs/p4|page_added_title:/docs/page_added|pfile:/docs/pfile|$") +} + func TestPagesFromGoTmplDraftPage(t *testing.T) { t.Parallel() b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)