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
2023-12-24 18:11:05 +00:00
|
|
|
// Copyright 2024 The Hugo Authors. All rights reserved.
|
2022-04-08 13:15:26 +00:00
|
|
|
//
|
|
|
|
// 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 i18n_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/hugolib"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestI18nFromTheme(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
files := `
|
|
|
|
-- config.toml --
|
|
|
|
[module]
|
|
|
|
[[module.imports]]
|
|
|
|
path = "mytheme"
|
|
|
|
-- i18n/en.toml --
|
|
|
|
[l1]
|
|
|
|
other = 'l1main'
|
|
|
|
[l2]
|
|
|
|
other = 'l2main'
|
|
|
|
-- themes/mytheme/i18n/en.toml --
|
|
|
|
[l1]
|
|
|
|
other = 'l1theme'
|
|
|
|
[l2]
|
|
|
|
other = 'l2theme'
|
|
|
|
[l3]
|
|
|
|
other = 'l3theme'
|
|
|
|
-- layouts/index.html --
|
|
|
|
l1: {{ i18n "l1" }}|l2: {{ i18n "l2" }}|l3: {{ i18n "l3" }}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2024-01-28 21:11:05 +00:00
|
|
|
b := hugolib.Test(t, files)
|
2022-04-08 13:15:26 +00:00
|
|
|
|
|
|
|
b.AssertFileContent("public/index.html", `
|
|
|
|
l1: l1main|l2: l2main|l3: l3theme
|
|
|
|
`)
|
|
|
|
}
|
2023-03-04 13:43:23 +00:00
|
|
|
|
|
|
|
func TestPassPageToI18n(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
files := `
|
|
|
|
-- config.toml --
|
|
|
|
-- content/_index.md --
|
|
|
|
---
|
|
|
|
title: "Home"
|
|
|
|
---
|
|
|
|
Duis quis irure id nisi sunt minim aliqua occaecat. Aliqua cillum labore consectetur quis culpa tempor quis non officia cupidatat in ad cillum. Velit irure pariatur nisi adipisicing officia reprehenderit commodo esse non.
|
|
|
|
|
|
|
|
Ullamco cupidatat nostrud ut reprehenderit. Consequat nisi culpa magna amet tempor velit reprehenderit. Ad minim eiusmod tempor nostrud eu aliquip consectetur commodo ut in aliqua enim. Cupidatat voluptate laborum consequat qui nulla laborum laborum aute ea culpa nulla dolor cillum veniam. Commodo esse tempor qui labore aute aliqua sint nulla do.
|
|
|
|
|
|
|
|
Ad deserunt esse nostrud labore. Amet reprehenderit fugiat nostrud eu reprehenderit sit reprehenderit minim deserunt esse id occaecat cillum. Ad qui Lorem cillum laboris ipsum anim in culpa ad dolor consectetur minim culpa.
|
|
|
|
|
|
|
|
Lorem cupidatat officia aute in eu commodo anim nulla deserunt occaecat reprehenderit dolore. Eu cupidatat reprehenderit ipsum sit laboris proident. Duis quis nulla tempor adipisicing. Adipisicing amet ad reprehenderit non mollit. Cupidatat proident tempor laborum sit ipsum adipisicing sunt magna labore. Eu irure nostrud cillum exercitation tempor proident. Laborum magna nisi consequat do sint occaecat magna incididunt.
|
|
|
|
|
|
|
|
Sit mollit amet esse dolore in labore aliquip eu duis officia incididunt. Esse veniam labore excepteur eiusmod occaecat ullamco magna sunt. Ipsum occaecat exercitation anim fugiat in amet excepteur excepteur aliquip laborum. Aliquip aliqua consequat officia sit sint amet aliqua ipsum eu veniam. Id enim quis ea in eu consequat exercitation occaecat veniam consequat anim nulla adipisicing minim. Ut duis cillum laboris duis non commodo eu aliquip tempor nisi aute do.
|
|
|
|
|
|
|
|
Ipsum nulla esse excepteur ut aliqua esse incididunt deserunt veniam dolore est laborum nisi veniam. Magna eiusmod Lorem do tempor incididunt ut aute aliquip ipsum ea laboris culpa. Occaecat do officia velit fugiat culpa eu minim magna sint occaecat sunt. Duis magna proident incididunt est cupidatat proident esse proident ut ipsum non dolor Lorem eiusmod. Officia quis irure id eu aliquip.
|
|
|
|
|
|
|
|
Duis anim elit in officia in in aliquip est. Aliquip nisi labore qui elit elit cupidatat ut labore incididunt eiusmod ipsum. Sit irure nulla non cupidatat exercitation sit culpa nisi ex dolore. Culpa nisi duis duis eiusmod commodo nulla.
|
|
|
|
|
|
|
|
Et magna aliqua amet qui mollit. Eiusmod aute ut anim ea est fugiat non nisi in laborum ullamco. Proident mollit sunt nostrud irure esse sunt eiusmod deserunt dolor. Irure aute ad magna est consequat duis cupidatat consequat. Enim tempor aute cillum quis ea do enim proident incididunt aliquip cillum tempor minim. Nulla minim tempor proident in excepteur consectetur veniam.
|
|
|
|
|
|
|
|
Exercitation tempor nulla incididunt deserunt laboris ad incididunt aliqua exercitation. Adipisicing laboris veniam aute eiusmod qui magna fugiat velit. Aute quis officia anim commodo id fugiat nostrud est. Quis ipsum amet velit adipisicing eu anim minim eu est in culpa aute. Esse in commodo irure enim proident reprehenderit ullamco in dolore aute cillum.
|
|
|
|
|
|
|
|
Irure excepteur ex occaecat ipsum laboris fugiat exercitation. Exercitation adipisicing velit excepteur eu culpa consequat exercitation dolore. In laboris aute quis qui mollit minim culpa. Magna velit ea aliquip veniam fugiat mollit veniam.
|
|
|
|
-- i18n/en.toml --
|
|
|
|
[a]
|
|
|
|
other = 'Reading time: {{ .ReadingTime }}'
|
|
|
|
-- layouts/index.html --
|
|
|
|
i18n: {{ i18n "a" . }}|
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2024-01-28 21:11:05 +00:00
|
|
|
b := hugolib.Test(t, files)
|
2023-03-04 13:43:23 +00:00
|
|
|
|
|
|
|
b.AssertFileContent("public/index.html", `
|
|
|
|
i18n: Reading time: 3|
|
|
|
|
`)
|
|
|
|
}
|
2023-04-22 22:58:44 +00:00
|
|
|
|
|
|
|
// Issue 9216
|
|
|
|
func TestI18nDefaultContentLanguage(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
files := `
|
|
|
|
-- config.toml --
|
|
|
|
disableKinds = ['RSS','sitemap','taxonomy','term','page','section']
|
|
|
|
defaultContentLanguage = 'es'
|
|
|
|
defaultContentLanguageInSubdir = true
|
|
|
|
[languages.es]
|
|
|
|
[languages.fr]
|
|
|
|
-- i18n/es.toml --
|
|
|
|
cat = 'gato'
|
|
|
|
-- i18n/fr.toml --
|
|
|
|
# this file intentionally empty
|
|
|
|
-- layouts/index.html --
|
|
|
|
{{ .Title }}_{{ T "cat" }}
|
|
|
|
-- content/_index.fr.md --
|
|
|
|
---
|
|
|
|
title: home_fr
|
|
|
|
---
|
|
|
|
-- content/_index.md --
|
|
|
|
---
|
|
|
|
title: home_es
|
|
|
|
---
|
|
|
|
`
|
|
|
|
|
2024-01-28 21:11:05 +00:00
|
|
|
b := hugolib.Test(t, files)
|
2023-04-22 22:58:44 +00:00
|
|
|
|
|
|
|
b.AssertFileContent("public/es/index.html", `home_es_gato`)
|
|
|
|
b.AssertFileContent("public/fr/index.html", `home_fr_gato`)
|
|
|
|
}
|