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 13:11:05 -05:00
|
|
|
|
// Copyright 2024 The Hugo Authors. All rights reserved.
|
2013-09-29 02:09:03 -04:00
|
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2013-09-29 02:09:03 -04:00
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-29 02:09:03 -04:00
|
|
|
|
//
|
|
|
|
|
// 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 commands
|
|
|
|
|
|
|
|
|
|
import (
|
2018-10-03 08:58:09 -04:00
|
|
|
|
"bytes"
|
2022-03-14 11:34:23 -04:00
|
|
|
|
"context"
|
2023-06-05 03:53:53 -04:00
|
|
|
|
"crypto/tls"
|
|
|
|
|
"crypto/x509"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
"encoding/json"
|
2023-06-05 03:53:53 -04:00
|
|
|
|
"encoding/pem"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
"errors"
|
2013-09-29 02:09:03 -04:00
|
|
|
|
"fmt"
|
2019-12-10 13:56:44 -05:00
|
|
|
|
"io"
|
2014-05-15 15:07:46 -04:00
|
|
|
|
"net"
|
2013-09-29 02:09:03 -04:00
|
|
|
|
"net/http"
|
2024-02-02 10:00:48 -05:00
|
|
|
|
_ "net/http/pprof"
|
2014-08-22 07:59:59 -04:00
|
|
|
|
"net/url"
|
2014-01-26 04:48:00 -05:00
|
|
|
|
"os"
|
2018-01-14 14:58:52 -05:00
|
|
|
|
"os/signal"
|
2022-03-21 04:35:15 -04:00
|
|
|
|
"path"
|
2017-11-02 03:25:20 -04:00
|
|
|
|
"path/filepath"
|
2018-10-03 08:58:09 -04:00
|
|
|
|
"regexp"
|
2013-09-29 02:09:03 -04:00
|
|
|
|
"strconv"
|
2013-11-22 00:28:05 -05:00
|
|
|
|
"strings"
|
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 13:11:05 -05:00
|
|
|
|
"sync"
|
|
|
|
|
"sync/atomic"
|
2018-01-14 14:58:52 -05:00
|
|
|
|
"syscall"
|
2014-09-22 09:45:05 -04:00
|
|
|
|
"time"
|
2014-03-31 13:23:34 -04:00
|
|
|
|
|
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 13:11:05 -05:00
|
|
|
|
"github.com/bep/mclib"
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
"github.com/bep/debounce"
|
|
|
|
|
"github.com/bep/simplecobra"
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
|
"github.com/gohugoio/hugo/common/herrors"
|
|
|
|
|
"github.com/gohugoio/hugo/common/hugo"
|
2024-03-17 06:12:33 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
"github.com/gohugoio/hugo/common/types"
|
|
|
|
|
"github.com/gohugoio/hugo/common/urls"
|
2023-05-19 06:54:42 -04:00
|
|
|
|
"github.com/gohugoio/hugo/config"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2022-05-16 03:22:51 -04:00
|
|
|
|
"github.com/gohugoio/hugo/hugolib"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
"github.com/gohugoio/hugo/hugolib/filesystems"
|
2017-11-12 04:03:56 -05:00
|
|
|
|
"github.com/gohugoio/hugo/livereload"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
"github.com/gohugoio/hugo/tpl"
|
|
|
|
|
"github.com/gohugoio/hugo/transform"
|
|
|
|
|
"github.com/gohugoio/hugo/transform/livereloadinject"
|
2017-06-13 13:07:35 -04:00
|
|
|
|
"github.com/spf13/afero"
|
2023-06-05 03:53:53 -04:00
|
|
|
|
"github.com/spf13/cobra"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
"github.com/spf13/fsync"
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
"golang.org/x/sync/semaphore"
|
2013-09-29 02:09:03 -04:00
|
|
|
|
)
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
var (
|
|
|
|
|
logDuplicateTemplateExecuteRe = regexp.MustCompile(`: template: .*?:\d+:\d+: executing ".*?"`)
|
|
|
|
|
logDuplicateTemplateParseRe = regexp.MustCompile(`: template: .*?:\d+:\d*`)
|
|
|
|
|
)
|
2018-04-09 16:28:03 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
var logReplacer = strings.NewReplacer(
|
|
|
|
|
"can't", "can’t", // Chroma lexer doesn't do well with "can't"
|
|
|
|
|
"*hugolib.pageState", "page.Page", // Page is the public interface.
|
|
|
|
|
"Rebuild failed:", "",
|
|
|
|
|
)
|
2015-11-12 10:23:41 -05:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
const (
|
|
|
|
|
configChangeConfig = "config file"
|
|
|
|
|
configChangeGoMod = "go.mod file"
|
|
|
|
|
configChangeGoWork = "go work file"
|
|
|
|
|
)
|
2015-11-16 21:55:18 -05:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func newHugoBuilder(r *rootCommand, s *serverCommand, onConfigLoaded ...func(reloaded bool) error) *hugoBuilder {
|
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 13:11:05 -05:00
|
|
|
|
var visitedURLs *types.EvictingStringQueue
|
|
|
|
|
if s != nil && !s.disableFastRender {
|
|
|
|
|
visitedURLs = types.NewEvictingStringQueue(20)
|
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return &hugoBuilder{
|
|
|
|
|
r: r,
|
|
|
|
|
s: s,
|
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 13:11:05 -05:00
|
|
|
|
visitedURLs: visitedURLs,
|
2023-01-04 12:24:36 -05:00
|
|
|
|
fullRebuildSem: semaphore.NewWeighted(1),
|
|
|
|
|
debounce: debounce.New(4 * time.Second),
|
|
|
|
|
onConfigLoaded: func(reloaded bool) error {
|
|
|
|
|
for _, wc := range onConfigLoaded {
|
|
|
|
|
if err := wc(reloaded); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-03-18 03:54:44 -04:00
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return nil
|
2022-03-18 03:54:44 -04:00
|
|
|
|
},
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2014-05-16 17:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func newServerCommand() *serverCommand {
|
2023-06-05 03:53:53 -04:00
|
|
|
|
// Flags.
|
|
|
|
|
var uninstall bool
|
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
|
c := &serverCommand{
|
2023-05-17 12:45:23 -04:00
|
|
|
|
quit: make(chan bool),
|
2023-06-05 03:53:53 -04:00
|
|
|
|
commands: []simplecobra.Commander{
|
|
|
|
|
&simpleCommand{
|
|
|
|
|
name: "trust",
|
|
|
|
|
short: "Install the local CA in the system trust store.",
|
|
|
|
|
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
|
|
|
|
|
action := "-install"
|
|
|
|
|
if uninstall {
|
|
|
|
|
action = "-uninstall"
|
|
|
|
|
}
|
|
|
|
|
os.Args = []string{action}
|
|
|
|
|
return mclib.RunMain()
|
|
|
|
|
},
|
|
|
|
|
withc: func(cmd *cobra.Command, r *rootCommand) {
|
completion: Improve existing argument completions, add many more
Do not offer filenames to arguments not taking one, complete arguments
of options taking resource kinds, directory names, --logLevel, release
--step, config and new --format.
As an internal refactoring, use higher level functions to set flag
completions. SetAnnotation works, but is more verbose than
alternatives, and uses bash specific wording.
While at it, move setting completions next to flag definitions
consistently.
Remove superfluous --destination completer setting, which is already
set elsewhere.
2024-04-07 16:33:17 -04:00
|
|
|
|
cmd.ValidArgsFunction = cobra.NoFileCompletions
|
2023-06-05 03:53:53 -04:00
|
|
|
|
cmd.Flags().BoolVar(&uninstall, "uninstall", false, "Uninstall the local CA (but do not delete it).")
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2023-06-05 03:53:53 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return c
|
2015-10-23 12:21:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 03:53:53 -04:00
|
|
|
|
func (c *serverCommand) Commands() []simplecobra.Commander {
|
|
|
|
|
return c.commands
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
type countingStatFs struct {
|
|
|
|
|
afero.Fs
|
|
|
|
|
statCounter uint64
|
2015-10-23 12:21:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func (fs *countingStatFs) Stat(name string) (os.FileInfo, error) {
|
|
|
|
|
f, err := fs.Fs.Stat(name)
|
|
|
|
|
if err == nil {
|
|
|
|
|
if !f.IsDir() {
|
|
|
|
|
atomic.AddUint64(&fs.statCounter, 1)
|
|
|
|
|
}
|
2015-10-23 12:21:37 -04:00
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return f, err
|
2015-10-23 12:21:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
// dynamicEvents contains events that is considered dynamic, as in "not static".
|
|
|
|
|
// Both of these categories will trigger a new build, but the asset events
|
|
|
|
|
// does not fit into the "navigate to changed" logic.
|
|
|
|
|
type dynamicEvents struct {
|
|
|
|
|
ContentEvents []fsnotify.Event
|
|
|
|
|
AssetEvents []fsnotify.Event
|
2015-10-23 12:21:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
type fileChangeDetector struct {
|
|
|
|
|
sync.Mutex
|
2024-07-06 10:06:24 -04:00
|
|
|
|
current map[string]uint64
|
|
|
|
|
prev map[string]uint64
|
2018-03-18 06:07:24 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
irrelevantRe *regexp.Regexp
|
|
|
|
|
}
|
2018-01-25 11:03:29 -05:00
|
|
|
|
|
2024-07-06 10:06:24 -04:00
|
|
|
|
func (f *fileChangeDetector) OnFileClose(name string, checksum uint64) {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
f.Lock()
|
|
|
|
|
defer f.Unlock()
|
2024-07-06 10:06:24 -04:00
|
|
|
|
f.current[name] = checksum
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func (f *fileChangeDetector) PrepareNew() {
|
|
|
|
|
if f == nil {
|
2022-03-14 11:34:23 -04:00
|
|
|
|
return
|
2014-01-29 17:50:31 -05:00
|
|
|
|
}
|
2013-11-22 00:28:05 -05:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
f.Lock()
|
|
|
|
|
defer f.Unlock()
|
2021-07-05 04:38:54 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if f.current == nil {
|
2024-07-06 10:06:24 -04:00
|
|
|
|
f.current = make(map[string]uint64)
|
|
|
|
|
f.prev = make(map[string]uint64)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return
|
2015-11-16 21:55:18 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 10:06:24 -04:00
|
|
|
|
f.prev = make(map[string]uint64)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
for k, v := range f.current {
|
|
|
|
|
f.prev[k] = v
|
2015-12-02 05:42:53 -05:00
|
|
|
|
}
|
2024-07-06 10:06:24 -04:00
|
|
|
|
f.current = make(map[string]uint64)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2013-10-09 18:24:40 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func (f *fileChangeDetector) changed() []string {
|
|
|
|
|
if f == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
f.Lock()
|
|
|
|
|
defer f.Unlock()
|
|
|
|
|
var c []string
|
|
|
|
|
for k, v := range f.current {
|
|
|
|
|
vv, found := f.prev[k]
|
|
|
|
|
if !found || v != vv {
|
|
|
|
|
c = append(c, k)
|
2013-09-29 02:09:03 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return f.filterIrrelevant(c)
|
2013-09-29 02:09:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func (f *fileChangeDetector) filterIrrelevant(in []string) []string {
|
|
|
|
|
var filtered []string
|
|
|
|
|
for _, v := range in {
|
|
|
|
|
if !f.irrelevantRe.MatchString(v) {
|
|
|
|
|
filtered = append(filtered, v)
|
|
|
|
|
}
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return filtered
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 03:25:20 -04:00
|
|
|
|
type fileServer struct {
|
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 13:11:05 -05:00
|
|
|
|
baseURLs []urls.BaseURL
|
2018-10-03 08:58:09 -04:00
|
|
|
|
roots []string
|
2022-03-17 17:03:27 -04:00
|
|
|
|
errorTemplate func(err any) (io.Reader, error)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
c *serverCommand
|
2020-05-27 07:50:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 03:54:44 -04:00
|
|
|
|
func (f *fileServer) createEndpoint(i int) (*http.ServeMux, net.Listener, string, string, error) {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
r := f.c.r
|
2017-11-02 03:25:20 -04:00
|
|
|
|
baseURL := f.baseURLs[i]
|
|
|
|
|
root := f.roots[i]
|
2022-03-18 03:54:44 -04:00
|
|
|
|
port := f.c.serverPorts[i].p
|
|
|
|
|
listener := f.c.serverPorts[i].ln
|
2023-01-04 12:24:36 -05:00
|
|
|
|
logger := f.c.r.logger
|
2017-11-02 03:25:20 -04:00
|
|
|
|
|
2017-11-12 04:03:56 -05:00
|
|
|
|
if i == 0 {
|
2024-04-13 12:22:19 -04:00
|
|
|
|
r.Printf("Environment: %q\n", f.c.hugoTry().Deps.Site.Hugo().Environment)
|
2024-02-05 03:44:28 -05:00
|
|
|
|
mainTarget := "disk"
|
|
|
|
|
if f.c.r.renderToMemory {
|
|
|
|
|
mainTarget = "memory"
|
|
|
|
|
}
|
|
|
|
|
if f.c.renderStaticToDisk {
|
|
|
|
|
r.Printf("Serving pages from %s and static files from disk\n", mainTarget)
|
2017-11-12 04:03:56 -05:00
|
|
|
|
} else {
|
2024-02-05 03:44:28 -05:00
|
|
|
|
r.Printf("Serving pages from %s\n", mainTarget)
|
2017-11-12 04:03:56 -05:00
|
|
|
|
}
|
2015-11-16 21:55:18 -05:00
|
|
|
|
}
|
2014-01-26 04:48:00 -05:00
|
|
|
|
|
2023-05-19 06:54:42 -04:00
|
|
|
|
var httpFs *afero.HttpFs
|
|
|
|
|
f.c.withConf(func(conf *commonConfig) {
|
|
|
|
|
httpFs = afero.NewHttpFs(conf.fs.PublishDirServer)
|
|
|
|
|
})
|
|
|
|
|
|
2022-03-21 04:35:15 -04:00
|
|
|
|
fs := filesOnlyFs{httpFs.Dir(path.Join("/", root))}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
if i == 0 && f.c.fastRenderMode {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
r.Println("Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender")
|
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-22 11:13:21 -04:00
|
|
|
|
decorate := func(h http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-10-03 08:58:09 -04:00
|
|
|
|
if f.c.showErrorInBrowser {
|
|
|
|
|
// First check the error state
|
|
|
|
|
err := f.c.getErrorWithContext()
|
|
|
|
|
if err != nil {
|
2023-05-18 04:36:48 -04:00
|
|
|
|
f.c.errState.setWasErr(true)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
w.WriteHeader(500)
|
2019-12-10 13:56:44 -05:00
|
|
|
|
r, err := f.errorTemplate(err)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
if err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
logger.Errorln(err)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
}
|
2020-03-10 13:12:11 -04:00
|
|
|
|
|
2018-10-22 13:50:27 -04:00
|
|
|
|
port = 1313
|
2023-05-19 06:54:42 -04:00
|
|
|
|
f.c.withConf(func(conf *commonConfig) {
|
|
|
|
|
if lrport := conf.configs.GetFirstLanguageConfig().BaseURLLiveReload().Port(); lrport != 0 {
|
|
|
|
|
port = lrport
|
|
|
|
|
}
|
|
|
|
|
})
|
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 13:11:05 -05:00
|
|
|
|
lr := baseURL.URL()
|
2020-12-02 06:52:26 -05:00
|
|
|
|
lr.Host = fmt.Sprintf("%s:%d", lr.Hostname(), port)
|
|
|
|
|
fmt.Fprint(w, injectLiveReloadScript(r, lr))
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if f.c.noHTTPCache {
|
2017-09-22 14:05:19 -04:00
|
|
|
|
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
|
2017-09-22 11:13:21 -04:00
|
|
|
|
w.Header().Set("Pragma", "no-cache")
|
|
|
|
|
}
|
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
|
|
|
|
|
2023-05-19 06:54:42 -04:00
|
|
|
|
var serverConfig config.Server
|
|
|
|
|
f.c.withConf(func(conf *commonConfig) {
|
|
|
|
|
serverConfig = conf.configs.Base.Server
|
|
|
|
|
})
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
2020-06-05 06:13:26 -04:00
|
|
|
|
// Ignore any query params for the operations below.
|
2022-09-17 05:25:37 -04:00
|
|
|
|
requestURI, _ := url.PathUnescape(strings.TrimSuffix(r.RequestURI, "?"+r.URL.RawQuery))
|
2020-06-05 06:13:26 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
for _, header := range serverConfig.MatchHeaders(requestURI) {
|
2020-03-08 11:33:15 -04:00
|
|
|
|
w.Header().Set(header.Key, header.Value)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if redirect := serverConfig.MatchRedirect(requestURI); !redirect.IsZero() {
|
2022-09-17 05:25:37 -04:00
|
|
|
|
// fullName := filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name)))
|
2020-10-05 11:56:28 -04:00
|
|
|
|
doRedirect := true
|
2024-04-11 03:23:17 -04:00
|
|
|
|
// This matches Netlify's behavior and is needed for SPA behavior.
|
2020-05-27 07:50:13 -04:00
|
|
|
|
// See https://docs.netlify.com/routing/redirects/rewrites-proxies/
|
2020-10-05 11:56:28 -04:00
|
|
|
|
if !redirect.Force {
|
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 13:11:05 -05:00
|
|
|
|
path := filepath.Clean(strings.TrimPrefix(requestURI, baseURL.Path()))
|
2022-09-13 05:33:42 -04:00
|
|
|
|
if root != "" {
|
|
|
|
|
path = filepath.Join(root, path)
|
|
|
|
|
}
|
2023-05-19 06:54:42 -04:00
|
|
|
|
var fs afero.Fs
|
|
|
|
|
f.c.withConf(func(conf *commonConfig) {
|
2023-05-27 09:22:45 -04:00
|
|
|
|
fs = conf.fs.PublishDirServer
|
2023-05-19 06:54:42 -04:00
|
|
|
|
})
|
2022-09-13 05:33:42 -04:00
|
|
|
|
|
|
|
|
|
fi, err := fs.Stat(path)
|
|
|
|
|
|
2020-10-05 11:56:28 -04:00
|
|
|
|
if err == nil {
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
|
// There will be overlapping directories, so we
|
|
|
|
|
// need to check for a file.
|
2022-09-13 05:33:42 -04:00
|
|
|
|
_, err = fs.Stat(filepath.Join(path, "index.html"))
|
2020-10-05 11:56:28 -04:00
|
|
|
|
doRedirect = err != nil
|
|
|
|
|
} else {
|
|
|
|
|
doRedirect = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if doRedirect {
|
2022-09-13 05:33:42 -04:00
|
|
|
|
switch redirect.Status {
|
|
|
|
|
case 404:
|
|
|
|
|
w.WriteHeader(404)
|
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 13:11:05 -05:00
|
|
|
|
file, err := fs.Open(strings.TrimPrefix(redirect.To, baseURL.Path()))
|
2022-09-13 05:33:42 -04:00
|
|
|
|
if err == nil {
|
|
|
|
|
defer file.Close()
|
|
|
|
|
io.Copy(w, file)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Fprintln(w, "<h1>Page Not Found</h1>")
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
case 200:
|
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 13:11:05 -05:00
|
|
|
|
if r2 := f.rewriteRequest(r, strings.TrimPrefix(redirect.To, baseURL.Path())); r2 != nil {
|
2020-10-05 11:56:28 -04:00
|
|
|
|
requestURI = redirect.To
|
|
|
|
|
r = r2
|
|
|
|
|
}
|
2022-09-13 05:33:42 -04:00
|
|
|
|
default:
|
2020-10-05 11:56:28 -04:00
|
|
|
|
w.Header().Set("Content-Type", "")
|
|
|
|
|
http.Redirect(w, r, redirect.To, redirect.Status)
|
|
|
|
|
return
|
2022-09-13 05:33:42 -04:00
|
|
|
|
|
2020-05-27 07:50:13 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if f.c.fastRenderMode && f.c.errState.buildErr() == nil {
|
2020-06-05 06:13:26 -04:00
|
|
|
|
if strings.HasSuffix(requestURI, "/") || strings.HasSuffix(requestURI, "html") || strings.HasSuffix(requestURI, "htm") {
|
|
|
|
|
if !f.c.visitedURLs.Contains(requestURI) {
|
2018-10-17 03:28:04 -04:00
|
|
|
|
// If not already on stack, re-render that single page.
|
2020-06-05 06:13:26 -04:00
|
|
|
|
if err := f.c.partialReRender(requestURI); err != nil {
|
|
|
|
|
f.c.handleBuildErr(err, fmt.Sprintf("Failed to render %q", requestURI))
|
2018-10-17 03:28:04 -04:00
|
|
|
|
if f.c.showErrorInBrowser {
|
2020-06-05 06:13:26 -04:00
|
|
|
|
http.Redirect(w, r, requestURI, http.StatusMovedPermanently)
|
2018-10-17 03:28:04 -04:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 06:13:26 -04:00
|
|
|
|
f.c.visitedURLs.Add(requestURI)
|
2018-10-17 03:28:04 -04:00
|
|
|
|
|
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-27 07:50:13 -04:00
|
|
|
|
|
2017-09-22 11:13:21 -04:00
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileserver := decorate(http.FileServer(fs))
|
2017-11-02 03:25:20 -04:00
|
|
|
|
mu := http.NewServeMux()
|
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 13:11:05 -05:00
|
|
|
|
if baseURL.Path() == "" || baseURL.Path() == "/" {
|
2017-11-02 03:25:20 -04:00
|
|
|
|
mu.Handle("/", fileserver)
|
2014-08-22 07:59:59 -04:00
|
|
|
|
} else {
|
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 13:11:05 -05:00
|
|
|
|
mu.Handle(baseURL.Path(), http.StripPrefix(baseURL.Path(), fileserver))
|
2014-08-22 07:59:59 -04:00
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if r.IsTestRun() {
|
|
|
|
|
var shutDownOnce sync.Once
|
|
|
|
|
mu.HandleFunc("/__stop", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
shutDownOnce.Do(func() {
|
|
|
|
|
close(f.c.quit)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2014-08-22 07:59:59 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
endpoint := net.JoinHostPort(f.c.serverInterface, strconv.Itoa(port))
|
2017-11-02 03:25:20 -04:00
|
|
|
|
|
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 13:11:05 -05:00
|
|
|
|
return mu, listener, baseURL.String(), endpoint, nil
|
2017-11-02 03:25:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func (f *fileServer) rewriteRequest(r *http.Request, toPath string) *http.Request {
|
|
|
|
|
r2 := new(http.Request)
|
|
|
|
|
*r2 = *r
|
|
|
|
|
r2.URL = new(url.URL)
|
|
|
|
|
*r2.URL = *r.URL
|
|
|
|
|
r2.URL.Path = toPath
|
|
|
|
|
r2.Header.Set("X-Rewrite-Original-URI", r.URL.RequestURI())
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return r2
|
2018-10-03 08:58:09 -04:00
|
|
|
|
}
|
2022-05-12 05:43:20 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
type filesOnlyFs struct {
|
|
|
|
|
fs http.FileSystem
|
|
|
|
|
}
|
2022-05-12 05:43:20 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func (fs filesOnlyFs) Open(name string) (http.File, error) {
|
|
|
|
|
f, err := fs.fs.Open(name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return noDirFile{f}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type noDirFile struct {
|
|
|
|
|
http.File
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f noDirFile) Readdir(count int) ([]os.FileInfo, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type serverCommand struct {
|
|
|
|
|
r *rootCommand
|
|
|
|
|
|
|
|
|
|
commands []simplecobra.Commander
|
|
|
|
|
|
|
|
|
|
*hugoBuilder
|
|
|
|
|
|
|
|
|
|
quit chan bool // Closed when the server should shut down. Used in tests only.
|
|
|
|
|
serverPorts []serverPortListener
|
|
|
|
|
doLiveReload bool
|
|
|
|
|
|
|
|
|
|
// Flags.
|
|
|
|
|
renderStaticToDisk bool
|
|
|
|
|
navigateToChanged bool
|
|
|
|
|
serverAppend bool
|
|
|
|
|
serverInterface string
|
2023-06-05 03:53:53 -04:00
|
|
|
|
tlsCertFile string
|
|
|
|
|
tlsKeyFile string
|
|
|
|
|
tlsAuto bool
|
2024-02-02 10:00:48 -05:00
|
|
|
|
pprof bool
|
2023-01-04 12:24:36 -05:00
|
|
|
|
serverPort int
|
|
|
|
|
liveReloadPort int
|
|
|
|
|
serverWatch bool
|
|
|
|
|
noHTTPCache bool
|
|
|
|
|
disableLiveReload bool
|
|
|
|
|
disableFastRender bool
|
|
|
|
|
disableBrowserError bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *serverCommand) Name() string {
|
|
|
|
|
return "server"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *serverCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
|
2024-02-02 10:00:48 -05:00
|
|
|
|
if c.pprof {
|
|
|
|
|
go func() {
|
|
|
|
|
http.ListenAndServe("localhost:8080", nil)
|
|
|
|
|
}()
|
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
// Watch runs its own server as part of the routine
|
|
|
|
|
if c.serverWatch {
|
|
|
|
|
|
|
|
|
|
watchDirs, err := c.getDirList()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watchGroups := helpers.ExtractAndGroupRootPaths(watchDirs)
|
|
|
|
|
|
|
|
|
|
for _, group := range watchGroups {
|
|
|
|
|
c.r.Printf("Watching for changes in %s\n", group)
|
|
|
|
|
}
|
|
|
|
|
watcher, err := c.newWatcher(c.r.poll, watchDirs...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-19 06:20:13 -04:00
|
|
|
|
err := func() error {
|
|
|
|
|
defer c.r.timeTrack(time.Now(), "Built")
|
2024-02-05 09:01:15 -05:00
|
|
|
|
return c.build()
|
2023-07-19 06:20:13 -04:00
|
|
|
|
}()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return c.serve()
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
|
func (c *serverCommand) Init(cd *simplecobra.Commandeer) error {
|
|
|
|
|
cmd := cd.CobraCommand
|
2024-10-13 22:10:38 -04:00
|
|
|
|
cmd.Short = "Start the embedded web server"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
cmd.Long = `Hugo provides its own webserver which builds and serves the site.
|
|
|
|
|
While hugo server is high performance, it is a webserver with limited options.
|
|
|
|
|
|
2024-02-21 14:18:38 -05:00
|
|
|
|
The ` + "`" + `hugo server` + "`" + ` command will by default write and serve files from disk, but
|
|
|
|
|
you can render to memory by using the ` + "`" + `--renderToMemory` + "`" + ` flag. This can be
|
|
|
|
|
faster in some cases, but it will consume more memory.
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
|
|
By default hugo will also watch your files for any changes you make and
|
|
|
|
|
automatically rebuild the site. It will then live reload any open browser pages
|
|
|
|
|
and push the latest content to them. As most Hugo sites are built in a fraction
|
|
|
|
|
of a second, you will be able to save and see your changes nearly instantly.`
|
|
|
|
|
cmd.Aliases = []string{"serve"}
|
|
|
|
|
|
|
|
|
|
cmd.Flags().IntVarP(&c.serverPort, "port", "p", 1313, "port on which the server will listen")
|
completion: Improve existing argument completions, add many more
Do not offer filenames to arguments not taking one, complete arguments
of options taking resource kinds, directory names, --logLevel, release
--step, config and new --format.
As an internal refactoring, use higher level functions to set flag
completions. SetAnnotation works, but is more verbose than
alternatives, and uses bash specific wording.
While at it, move setting completions next to flag definitions
consistently.
Remove superfluous --destination completer setting, which is already
set elsewhere.
2024-04-07 16:33:17 -04:00
|
|
|
|
_ = cmd.RegisterFlagCompletionFunc("port", cobra.NoFileCompletions)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
cmd.Flags().IntVar(&c.liveReloadPort, "liveReloadPort", -1, "port for live reloading (i.e. 443 in HTTPS proxy situations)")
|
completion: Improve existing argument completions, add many more
Do not offer filenames to arguments not taking one, complete arguments
of options taking resource kinds, directory names, --logLevel, release
--step, config and new --format.
As an internal refactoring, use higher level functions to set flag
completions. SetAnnotation works, but is more verbose than
alternatives, and uses bash specific wording.
While at it, move setting completions next to flag definitions
consistently.
Remove superfluous --destination completer setting, which is already
set elsewhere.
2024-04-07 16:33:17 -04:00
|
|
|
|
_ = cmd.RegisterFlagCompletionFunc("liveReloadPort", cobra.NoFileCompletions)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
cmd.Flags().StringVarP(&c.serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind")
|
completion: Improve existing argument completions, add many more
Do not offer filenames to arguments not taking one, complete arguments
of options taking resource kinds, directory names, --logLevel, release
--step, config and new --format.
As an internal refactoring, use higher level functions to set flag
completions. SetAnnotation works, but is more verbose than
alternatives, and uses bash specific wording.
While at it, move setting completions next to flag definitions
consistently.
Remove superfluous --destination completer setting, which is already
set elsewhere.
2024-04-07 16:33:17 -04:00
|
|
|
|
_ = cmd.RegisterFlagCompletionFunc("bind", cobra.NoFileCompletions)
|
2023-06-05 03:53:53 -04:00
|
|
|
|
cmd.Flags().StringVarP(&c.tlsCertFile, "tlsCertFile", "", "", "path to TLS certificate file")
|
completion: Improve existing argument completions, add many more
Do not offer filenames to arguments not taking one, complete arguments
of options taking resource kinds, directory names, --logLevel, release
--step, config and new --format.
As an internal refactoring, use higher level functions to set flag
completions. SetAnnotation works, but is more verbose than
alternatives, and uses bash specific wording.
While at it, move setting completions next to flag definitions
consistently.
Remove superfluous --destination completer setting, which is already
set elsewhere.
2024-04-07 16:33:17 -04:00
|
|
|
|
_ = cmd.MarkFlagFilename("tlsCertFile", "pem")
|
2023-06-05 03:53:53 -04:00
|
|
|
|
cmd.Flags().StringVarP(&c.tlsKeyFile, "tlsKeyFile", "", "", "path to TLS key file")
|
completion: Improve existing argument completions, add many more
Do not offer filenames to arguments not taking one, complete arguments
of options taking resource kinds, directory names, --logLevel, release
--step, config and new --format.
As an internal refactoring, use higher level functions to set flag
completions. SetAnnotation works, but is more verbose than
alternatives, and uses bash specific wording.
While at it, move setting completions next to flag definitions
consistently.
Remove superfluous --destination completer setting, which is already
set elsewhere.
2024-04-07 16:33:17 -04:00
|
|
|
|
_ = cmd.MarkFlagFilename("tlsKeyFile", "pem")
|
2023-06-05 03:53:53 -04:00
|
|
|
|
cmd.Flags().BoolVar(&c.tlsAuto, "tlsAuto", false, "generate and use locally-trusted certificates.")
|
2024-02-02 10:00:48 -05:00
|
|
|
|
cmd.Flags().BoolVar(&c.pprof, "pprof", false, "enable the pprof server (port 8080)")
|
2023-01-04 12:24:36 -05:00
|
|
|
|
cmd.Flags().BoolVarP(&c.serverWatch, "watch", "w", true, "watch filesystem for changes and recreate as needed")
|
|
|
|
|
cmd.Flags().BoolVar(&c.noHTTPCache, "noHTTPCache", false, "prevent HTTP caching")
|
|
|
|
|
cmd.Flags().BoolVarP(&c.serverAppend, "appendPort", "", true, "append port to baseURL")
|
|
|
|
|
cmd.Flags().BoolVar(&c.disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild")
|
2024-05-30 05:35:02 -04:00
|
|
|
|
cmd.Flags().BoolVarP(&c.navigateToChanged, "navigateToChanged", "N", false, "navigate to changed content file on live browser reload")
|
2023-01-04 12:24:36 -05:00
|
|
|
|
cmd.Flags().BoolVar(&c.renderStaticToDisk, "renderStaticToDisk", false, "serve static files from disk and dynamic files from memory")
|
|
|
|
|
cmd.Flags().BoolVar(&c.disableFastRender, "disableFastRender", false, "enables full re-renders on changes")
|
|
|
|
|
cmd.Flags().BoolVar(&c.disableBrowserError, "disableBrowserError", false, "do not show build errors in the browser")
|
|
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
|
r := cd.Root.Command.(*rootCommand)
|
2023-05-28 04:44:40 -04:00
|
|
|
|
applyLocalFlagsBuild(cmd, r)
|
2023-05-17 06:45:51 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
|
func (c *serverCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
c.r = cd.Root.Command.(*rootCommand)
|
|
|
|
|
|
|
|
|
|
c.hugoBuilder = newHugoBuilder(
|
|
|
|
|
c.r,
|
|
|
|
|
c,
|
|
|
|
|
func(reloaded bool) error {
|
|
|
|
|
if !reloaded {
|
|
|
|
|
if err := c.createServerPorts(cd); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-06-05 03:53:53 -04:00
|
|
|
|
|
|
|
|
|
if (c.tlsCertFile == "" || c.tlsKeyFile == "") && c.tlsAuto {
|
|
|
|
|
c.withConfE(func(conf *commonConfig) error {
|
|
|
|
|
return c.createCertificates(conf)
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2023-06-05 03:53:53 -04:00
|
|
|
|
|
2024-04-13 12:22:19 -04:00
|
|
|
|
if err := c.setServerInfoInConfig(); err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reloaded && c.fastRenderMode {
|
2023-05-19 06:54:42 -04:00
|
|
|
|
c.withConf(func(conf *commonConfig) {
|
|
|
|
|
conf.fs.PublishDir = hugofs.NewHashingFs(conf.fs.PublishDir, c.changeDetector)
|
|
|
|
|
conf.fs.PublishDirStatic = hugofs.NewHashingFs(conf.fs.PublishDirStatic, c.changeDetector)
|
|
|
|
|
})
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
destinationFlag := cd.CobraCommand.Flags().Lookup("destination")
|
2024-02-05 03:44:28 -05:00
|
|
|
|
if c.r.renderToMemory && (destinationFlag != nil && destinationFlag.Changed) {
|
|
|
|
|
return fmt.Errorf("cannot use --renderToMemory with --destination")
|
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
c.doLiveReload = !c.disableLiveReload
|
|
|
|
|
c.fastRenderMode = !c.disableFastRender
|
|
|
|
|
c.showErrorInBrowser = c.doLiveReload && !c.disableBrowserError
|
|
|
|
|
|
|
|
|
|
if c.fastRenderMode {
|
|
|
|
|
// For now, fast render mode only. It should, however, be fast enough
|
|
|
|
|
// for the full variant, too.
|
|
|
|
|
c.changeDetector = &fileChangeDetector{
|
|
|
|
|
// We use this detector to decide to do a Hot reload of a single path or not.
|
|
|
|
|
// We need to filter out source maps and possibly some other to be able
|
|
|
|
|
// to make that decision.
|
|
|
|
|
irrelevantRe: regexp.MustCompile(`\.map$`),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.changeDetector.PrepareNew()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := c.loadConfig(cd, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 12:22:19 -04:00
|
|
|
|
func (c *serverCommand) setServerInfoInConfig() error {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if len(c.serverPorts) == 0 {
|
|
|
|
|
panic("no server ports set")
|
|
|
|
|
}
|
2023-05-19 06:54:42 -04:00
|
|
|
|
return c.withConfE(func(conf *commonConfig) error {
|
|
|
|
|
for i, language := range conf.configs.Languages {
|
2024-03-12 11:18:23 -04:00
|
|
|
|
isMultihost := conf.configs.IsMultihost
|
2023-05-19 06:54:42 -04:00
|
|
|
|
var serverPort int
|
2024-03-12 11:18:23 -04:00
|
|
|
|
if isMultihost {
|
2023-05-19 06:54:42 -04:00
|
|
|
|
serverPort = c.serverPorts[i].p
|
|
|
|
|
} else {
|
|
|
|
|
serverPort = c.serverPorts[0].p
|
|
|
|
|
}
|
|
|
|
|
langConfig := conf.configs.LanguageConfigMap[language.Lang]
|
|
|
|
|
baseURLStr, err := c.fixURL(langConfig.BaseURL, c.r.baseURL, serverPort)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
baseURL, err := urls.NewBaseURLFromString(baseURLStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create baseURL from %q: %s", baseURLStr, err)
|
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
2023-05-19 06:54:42 -04:00
|
|
|
|
baseURLLiveReload := baseURL
|
|
|
|
|
if c.liveReloadPort != -1 {
|
|
|
|
|
baseURLLiveReload, _ = baseURLLiveReload.WithPort(c.liveReloadPort)
|
|
|
|
|
}
|
2024-04-13 12:22:19 -04:00
|
|
|
|
langConfig.C.SetServerInfo(baseURL, baseURLLiveReload, c.serverInterface)
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2023-05-19 06:54:42 -04:00
|
|
|
|
return nil
|
|
|
|
|
})
|
2022-05-02 10:07:52 -04:00
|
|
|
|
}
|
2017-11-02 03:25:20 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func (c *serverCommand) getErrorWithContext() any {
|
|
|
|
|
errCount := c.errCount()
|
|
|
|
|
|
|
|
|
|
if errCount == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m := make(map[string]any)
|
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
|
m["Error"] = cleanErrorLog(c.r.logger.Errors())
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
m["Version"] = hugo.BuildVersionString()
|
|
|
|
|
ferrors := herrors.UnwrapFileErrorsWithErrorContext(c.errState.buildErr())
|
|
|
|
|
m["Files"] = ferrors
|
|
|
|
|
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 03:53:53 -04:00
|
|
|
|
func (c *serverCommand) createCertificates(conf *commonConfig) error {
|
|
|
|
|
hostname := "localhost"
|
|
|
|
|
if c.r.baseURL != "" {
|
|
|
|
|
u, err := url.Parse(c.r.baseURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
hostname = u.Hostname()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For now, store these in the Hugo cache dir.
|
|
|
|
|
// Hugo should probably introduce some concept of a less temporary application directory.
|
|
|
|
|
keyDir := filepath.Join(conf.configs.LoadingInfo.BaseConfig.CacheDir, "_mkcerts")
|
|
|
|
|
|
|
|
|
|
// Create the directory if it doesn't exist.
|
|
|
|
|
if _, err := os.Stat(keyDir); os.IsNotExist(err) {
|
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 13:11:05 -05:00
|
|
|
|
if err := os.MkdirAll(keyDir, 0o777); err != nil {
|
2023-06-05 03:53:53 -04:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.tlsCertFile = filepath.Join(keyDir, fmt.Sprintf("%s.pem", hostname))
|
|
|
|
|
c.tlsKeyFile = filepath.Join(keyDir, fmt.Sprintf("%s-key.pem", hostname))
|
|
|
|
|
|
|
|
|
|
// Check if the certificate already exists and is valid.
|
2023-07-28 04:23:20 -04:00
|
|
|
|
certPEM, err := os.ReadFile(c.tlsCertFile)
|
2023-06-05 03:53:53 -04:00
|
|
|
|
if err == nil {
|
2023-07-28 04:23:20 -04:00
|
|
|
|
rootPem, err := os.ReadFile(filepath.Join(mclib.GetCAROOT(), "rootCA.pem"))
|
2023-06-05 03:53:53 -04:00
|
|
|
|
if err == nil {
|
|
|
|
|
if err := c.verifyCert(rootPem, certPEM, hostname); err == nil {
|
|
|
|
|
c.r.Println("Using existing", c.tlsCertFile, "and", c.tlsKeyFile)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.r.Println("Creating TLS certificates in", keyDir)
|
|
|
|
|
|
|
|
|
|
// Yes, this is unfortunate, but it's currently the only way to use Mkcert as a library.
|
|
|
|
|
os.Args = []string{"-cert-file", c.tlsCertFile, "-key-file", c.tlsKeyFile, hostname}
|
|
|
|
|
return mclib.RunMain()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *serverCommand) verifyCert(rootPEM, certPEM []byte, name string) error {
|
|
|
|
|
roots := x509.NewCertPool()
|
|
|
|
|
ok := roots.AppendCertsFromPEM(rootPEM)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("failed to parse root certificate")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
block, _ := pem.Decode(certPEM)
|
|
|
|
|
if block == nil {
|
|
|
|
|
return fmt.Errorf("failed to parse certificate PEM")
|
|
|
|
|
}
|
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to parse certificate: %v", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts := x509.VerifyOptions{
|
|
|
|
|
DNSName: name,
|
|
|
|
|
Roots: roots,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := cert.Verify(opts); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to verify certificate: %v", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func (c *serverCommand) createServerPorts(cd *simplecobra.Commandeer) error {
|
|
|
|
|
flags := cd.CobraCommand.Flags()
|
2023-05-19 06:54:42 -04:00
|
|
|
|
var cerr error
|
|
|
|
|
c.withConf(func(conf *commonConfig) {
|
2024-03-12 11:18:23 -04:00
|
|
|
|
isMultihost := conf.configs.IsMultihost
|
2023-05-19 06:54:42 -04:00
|
|
|
|
c.serverPorts = make([]serverPortListener, 1)
|
2024-03-12 11:18:23 -04:00
|
|
|
|
if isMultihost {
|
2023-05-19 06:54:42 -04:00
|
|
|
|
if !c.serverAppend {
|
|
|
|
|
cerr = errors.New("--appendPort=false not supported when in multihost mode")
|
|
|
|
|
return
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2023-05-19 06:54:42 -04:00
|
|
|
|
c.serverPorts = make([]serverPortListener, len(conf.configs.Languages))
|
|
|
|
|
}
|
|
|
|
|
currentServerPort := c.serverPort
|
|
|
|
|
for i := 0; i < len(c.serverPorts); i++ {
|
|
|
|
|
l, err := net.Listen("tcp", net.JoinHostPort(c.serverInterface, strconv.Itoa(currentServerPort)))
|
|
|
|
|
if err == nil {
|
|
|
|
|
c.serverPorts[i] = serverPortListener{ln: l, p: currentServerPort}
|
|
|
|
|
} else {
|
|
|
|
|
if i == 0 && flags.Changed("port") {
|
|
|
|
|
// port set explicitly by user -- he/she probably meant it!
|
|
|
|
|
cerr = fmt.Errorf("server startup failed: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.r.Println("port", currentServerPort, "already in use, attempting to use an available port")
|
|
|
|
|
l, sp, err := helpers.TCPListen()
|
|
|
|
|
if err != nil {
|
|
|
|
|
cerr = fmt.Errorf("unable to find alternative port to use: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.serverPorts[i] = serverPortListener{ln: l, p: sp.Port}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2023-05-19 06:54:42 -04:00
|
|
|
|
|
|
|
|
|
currentServerPort = c.serverPorts[i].p + 1
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2023-05-19 06:54:42 -04:00
|
|
|
|
})
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
2023-05-19 06:54:42 -04:00
|
|
|
|
return cerr
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fixURL massages the baseURL into a form needed for serving
|
|
|
|
|
// all pages correctly.
|
2023-06-05 03:53:53 -04:00
|
|
|
|
func (c *serverCommand) fixURL(baseURLFromConfig, baseURLFromFlag string, port int) (string, error) {
|
|
|
|
|
certsSet := (c.tlsCertFile != "" && c.tlsKeyFile != "") || c.tlsAuto
|
2023-01-04 12:24:36 -05:00
|
|
|
|
useLocalhost := false
|
2023-06-05 03:53:53 -04:00
|
|
|
|
baseURL := baseURLFromFlag
|
|
|
|
|
if baseURL == "" {
|
|
|
|
|
baseURL = baseURLFromConfig
|
2023-01-04 12:24:36 -05:00
|
|
|
|
useLocalhost = true
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 03:53:53 -04:00
|
|
|
|
if !strings.HasSuffix(baseURL, "/") {
|
|
|
|
|
baseURL = baseURL + "/"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// do an initial parse of the input string
|
2023-06-05 03:53:53 -04:00
|
|
|
|
u, err := url.Parse(baseURL)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if no Host is defined, then assume that no schema or double-slash were
|
|
|
|
|
// present in the url. Add a double-slash and make a best effort attempt.
|
2023-06-05 03:53:53 -04:00
|
|
|
|
if u.Host == "" && baseURL != "/" {
|
|
|
|
|
baseURL = "//" + baseURL
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
2023-06-05 03:53:53 -04:00
|
|
|
|
u, err = url.Parse(baseURL)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if useLocalhost {
|
2023-06-05 03:53:53 -04:00
|
|
|
|
if certsSet {
|
|
|
|
|
u.Scheme = "https"
|
|
|
|
|
} else if u.Scheme == "https" {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
u.Scheme = "http"
|
|
|
|
|
}
|
|
|
|
|
u.Host = "localhost"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.serverAppend {
|
|
|
|
|
if strings.Contains(u.Host, ":") {
|
|
|
|
|
u.Host, _, err = net.SplitHostPort(u.Host)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to split baseURL hostport: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
u.Host += fmt.Sprintf(":%d", port)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return u.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *serverCommand) partialReRender(urls ...string) error {
|
|
|
|
|
defer func() {
|
|
|
|
|
c.errState.setWasErr(false)
|
|
|
|
|
}()
|
|
|
|
|
c.errState.setBuildErr(nil)
|
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 13:11:05 -05:00
|
|
|
|
visited := types.NewEvictingStringQueue(len(urls))
|
2023-01-04 12:24:36 -05:00
|
|
|
|
for _, url := range urls {
|
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 13:11:05 -05:00
|
|
|
|
visited.Add(url)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 04:36:48 -04:00
|
|
|
|
h, err := c.hugo()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
// Note: We do not set NoBuildLock as the file lock is not acquired at this stage.
|
2023-05-18 04:36:48 -04:00
|
|
|
|
return h.Build(hugolib.BuildCfg{NoBuildLock: false, RecentlyVisited: visited, PartialReRender: true, ErrRecovery: c.errState.wasErr()})
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *serverCommand) serve() error {
|
2017-11-02 03:25:20 -04:00
|
|
|
|
var (
|
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 13:11:05 -05:00
|
|
|
|
baseURLs []urls.BaseURL
|
2017-11-02 03:25:20 -04:00
|
|
|
|
roots []string
|
2023-05-19 06:54:42 -04:00
|
|
|
|
h *hugolib.HugoSites
|
2017-11-02 03:25:20 -04:00
|
|
|
|
)
|
2023-05-19 06:54:42 -04:00
|
|
|
|
err := c.withConfE(func(conf *commonConfig) error {
|
2024-03-12 11:18:23 -04:00
|
|
|
|
isMultihost := conf.configs.IsMultihost
|
2023-05-19 06:54:42 -04:00
|
|
|
|
var err error
|
|
|
|
|
h, err = c.r.HugFromConfig(conf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-11-02 03:25:20 -04:00
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
|
// We need the server to share the same logger as the Hugo build (for error counts etc.)
|
|
|
|
|
c.r.logger = h.Log
|
|
|
|
|
|
2024-03-12 11:18:23 -04:00
|
|
|
|
if isMultihost {
|
2023-05-19 06:54:42 -04:00
|
|
|
|
for _, l := range conf.configs.ConfigLangs() {
|
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 13:11:05 -05:00
|
|
|
|
baseURLs = append(baseURLs, l.BaseURL())
|
2023-05-19 06:54:42 -04:00
|
|
|
|
roots = append(roots, l.Language().Lang)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
l := conf.configs.GetFirstLanguageConfig()
|
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 13:11:05 -05:00
|
|
|
|
baseURLs = []urls.BaseURL{l.BaseURL()}
|
2023-05-19 06:54:42 -04:00
|
|
|
|
roots = []string{""}
|
2017-11-02 03:25:20 -04:00
|
|
|
|
}
|
2023-05-19 06:54:42 -04:00
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2014-01-26 04:48:00 -05:00
|
|
|
|
}
|
2017-11-02 03:25:20 -04:00
|
|
|
|
|
2023-02-18 15:47:35 -05:00
|
|
|
|
// Cache it here. The HugoSites object may be unavailable later on due to intermittent configuration errors.
|
2022-05-16 03:22:51 -04:00
|
|
|
|
// To allow the en user to change the error template while the server is running, we use
|
|
|
|
|
// the freshest template we can provide.
|
|
|
|
|
var (
|
|
|
|
|
errTempl tpl.Template
|
|
|
|
|
templHandler tpl.TemplateHandler
|
|
|
|
|
)
|
|
|
|
|
getErrorTemplateAndHandler := func(h *hugolib.HugoSites) (tpl.Template, tpl.TemplateHandler) {
|
|
|
|
|
if h == nil {
|
|
|
|
|
return errTempl, templHandler
|
|
|
|
|
}
|
|
|
|
|
templHandler := h.Tmpl()
|
|
|
|
|
errTempl, found := templHandler.Lookup("_server/error.html")
|
|
|
|
|
if !found {
|
|
|
|
|
panic("template server/error.html not found")
|
|
|
|
|
}
|
|
|
|
|
return errTempl, templHandler
|
2022-05-15 15:01:36 -04:00
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
errTempl, templHandler = getErrorTemplateAndHandler(h)
|
2022-05-15 15:01:36 -04:00
|
|
|
|
|
2017-11-02 03:25:20 -04:00
|
|
|
|
srv := &fileServer{
|
2019-12-10 13:56:44 -05:00
|
|
|
|
baseURLs: baseURLs,
|
|
|
|
|
roots: roots,
|
|
|
|
|
c: c,
|
2022-03-17 17:03:27 -04:00
|
|
|
|
errorTemplate: func(ctx any) (io.Reader, error) {
|
2022-05-16 03:22:51 -04:00
|
|
|
|
// hugoTry does not block, getErrorTemplateAndHandler will fall back
|
|
|
|
|
// to cached values if nil.
|
|
|
|
|
templ, handler := getErrorTemplateAndHandler(c.hugoTry())
|
2019-12-10 13:56:44 -05:00
|
|
|
|
b := &bytes.Buffer{}
|
2023-02-25 03:24:59 -05:00
|
|
|
|
err := handler.ExecuteWithContext(context.Background(), templ, b, ctx)
|
2019-12-10 13:56:44 -05:00
|
|
|
|
return b, err
|
|
|
|
|
},
|
2017-11-02 03:25:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
doLiveReload := !c.disableLiveReload
|
2017-11-12 04:03:56 -05:00
|
|
|
|
|
|
|
|
|
if doLiveReload {
|
|
|
|
|
livereload.Initialize()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 07:23:25 -05:00
|
|
|
|
sigs := make(chan os.Signal, 1)
|
2018-01-14 14:58:52 -05:00
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2022-03-14 11:34:23 -04:00
|
|
|
|
var servers []*http.Server
|
2018-01-14 14:58:52 -05:00
|
|
|
|
|
2022-03-18 03:54:44 -04:00
|
|
|
|
wg1, ctx := errgroup.WithContext(context.Background())
|
|
|
|
|
|
2018-02-21 03:23:43 -05:00
|
|
|
|
for i := range baseURLs {
|
2022-03-18 03:54:44 -04:00
|
|
|
|
mu, listener, serverURL, endpoint, err := srv.createEndpoint(i)
|
2023-10-04 15:25:43 -04:00
|
|
|
|
var srv *http.Server
|
2023-06-05 03:53:53 -04:00
|
|
|
|
if c.tlsCertFile != "" && c.tlsKeyFile != "" {
|
|
|
|
|
srv = &http.Server{
|
|
|
|
|
Addr: endpoint,
|
|
|
|
|
Handler: mu,
|
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
srv = &http.Server{
|
|
|
|
|
Addr: endpoint,
|
|
|
|
|
Handler: mu,
|
|
|
|
|
}
|
2022-03-14 11:34:23 -04:00
|
|
|
|
}
|
2023-06-05 03:53:53 -04:00
|
|
|
|
|
2022-03-14 11:34:23 -04:00
|
|
|
|
servers = append(servers, srv)
|
2017-11-02 03:25:20 -04:00
|
|
|
|
|
2017-11-12 04:03:56 -05:00
|
|
|
|
if doLiveReload {
|
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 13:11:05 -05:00
|
|
|
|
baseURL := baseURLs[i]
|
|
|
|
|
mu.HandleFunc(baseURL.Path()+"livereload.js", livereload.ServeJS)
|
|
|
|
|
mu.HandleFunc(baseURL.Path()+"livereload", livereload.Handler)
|
2017-11-12 04:03:56 -05:00
|
|
|
|
}
|
2023-09-11 05:38:24 -04:00
|
|
|
|
c.r.Printf("Web Server is available at %s (bind address %s) %s\n", serverURL, c.serverInterface, roots[i])
|
2022-03-18 03:54:44 -04:00
|
|
|
|
wg1.Go(func() error {
|
2023-06-05 03:53:53 -04:00
|
|
|
|
if c.tlsCertFile != "" && c.tlsKeyFile != "" {
|
|
|
|
|
err = srv.ServeTLS(listener, c.tlsCertFile, c.tlsKeyFile)
|
|
|
|
|
} else {
|
|
|
|
|
err = srv.Serve(listener)
|
|
|
|
|
}
|
2022-03-14 11:34:23 -04:00
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
2022-03-18 03:54:44 -04:00
|
|
|
|
return err
|
2017-11-02 03:25:20 -04:00
|
|
|
|
}
|
2022-03-18 03:54:44 -04:00
|
|
|
|
return nil
|
|
|
|
|
})
|
2017-11-02 03:25:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if c.r.IsTestRun() {
|
|
|
|
|
// Write a .ready file to disk to signal ready status.
|
|
|
|
|
// This is where the test is run from.
|
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 13:11:05 -05:00
|
|
|
|
var baseURLs []string
|
|
|
|
|
for _, baseURL := range srv.baseURLs {
|
|
|
|
|
baseURLs = append(baseURLs, baseURL.String())
|
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
testInfo := map[string]any{
|
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 13:11:05 -05:00
|
|
|
|
"baseURLs": baseURLs,
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2018-01-14 14:58:52 -05:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
dir := os.Getenv("WORK")
|
|
|
|
|
if dir != "" {
|
|
|
|
|
readyFile := filepath.Join(dir, ".ready")
|
|
|
|
|
// encode the test info as JSON into the .ready file.
|
|
|
|
|
b, err := json.Marshal(testInfo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2022-05-15 15:01:36 -04:00
|
|
|
|
}
|
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 13:11:05 -05:00
|
|
|
|
err = os.WriteFile(readyFile, b, 0o777)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 06:54:42 -04:00
|
|
|
|
c.r.Println("Press Ctrl+C to stop")
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
|
|
err = func() error {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-c.quit:
|
|
|
|
|
return nil
|
|
|
|
|
case <-sigs:
|
|
|
|
|
return nil
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err()
|
2022-05-15 15:01:36 -04:00
|
|
|
|
}
|
2018-04-11 03:38:58 -04:00
|
|
|
|
}
|
2022-05-15 15:01:36 -04:00
|
|
|
|
}()
|
|
|
|
|
if err != nil {
|
2023-05-19 06:54:42 -04:00
|
|
|
|
c.r.Println("Error:", err)
|
2018-04-11 03:38:58 -04:00
|
|
|
|
}
|
2018-01-14 14:58:52 -05:00
|
|
|
|
|
2022-03-14 11:34:23 -04:00
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
|
defer cancel()
|
2022-03-18 03:54:44 -04:00
|
|
|
|
wg2, ctx := errgroup.WithContext(ctx)
|
2022-03-14 11:34:23 -04:00
|
|
|
|
for _, srv := range servers {
|
|
|
|
|
srv := srv
|
2022-03-18 03:54:44 -04:00
|
|
|
|
wg2.Go(func() error {
|
2022-03-14 11:34:23 -04:00
|
|
|
|
return srv.Shutdown(ctx)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 03:54:44 -04:00
|
|
|
|
err1, err2 := wg1.Wait(), wg2.Wait()
|
|
|
|
|
if err1 != nil {
|
|
|
|
|
return err1
|
|
|
|
|
}
|
|
|
|
|
return err2
|
2013-09-29 02:09:03 -04:00
|
|
|
|
}
|
2014-08-22 07:59:59 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
type serverPortListener struct {
|
|
|
|
|
p int
|
|
|
|
|
ln net.Listener
|
|
|
|
|
}
|
2016-05-12 19:06:56 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
type staticSyncer struct {
|
|
|
|
|
c *hugoBuilder
|
|
|
|
|
}
|
2016-05-12 19:06:56 -04:00
|
|
|
|
|
2023-05-18 04:36:48 -04:00
|
|
|
|
func (s *staticSyncer) isStatic(h *hugolib.HugoSites, filename string) bool {
|
|
|
|
|
return h.BaseFs.SourceFilesystems.IsStatic(filename)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
}
|
2014-08-22 07:59:59 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func (s *staticSyncer) syncsStaticEvents(staticEvents []fsnotify.Event) error {
|
|
|
|
|
c := s.c
|
2016-05-12 19:06:56 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
syncFn := func(sourceFs *filesystems.SourceFilesystem) (uint64, error) {
|
|
|
|
|
publishDir := helpers.FilePathSeparator
|
|
|
|
|
|
|
|
|
|
if sourceFs.PublishFolder != "" {
|
|
|
|
|
publishDir = filepath.Join(publishDir, sourceFs.PublishFolder)
|
2016-05-12 19:06:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
syncer := fsync.NewSyncer()
|
2023-05-19 06:54:42 -04:00
|
|
|
|
c.withConf(func(conf *commonConfig) {
|
|
|
|
|
syncer.NoTimes = conf.configs.Base.NoTimes
|
|
|
|
|
syncer.NoChmod = conf.configs.Base.NoChmod
|
|
|
|
|
syncer.ChmodFilter = chmodFilter
|
|
|
|
|
syncer.SrcFs = sourceFs.Fs
|
|
|
|
|
syncer.DestFs = conf.fs.PublishDir
|
|
|
|
|
if c.s != nil && c.s.renderStaticToDisk {
|
|
|
|
|
syncer.DestFs = conf.fs.PublishDirStatic
|
|
|
|
|
}
|
|
|
|
|
})
|
2016-05-12 19:06:56 -04:00
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
|
logger := s.c.r.logger
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
|
|
for _, ev := range staticEvents {
|
|
|
|
|
// Due to our approach of layering both directories and the content's rendered output
|
|
|
|
|
// into one we can't accurately remove a file not in one of the source directories.
|
|
|
|
|
// If a file is in the local static dir and also in the theme static dir and we remove
|
|
|
|
|
// it from one of those locations we expect it to still exist in the destination
|
|
|
|
|
//
|
|
|
|
|
// If Hugo generates a file (from the content dir) over a static file
|
|
|
|
|
// the content generated file should take precedence.
|
|
|
|
|
//
|
|
|
|
|
// Because we are now watching and handling individual events it is possible that a static
|
|
|
|
|
// event that occupies the same path as a content generated file will take precedence
|
|
|
|
|
// until a regeneration of the content takes places.
|
|
|
|
|
//
|
|
|
|
|
// Hugo assumes that these cases are very rare and will permit this bad behavior
|
|
|
|
|
// The alternative is to track every single file and which pipeline rendered it
|
|
|
|
|
// and then to handle conflict resolution on every event.
|
|
|
|
|
|
|
|
|
|
fromPath := ev.Name
|
|
|
|
|
|
2024-02-20 08:46:03 -05:00
|
|
|
|
relPath, found := sourceFs.MakePathRelative(fromPath, true)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
|
// Not member of this virtual host.
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove || rename is harder and will require an assumption.
|
|
|
|
|
// Hugo takes the following approach:
|
|
|
|
|
// If the static file exists in any of the static source directories after this event
|
|
|
|
|
// Hugo will re-sync it.
|
|
|
|
|
// If it does not exist in all of the static directories Hugo will remove it.
|
|
|
|
|
//
|
|
|
|
|
// This assumes that Hugo has not generated content on top of a static file and then removed
|
|
|
|
|
// the source of that static file. In this case Hugo will incorrectly remove that file
|
|
|
|
|
// from the published directory.
|
|
|
|
|
if ev.Op&fsnotify.Rename == fsnotify.Rename || ev.Op&fsnotify.Remove == fsnotify.Remove {
|
|
|
|
|
if _, err := sourceFs.Fs.Stat(relPath); herrors.IsNotExist(err) {
|
|
|
|
|
// If file doesn't exist in any static dir, remove it
|
|
|
|
|
logger.Println("File no longer exists in static dir, removing", relPath)
|
2023-05-19 06:54:42 -04:00
|
|
|
|
c.withConf(func(conf *commonConfig) {
|
|
|
|
|
_ = conf.fs.PublishDirStatic.RemoveAll(relPath)
|
|
|
|
|
})
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
// If file still exists, sync it
|
|
|
|
|
logger.Println("Syncing", relPath, "to", publishDir)
|
|
|
|
|
|
|
|
|
|
if err := syncer.Sync(relPath, relPath); err != nil {
|
|
|
|
|
c.r.logger.Errorln(err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.r.logger.Errorln(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For all other event operations Hugo will sync static.
|
|
|
|
|
logger.Println("Syncing", relPath, "to", publishDir)
|
|
|
|
|
if err := syncer.Sync(filepath.Join(publishDir, relPath), relPath); err != nil {
|
|
|
|
|
c.r.logger.Errorln(err)
|
2014-08-22 07:59:59 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
|
|
return 0, nil
|
2014-08-22 07:59:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
_, err := c.doWithPublishDirs(syncFn)
|
|
|
|
|
return err
|
2014-08-22 07:59:59 -04:00
|
|
|
|
}
|
2014-09-22 09:45:05 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func chmodFilter(dst, src os.FileInfo) bool {
|
|
|
|
|
// Hugo publishes data from multiple sources, potentially
|
|
|
|
|
// with overlapping directory structures. We cannot sync permissions
|
|
|
|
|
// for directories as that would mean that we might end up with write-protected
|
|
|
|
|
// directories inside /public.
|
|
|
|
|
// One example of this would be syncing from the Go Module cache,
|
|
|
|
|
// which have 0555 directories.
|
|
|
|
|
return src.IsDir()
|
|
|
|
|
}
|
2014-09-22 09:45:05 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func cleanErrorLog(content string) string {
|
|
|
|
|
content = strings.ReplaceAll(content, "\n", " ")
|
|
|
|
|
content = logReplacer.Replace(content)
|
|
|
|
|
content = logDuplicateTemplateExecuteRe.ReplaceAllString(content, "")
|
|
|
|
|
content = logDuplicateTemplateParseRe.ReplaceAllString(content, "")
|
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
|
parts := strings.Split(content, ": ")
|
|
|
|
|
keep := make([]string, 0, len(parts))
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
if seen[part] {
|
|
|
|
|
continue
|
2014-09-22 09:45:05 -04:00
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
seen[part] = true
|
|
|
|
|
keep = append(keep, part)
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(keep, ": ")
|
|
|
|
|
}
|
2014-09-22 09:45:05 -04:00
|
|
|
|
|
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 13:11:05 -05:00
|
|
|
|
func injectLiveReloadScript(src io.Reader, baseURL *url.URL) string {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
chain := transform.Chain{livereloadinject.New(baseURL)}
|
|
|
|
|
chain.Apply(&b, src)
|
2014-09-22 09:45:05 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return b.String()
|
|
|
|
|
}
|
2014-09-22 09:45:05 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func partitionDynamicEvents(sourceFs *filesystems.SourceFilesystems, events []fsnotify.Event) (de dynamicEvents) {
|
|
|
|
|
for _, e := range events {
|
2023-05-18 04:36:48 -04:00
|
|
|
|
if !sourceFs.IsContent(e.Name) {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
de.AssetEvents = append(de.AssetEvents, e)
|
|
|
|
|
} else {
|
|
|
|
|
de.ContentEvents = append(de.ContentEvents, e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2014-09-22 09:45:05 -04:00
|
|
|
|
|
2024-03-17 06:12:33 -04:00
|
|
|
|
func pickOneWriteOrCreatePath(contentTypes config.ContentTypesProvider, events []fsnotify.Event) string {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
name := ""
|
|
|
|
|
|
|
|
|
|
for _, ev := range events {
|
|
|
|
|
if ev.Op&fsnotify.Write == fsnotify.Write || ev.Op&fsnotify.Create == fsnotify.Create {
|
2024-03-17 06:12:33 -04:00
|
|
|
|
if contentTypes.IsIndexContentFile(ev.Name) {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
return ev.Name
|
2014-09-22 09:45:05 -04:00
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
2024-03-17 06:12:33 -04:00
|
|
|
|
if contentTypes.IsContentFile(ev.Name) {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
name = ev.Name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2014-09-22 09:45:05 -04:00
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func formatByteCount(b uint64) string {
|
|
|
|
|
const unit = 1000
|
|
|
|
|
if b < unit {
|
|
|
|
|
return fmt.Sprintf("%d B", b)
|
|
|
|
|
}
|
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
|
|
|
div *= unit
|
|
|
|
|
exp++
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("%.1f %cB",
|
|
|
|
|
float64(b)/float64(div), "kMGTPE"[exp])
|
2014-09-22 09:45:05 -04:00
|
|
|
|
}
|