From a49bfbede4c131688e156f7b145ab77a4df311eb Mon Sep 17 00:00:00 2001 From: Paul Gottschling Date: Fri, 7 Jan 2022 09:28:16 -0500 Subject: [PATCH] Assign LazyContentProvider in shiftToOutputFormat In response to PR feedback to keep all content reuse logic in shiftToOutputFormat --- hugolib/page.go | 14 ++++++++++++++ hugolib/page__output.go | 18 +++--------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/hugolib/page.go b/hugolib/page.go index d2d96204408..509a083e826 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 { diff --git a/hugolib/page__output.go b/hugolib/page__output.go index 7f1140e74d8..377e16df522 100644 --- a/hugolib/page__output.go +++ b/hugolib/page__output.go @@ -55,21 +55,9 @@ func newPageOutput( } po := &pageOutput{ - f: f, - pagePerOutputProviders: providers, - // We attempt to assign pageContentOutputs while preparing each site - // for rendering and before rendering each site. This lets us share - // content between page outputs and conserve resources. However, this - // does not guarantee that all pageContentOutputs required by templates - // will be assigned. In the case of an unanticipated request, we lazily - // initialize a pageContentOutput. - ContentProvider: page.NewLazyContentProvider(func() (page.ContentProvider, error) { - cp, err := newPageContentOutput(ps, ps.pageOutput) - if err != nil { - return nil, err - } - return cp, nil - }), + f: f, + pagePerOutputProviders: providers, + ContentProvider: page.NopPage, TableOfContentsProvider: page.NopPage, render: render, paginator: pag,