diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-12-24 19:11:05 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2024-01-27 16:28:14 +0100 |
commit | 7285e74090852b5d52f25e577850fa75f4aa8573 (patch) | |
tree | 54d07cb4a7de2db5c89f2590266595f0aca6cbd6 /hugolib/content_render_hooks_test.go | |
parent | 5fd1e7490305570872d3899f5edda950903c5213 (diff) |
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaningdevelop2024
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
Diffstat (limited to 'hugolib/content_render_hooks_test.go')
-rw-r--r-- | hugolib/content_render_hooks_test.go | 369 |
1 files changed, 36 insertions, 333 deletions
diff --git a/hugolib/content_render_hooks_test.go b/hugolib/content_render_hooks_test.go index 5b2121ef8..22a33c35a 100644 --- a/hugolib/content_render_hooks_test.go +++ b/hugolib/content_render_hooks_test.go @@ -14,361 +14,61 @@ package hugolib import ( - "fmt" "testing" - - qt "github.com/frankban/quicktest" ) -func TestRenderHookEditNestedPartial(t *testing.T) { - config := ` -baseURL="https://example.org" -workingDir="/mywork" -` - b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running() - - b.WithTemplates("_default/single.html", "{{ .Content }}") - b.WithTemplates("partials/mypartial1.html", `PARTIAL1 {{ partial "mypartial2.html" }}`) - b.WithTemplates("partials/mypartial2.html", `PARTIAL2`) - b.WithTemplates("_default/_markup/render-link.html", `Link {{ .Text | safeHTML }}|{{ partial "mypartial1.html" . }}END`) - - b.WithContent("p1.md", `--- -title: P1 ---- - -[First Link](https://www.google.com "Google's Homepage") - -`) - b.Build(BuildCfg{}) - - b.AssertFileContent("public/p1/index.html", `Link First Link|PARTIAL1 PARTIAL2END`) - - b.EditFiles("layouts/partials/mypartial1.html", `PARTIAL1_EDITED {{ partial "mypartial2.html" }}`) - - b.Build(BuildCfg{}) - - b.AssertFileContent("public/p1/index.html", `Link First Link|PARTIAL1_EDITED PARTIAL2END`) - - b.EditFiles("layouts/partials/mypartial2.html", `PARTIAL2_EDITED`) - - b.Build(BuildCfg{}) - - b.AssertFileContent("public/p1/index.html", `Link First Link|PARTIAL1_EDITED PARTIAL2_EDITEDEND`) -} - -func TestRenderHooks(t *testing.T) { - config := ` -baseURL="https://example.org" -workingDir="/mywork" - -[markup] -[markup.goldmark] -[markup.goldmark.parser] -autoHeadingID = true -autoHeadingIDType = "github" -[markup.goldmark.parser.attribute] -block = true -title = true - -` - b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running() - b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`) - b.WithTemplatesAdded("shortcodes/myshortcode1.html", `{{ partial "mypartial1" }}`) - b.WithTemplatesAdded("shortcodes/myshortcode2.html", `{{ partial "mypartial2" }}`) - b.WithTemplatesAdded("shortcodes/myshortcode3.html", `SHORT3|`) - b.WithTemplatesAdded("shortcodes/myshortcode4.html", ` -<div class="foo"> -{{ .Inner | markdownify }} -</div> -`) - b.WithTemplatesAdded("shortcodes/myshortcode5.html", ` -Inner Inline: {{ .Inner | .Page.RenderString }} -Inner Block: {{ .Inner | .Page.RenderString (dict "display" "block" ) }} -`) - - b.WithTemplatesAdded("shortcodes/myshortcode6.html", `.Render: {{ .Page.Render "myrender" }}`) - b.WithTemplatesAdded("partials/mypartial1.html", `PARTIAL1`) - b.WithTemplatesAdded("partials/mypartial2.html", `PARTIAL2 {{ partial "mypartial3.html" }}`) - b.WithTemplatesAdded("partials/mypartial3.html", `PARTIAL3`) - b.WithTemplatesAdded("partials/mypartial4.html", `PARTIAL4`) - b.WithTemplatesAdded("customview/myrender.html", `myrender: {{ .Title }}|P4: {{ partial "mypartial4" }}`) - b.WithTemplatesAdded("_default/_markup/render-link.html", `{{ with .Page }}{{ .Title }}{{ end }}|{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`) - b.WithTemplatesAdded("docs/_markup/render-link.html", `Link docs section: {{ .Text | safeHTML }}|END`) - b.WithTemplatesAdded("_default/_markup/render-image.html", `IMAGE: {{ .Page.Title }}||{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`) - b.WithTemplatesAdded("_default/_markup/render-heading.html", `HEADING: {{ .Page.Title }}||Level: {{ .Level }}|Anchor: {{ .Anchor | safeURL }}|Text: {{ .Text | safeHTML }}|Attributes: {{ .Attributes }}|END`) - b.WithTemplatesAdded("docs/_markup/render-heading.html", `Docs Level: {{ .Level }}|END`) - - b.WithContent("customview/p1.md", `--- -title: Custom View ---- - -{{< myshortcode6 >}} - - `, "blog/p1.md", `--- -title: Cool Page ---- - -[First Link](https://www.google.com "Google's Homepage") -<https://foo.bar/> -https://bar.baz/ -<fake@example.com> -<mailto:fake2@example.com> - -{{< myshortcode3 >}} - -[Second Link](https://www.google.com "Google's Homepage") - -Image: - -![Drag Racing](/images/Dragster.jpg "image title") - -Attributes: - -## Some Heading {.text-serif #a-heading title="Hovered"} - - -`, "blog/p2.md", `--- -title: Cool Page2 -layout: mylayout ---- - -{{< myshortcode1 >}} - -[Some Text](https://www.google.com "Google's Homepage") - -,[No Whitespace Please](https://gohugo.io), - - - -`, "blog/p3.md", `--- -title: Cool Page3 ---- - -{{< myshortcode2 >}} - - -`, "docs/docs1.md", `--- -title: Docs 1 ---- - - -[Docs 1](https://www.google.com "Google's Homepage") - - -`, "blog/p4.md", `--- -title: Cool Page With Image ---- - -Image: - -![Drag Racing](/images/Dragster.jpg "image title") - - -`, "blog/p5.md", `--- -title: Cool Page With Markdownify ---- - -{{< myshortcode4 >}} -Inner Link: [Inner Link](https://www.google.com "Google's Homepage") -{{< /myshortcode4 >}} - -`, "blog/p6.md", `--- -title: With RenderString ---- - -{{< myshortcode5 >}}Inner Link: [Inner Link](https://www.gohugo.io "Hugo's Homepage"){{< /myshortcode5 >}} - -`, "blog/p7.md", `--- -title: With Headings ---- - -# Heading Level 1 -some text - -## Heading Level 2 - -### Heading Level 3 -`, - "docs/p8.md", `--- -title: Doc With Heading ---- - -# Docs lvl 1 - -`, - ) - - for i := 1; i <= 30; i++ { - // Add some content with no shortcodes or links, i.e no templates needed. - b.WithContent(fmt.Sprintf("blog/notempl%d.md", i), `--- -title: No Template ---- - -## Content -`) - } - counters := &testCounters{} - b.Build(BuildCfg{testCounters: counters}) - b.Assert(int(counters.contentRenderCounter), qt.Equals, 45) - - b.AssertFileContent("public/blog/p1/index.html", ` -Cool Page|https://www.google.com|Title: Google's Homepage|Text: First Link|END -Cool Page|https://foo.bar/|Title: |Text: https://foo.bar/|END -Cool Page|https://bar.baz/|Title: |Text: https://bar.baz/|END -Cool Page|mailto:fake@example.com|Title: |Text: fake@example.com|END -Cool Page|mailto:fake2@example.com|Title: |Text: mailto:fake2@example.com|END -Text: Second -SHORT3| -<p>IMAGE: Cool Page||/images/Dragster.jpg|Title: image title|Text: Drag Racing|END</p> -`) - - b.AssertFileContent("public/customview/p1/index.html", `.Render: myrender: Custom View|P4: PARTIAL4`) - b.AssertFileContent("public/blog/p2/index.html", - `PARTIAL -,Cool Page2|https://gohugo.io|Title: |Text: No Whitespace Please|END,`, - ) - b.AssertFileContent("public/blog/p3/index.html", `PARTIAL3`) - // We may add type template support later, keep this for then. b.AssertFileContent("public/docs/docs1/index.html", `Link docs section: Docs 1|END`) - b.AssertFileContent("public/blog/p4/index.html", `<p>IMAGE: Cool Page With Image||/images/Dragster.jpg|Title: image title|Text: Drag Racing|END</p>`) - // markdownify - b.AssertFileContent("public/blog/p5/index.html", "Inner Link: |https://www.google.com|Title: Google's Homepage|Text: Inner Link|END") - - b.AssertFileContent("public/blog/p6/index.html", - "Inner Inline: Inner Link: With RenderString|https://www.gohugo.io|Title: Hugo's Homepage|Text: Inner Link|END", - "Inner Block: <p>Inner Link: With RenderString|https://www.gohugo.io|Title: Hugo's Homepage|Text: Inner Link|END</p>", - ) - - b.EditFiles( - "layouts/_default/_markup/render-link.html", `EDITED: {{ .Destination | safeURL }}|`, - "layouts/_default/_markup/render-image.html", `IMAGE EDITED: {{ .Destination | safeURL }}|`, - "layouts/docs/_markup/render-link.html", `DOCS EDITED: {{ .Destination | safeURL }}|`, - "layouts/partials/mypartial1.html", `PARTIAL1_EDITED`, - "layouts/partials/mypartial3.html", `PARTIAL3_EDITED`, - "layouts/partials/mypartial4.html", `PARTIAL4_EDITED`, - "layouts/shortcodes/myshortcode3.html", `SHORT3_EDITED|`, - ) - - counters = &testCounters{} - b.Build(BuildCfg{testCounters: counters}) - // Make sure that only content using the changed templates are re-rendered. - b.Assert(int(counters.contentRenderCounter), qt.Equals, 7) - - b.AssertFileContent("public/customview/p1/index.html", `.Render: myrender: Custom View|P4: PARTIAL4_EDITED`) - b.AssertFileContent("public/blog/p1/index.html", `<p>EDITED: https://www.google.com|</p>`, "SHORT3_EDITED|") - b.AssertFileContent("public/blog/p2/index.html", `PARTIAL1_EDITED`) - b.AssertFileContent("public/blog/p3/index.html", `PARTIAL3_EDITED`) - // We may add type template support later, keep this for then. b.AssertFileContent("public/docs/docs1/index.html", `DOCS EDITED: https://www.google.com|</p>`) - b.AssertFileContent("public/blog/p4/index.html", `IMAGE EDITED: /images/Dragster.jpg|`) - b.AssertFileContent("public/blog/p6/index.html", "<p>Inner Link: EDITED: https://www.gohugo.io|</p>") - b.AssertFileContent("public/blog/p7/index.html", "HEADING: With Headings||Level: 1|Anchor: heading-level-1|Text: Heading Level 1|Attributes: map[id:heading-level-1]|END<p>some text</p>\nHEADING: With Headings||Level: 2|Anchor: heading-level-2|Text: Heading Level 2|Attributes: map[id:heading-level-2]|ENDHEADING: With Headings||Level: 3|Anchor: heading-level-3|Text: Heading Level 3|Attributes: map[id:heading-level-3]|END") - - // https://github.com/gohugoio/hugo/issues/7349 - b.AssertFileContent("public/docs/p8/index.html", "Docs Level: 1") -} - -func TestRenderHooksDeleteTemplate(t *testing.T) { - config := ` -baseURL="https://example.org" -workingDir="/mywork" -` - b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running() - b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`) - b.WithTemplatesAdded("_default/_markup/render-link.html", `html-render-link`) - - b.WithContent("p1.md", `--- -title: P1 ---- -[First Link](https://www.google.com "Google's Homepage") - -`) - b.Build(BuildCfg{}) - - b.AssertFileContent("public/p1/index.html", `<p>html-render-link</p>`) - - b.RemoveFiles( - "layouts/_default/_markup/render-link.html", - ) - - b.Build(BuildCfg{}) - b.AssertFileContent("public/p1/index.html", `<p><a href="https://www.google.com" title="Google's Homepage">First Link</a></p>`) -} - -func TestRenderHookAddTemplate(t *testing.T) { - config := ` -baseURL="https://example.org" -workingDir="/mywork" -` - b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running() - b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`) - - b.WithContent("p1.md", `--- -title: P1 ---- -[First Link](https://www.google.com "Google's Homepage") - -`) - b.Build(BuildCfg{}) - - b.AssertFileContent("public/p1/index.html", `<p><a href="https://www.google.com" title="Google's Homepage">First Link</a></p>`) - - b.EditFiles("layouts/_default/_markup/render-link.html", `html-render-link`) - - b.Build(BuildCfg{}) - - b.AssertFileContent("public/p1/index.html", `<p>html-render-link</p>`) -} - func TestRenderHooksRSS(t *testing.T) { - b := newTestSitesBuilder(t) - - b.WithTemplates("index.html", ` + files := ` +-- hugo.toml -- +baseURL = "https://example.org" +disableKinds = ["taxonomy", "term"] +-- layouts/index.html -- {{ $p := site.GetPage "p1.md" }} {{ $p2 := site.GetPage "p2.md" }} - P1: {{ $p.Content }} P2: {{ $p2.Content }} - - `, "index.xml", ` - +-- layouts/index.xml -- {{ $p2 := site.GetPage "p2.md" }} {{ $p3 := site.GetPage "p3.md" }} - P2: {{ $p2.Content }} P3: {{ $p3.Content }} - - - `, - "_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|`, - "_default/_markup/render-link.rss.xml", `xml-link: {{ .Destination | safeURL }}|`, - "_default/_markup/render-heading.html", `html-heading: {{ .Text }}|`, - "_default/_markup/render-heading.rss.xml", `xml-heading: {{ .Text }}|`, - ) - - b.WithContent("p1.md", `--- +-- layouts/_default/_markup/render-link.html -- +html-link: {{ .Destination | safeURL }}| +-- layouts/_default/_markup/render-link.rss.xml -- +xml-link: {{ .Destination | safeURL }}| +-- layouts/_default/_markup/render-heading.html -- +html-heading: {{ .Text }}| +-- layouts/_default/_markup/render-heading.rss.xml -- +xml-heading: {{ .Text }}| +-- content/p1.md -- +--- title: "p1" --- P1. [I'm an inline-style link](https://www.gohugo.io) # Heading in p1 -`, "p2.md", `--- +-- content/p2.md -- +--- title: "p2" --- -P1. [I'm an inline-style link](https://www.bep.is) +P2. [I'm an inline-style link](https://www.bep.is) # Heading in p2 -`, - "p3.md", `--- -title: "p2" +-- content/p3.md -- +--- +title: "p3" outputs: ["rss"] --- P3. [I'm an inline-style link](https://www.example.org) - -`, - ) - - b.Build(BuildCfg{}) +` + b := NewIntegrationTestBuilder( + IntegrationTestConfig{ + T: t, + TxtarString: files, + }, + ).Build() b.AssertFileContent("public/index.html", ` P1: <p>P1. html-link: https://www.gohugo.io|</p> @@ -376,7 +76,7 @@ html-heading: Heading in p1| html-heading: Heading in p2| `) b.AssertFileContent("public/index.xml", ` -P2: <p>P1. xml-link: https://www.bep.is|</p> +P2: <p>P2. xml-link: https://www.bep.is|</p> P3: <p>P3. xml-link: https://www.example.org|</p> xml-heading: Heading in p2| `) @@ -453,8 +153,12 @@ title: "p2" -- layouts/_default/_markup/render-heading.html -- Heading: {{ .Text }}| -Self Fragments: {{ .Page.Fragments.Identifiers }}| -P1 Fragments: {{ (site.GetPage "p1.md").Fragments.Identifiers }}| +{{ with .Page }} +Self Fragments: {{ .Fragments.Identifiers }}| +{{ end }} +{{ with (site.GetPage "p1.md") }} +P1 Fragments: {{ .Fragments.Identifiers }}| +{{ end }} -- layouts/_default/single.html -- {{ .Content}} ` @@ -474,5 +178,4 @@ P1 Fragments: [b c z] Self Fragments: [d e f] P1 Fragments: [b c z] `) - } |