Fix missing page data for alternative formats
authorPaul Gottschling <paul.gottschling@gmail.com>
Thu, 9 Sep 2021 13:30:41 +0000 (09:30 -0400)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 12 Jan 2022 06:45:53 +0000 (07:45 +0100)
commit25d645f47ae0a32470d303c9478c9e0b2fff0f0e
tree019995a2274ee1c055e35f6dbb54d80b68f85177
parentfbb3c181cb079ee422dd8ad13438464af8a90469
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
hugolib/page.go
hugolib/page_test.go
resources/page/page_lazy_contentprovider.go [new file with mode: 0644]