hugo/resources/page/site.go
Bjørn Erik Pedersen 7285e74090
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
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
2024-01-27 16:28:14 +01:00

485 lines
9.8 KiB
Go

// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package page
import (
"html/template"
"time"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/config/privacy"
"github.com/gohugoio/hugo/config/services"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/navigation"
)
// Site represents a site. There can be multople sites in a multilingual setup.
type Site interface {
// Returns the Language configured for this Site.
Language() *langs.Language
// Returns all the languages configured for all sites.
Languages() langs.Languages
GetPage(ref ...string) (Page, error)
// AllPages returns all pages for all languages.
AllPages() Pages
// Returns all the regular Pages in this Site.
RegularPages() Pages
// Returns all Pages in this Site.
Pages() Pages
// Returns all the top level sections.
Sections() Pages
// A shortcut to the home
Home() Page
// Returns true if we're running in a server.
// Deprecated: use hugo.IsServer instead
IsServer() bool
// Returns the server port.
ServerPort() int
// Returns the configured title for this Site.
Title() string
// Returns the configured language code for this Site.
// Deprecated: Use .Language.LanguageCode instead.
LanguageCode() string
// Returns the configured copyright information for this Site.
Copyright() string
// Returns all Sites for all languages.
Sites() Sites
// Returns Site currently rendering.
Current() Site
// Returns a struct with some information about the build.
Hugo() hugo.HugoInfo
// Returns the BaseURL for this Site.
BaseURL() string
// Returns a taxonomy map.
Taxonomies() TaxonomyList
// Returns the last modification date of the content.
// Deprecated: Use .Lastmod instead.
LastChange() time.Time
// Returns the last modification date of the content.
Lastmod() time.Time
// Returns the Menus for this site.
Menus() navigation.Menus
// The main sections in the site.
MainSections() []string
// Returns the Params configured for this site.
Params() maps.Params
// Param is a convenience method to do lookups in Params.
Param(key any) (any, error)
// Returns a map of all the data inside /data.
Data() map[string]any
// Returns the site config.
Config() SiteConfig
// Author is deprecated and will be removed in a future release.
Author() map[string]interface{}
// Authors is deprecated and will be removed in a future release.
Authors() AuthorList
// Returns the social links for this site.
Social() map[string]string
// Deprecated: Use Config().Services.GoogleAnalytics instead.
GoogleAnalytics() string
// Deprecated: Use Config().Privacy.Disqus instead.
DisqusShortname() string
// BuildDrafts is deprecated and will be removed in a future release.
BuildDrafts() bool
// IsMultiLingual reports whether this site is configured with more than one language.
IsMultiLingual() bool
// LanguagePrefix returns the language prefix for this site.
LanguagePrefix() string
// Deprecated. Use site.Home.OutputFormats.Get "rss" instead.
RSSLink() template.URL
}
// Sites represents an ordered list of sites (languages).
type Sites []Site
// First is a convenience method to get the first Site, i.e. the main language.
func (s Sites) First() Site {
if len(s) == 0 {
return nil
}
return s[0]
}
// Some additional interfaces implemented by siteWrapper that's not on Site.
var _ identity.ForEeachIdentityByNameProvider = (*siteWrapper)(nil)
type siteWrapper struct {
s Site
}
func WrapSite(s Site) Site {
if s == nil {
panic("Site is nil")
}
return &siteWrapper{s: s}
}
func (s *siteWrapper) Key() string {
return s.s.Language().Lang
}
func (s *siteWrapper) Social() map[string]string {
return s.s.Social()
}
func (s *siteWrapper) Author() map[string]interface{} {
return s.s.Author()
}
func (s *siteWrapper) Authors() AuthorList {
return AuthorList{}
}
// Deprecated: Use .Site.Config.Services.GoogleAnalytics.ID instead
func (s *siteWrapper) GoogleAnalytics() string {
return s.s.GoogleAnalytics()
}
func (s *siteWrapper) GetPage(ref ...string) (Page, error) {
return s.s.GetPage(ref...)
}
func (s *siteWrapper) Language() *langs.Language {
return s.s.Language()
}
func (s *siteWrapper) Languages() langs.Languages {
return s.s.Languages()
}
func (s *siteWrapper) AllPages() Pages {
return s.s.AllPages()
}
func (s *siteWrapper) RegularPages() Pages {
return s.s.RegularPages()
}
func (s *siteWrapper) Pages() Pages {
return s.s.Pages()
}
func (s *siteWrapper) Sections() Pages {
return s.s.Sections()
}
func (s *siteWrapper) Home() Page {
return s.s.Home()
}
// Deprecated: use hugo.IsServer instead
func (s *siteWrapper) IsServer() bool {
return s.s.IsServer()
}
func (s *siteWrapper) ServerPort() int {
return s.s.ServerPort()
}
func (s *siteWrapper) Title() string {
return s.s.Title()
}
func (s *siteWrapper) LanguageCode() string {
return s.s.LanguageCode()
}
func (s *siteWrapper) Copyright() string {
return s.s.Copyright()
}
func (s *siteWrapper) Sites() Sites {
return s.s.Sites()
}
func (s *siteWrapper) Current() Site {
return s.s.Current()
}
func (s *siteWrapper) Config() SiteConfig {
return s.s.Config()
}
func (s *siteWrapper) Hugo() hugo.HugoInfo {
return s.s.Hugo()
}
func (s *siteWrapper) BaseURL() string {
return s.s.BaseURL()
}
func (s *siteWrapper) Taxonomies() TaxonomyList {
return s.s.Taxonomies()
}
func (s *siteWrapper) LastChange() time.Time {
return s.s.Lastmod()
}
func (s *siteWrapper) Lastmod() time.Time {
return s.s.Lastmod()
}
func (s *siteWrapper) Menus() navigation.Menus {
return s.s.Menus()
}
func (s *siteWrapper) MainSections() []string {
return s.s.MainSections()
}
func (s *siteWrapper) Params() maps.Params {
return s.s.Params()
}
func (s *siteWrapper) Param(key any) (any, error) {
return s.s.Param(key)
}
func (s *siteWrapper) Data() map[string]any {
return s.s.Data()
}
func (s *siteWrapper) BuildDrafts() bool {
return s.s.BuildDrafts()
}
func (s *siteWrapper) IsMultiLingual() bool {
return s.s.IsMultiLingual()
}
// Deprecated: Use .Site.Config.Services.Disqus.Shortname instead
func (s *siteWrapper) DisqusShortname() string {
return s.s.DisqusShortname()
}
func (s *siteWrapper) LanguagePrefix() string {
return s.s.LanguagePrefix()
}
func (s *siteWrapper) RSSLink() template.URL {
return s.s.RSSLink()
}
// For internal use only.
func (s *siteWrapper) ForEeachIdentityByName(name string, f func(identity.Identity) bool) {
s.s.(identity.ForEeachIdentityByNameProvider).ForEeachIdentityByName(name, f)
}
type testSite struct {
h hugo.HugoInfo
l *langs.Language
}
func (s testSite) Author() map[string]interface{} {
return nil
}
func (s testSite) Authors() AuthorList {
return AuthorList{}
}
func (s testSite) Social() map[string]string {
return make(map[string]string)
}
func (t testSite) Hugo() hugo.HugoInfo {
return t.h
}
func (t testSite) ServerPort() int {
return 1313
}
func (testSite) LastChange() (t time.Time) {
return
}
func (testSite) Lastmod() (t time.Time) {
return
}
func (t testSite) Title() string {
return "foo"
}
func (t testSite) LanguageCode() string {
return t.l.Lang
}
func (t testSite) Copyright() string {
return ""
}
func (t testSite) Sites() Sites {
return nil
}
func (t testSite) Sections() Pages {
return nil
}
func (t testSite) GetPage(ref ...string) (Page, error) {
return nil, nil
}
func (t testSite) Current() Site {
return t
}
func (s testSite) LanguagePrefix() string {
return ""
}
func (t testSite) Languages() langs.Languages {
return nil
}
// Deprecated: Use .Site.Config.Services.GoogleAnalytics.ID instead
func (t testSite) GoogleAnalytics() string {
return ""
}
func (t testSite) MainSections() []string {
return nil
}
// Deprecated: use hugo.IsServer instead
func (t testSite) IsServer() bool {
return false
}
func (t testSite) Language() *langs.Language {
return t.l
}
func (t testSite) Home() Page {
return nil
}
func (t testSite) Pages() Pages {
return nil
}
func (t testSite) AllPages() Pages {
return nil
}
func (t testSite) RegularPages() Pages {
return nil
}
func (t testSite) Menus() navigation.Menus {
return nil
}
func (t testSite) Taxonomies() TaxonomyList {
return nil
}
func (t testSite) BaseURL() string {
return ""
}
func (t testSite) Params() maps.Params {
return nil
}
func (t testSite) Data() map[string]any {
return nil
}
func (s testSite) Config() SiteConfig {
return SiteConfig{}
}
// Deprecated: Use .Site.Config.Services.Disqus.Shortname instead
func (testSite) DisqusShortname() string {
return ""
}
func (s testSite) BuildDrafts() bool {
return false
}
func (s testSite) IsMultiLingual() bool {
return false
}
func (s testSite) Param(key any) (any, error) {
return nil, nil
}
func (s testSite) RSSLink() template.URL {
return ""
}
// NewDummyHugoSite creates a new minimal test site.
func NewDummyHugoSite(conf config.AllProvider) Site {
return testSite{
h: hugo.NewInfo(conf, nil),
l: &langs.Language{
Lang: "en",
},
}
}
// SiteConfig holds the config in site.Config.
type SiteConfig struct {
// This contains all privacy related settings that can be used to
// make the YouTube template etc. GDPR compliant.
Privacy privacy.Config
// Services contains config for services such as Google Analytics etc.
Services services.Config
}