Skip to content

Commit

Permalink
Assign LazyContentProvider in shiftToOutputFormat
Browse files Browse the repository at this point in the history
In response to PR feedback to keep all content reuse logic in
shiftToOutputFormat
  • Loading branch information
ptgott committed Jan 11, 2022
1 parent cbc451c commit a49bfbe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
14 changes: 14 additions & 0 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 3 additions & 15 deletions hugolib/page__output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a49bfbe

Please sign in to comment.