diff options
author | Paul Gottschling <paul.gottschling@gmail.com> | 2021-09-09 16:30:41 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-01-12 09:45:53 +0300 |
commit | 25d645f47ae0a32470d303c9478c9e0b2fff0f0e (patch) | |
tree | 019995a2274ee1c055e35f6dbb54d80b68f85177 /hugolib/page.go | |
parent | fbb3c181cb079ee422dd8ad13438464af8a90469 (diff) |
Fix missing page data for alternative formats
When a template calls the .Translations function and a
Hugo environment is using multiple output formats,
a template that calls methods like .Summary and .Len on
each translation will unexpectedly show empty return
values for these methods.
This is because each pageOutput's ContentProvider is
assigned to a page.NopPage in newPageOutput. When
*HugoSites.render assigns pageContentOutputs to
pageOutputs in *pageState.shiftToOutputFormat, it
reuses pageContentOutputs from other pageOutputs,
leaving some pageContentOutputs as NopPages. While this
approach conserves resources, sometimes it means that
a template will unexpectedly call a method on a
pageContentOutput that is actually a NopPage.
In the case of ContentProvider methods called on
translations for alternative output formats, the methods
were called on NopPages.
This change introduces LazyContentProvider, which
performs late initialization when one of its methods is
called. This way, we can reuse content in "normal" cases
but ensure that ContentProvider methods work as expected
when a pageOutput is not assigned a pageContentOutput
during the initial pre-render phase.
Fixes #8919
Diffstat (limited to 'hugolib/page.go')
-rw-r--r-- | hugolib/page.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/hugolib/page.go b/hugolib/page.go index d2d962044..509a083e8 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -940,6 +940,20 @@ func (p *pageState) shiftToOutputFormat(isRenderingSite bool, idx int) error { panic(fmt.Sprintf("pageOutput is nil for output idx %d", idx)) } + // We attempt to assign pageContentOutputs while preparing each site + // for rendering and before rendering each site. This lets us share + // content between page outputs to conserve resources. But if a template + // unexpectedly calls a method of a ContentProvider that is not yet + // initialized, we assign a LazyContentProvider that performs the + // initialization just in time. + p.pageOutput.ContentProvider = page.NewLazyContentProvider(func() (page.ContentProvider, error) { + cp, err := newPageContentOutput(p, p.pageOutput) + if err != nil { + return nil, err + } + return cp, nil + }) + // Reset any built paginator. This will trigger when re-rendering pages in // server mode. if isRenderingSite && p.pageOutput.paginator != nil && p.pageOutput.paginator.current != nil { |