diff options
author | Bjørn Erik Pedersen <[email protected]> | 2024-05-17 17:06:47 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2024-06-04 16:07:39 +0200 |
commit | 447108fed2842e264897659856e9fd9cdc32ca23 (patch) | |
tree | 53687693e04496919dd84266cc1edc16746101b0 /commands | |
parent | c71e24af5172e230baa5f7dfa2078721cda38df4 (diff) | |
download | hugo-447108fed2842e264897659856e9fd9cdc32ca23.tar.gz hugo-447108fed2842e264897659856e9fd9cdc32ca23.zip |
Add a HTTP cache for remote resources.
Fixes #12502
Closes #11891
Diffstat (limited to 'commands')
-rw-r--r-- | commands/commandeer.go | 14 | ||||
-rw-r--r-- | commands/hugobuilder.go | 32 |
2 files changed, 44 insertions, 2 deletions
diff --git a/commands/commandeer.go b/commands/commandeer.go index 59fe32f74..f18a95bb9 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -48,6 +48,7 @@ import ( "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugolib" + "github.com/gohugoio/hugo/identity" "github.com/gohugoio/hugo/resources/kinds" "github.com/spf13/afero" "github.com/spf13/cobra" @@ -103,6 +104,9 @@ type rootCommand struct { commonConfigs *lazycache.Cache[int32, *commonConfig] hugoSites *lazycache.Cache[int32, *hugolib.HugoSites] + // changesFromBuild received from Hugo in watch mode. + changesFromBuild chan []identity.Identity + commands []simplecobra.Commander // Flags @@ -304,7 +308,7 @@ func (r *rootCommand) ConfigFromProvider(key int32, cfg config.Provider) (*commo func (r *rootCommand) HugFromConfig(conf *commonConfig) (*hugolib.HugoSites, error) { h, _, err := r.hugoSites.GetOrCreate(r.configVersionID.Load(), func(key int32) (*hugolib.HugoSites, error) { - depsCfg := deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, LogOut: r.logger.Out(), LogLevel: r.logger.Level()} + depsCfg := r.newDepsConfig(conf) return hugolib.NewHugoSites(depsCfg) }) return h, err @@ -316,12 +320,16 @@ func (r *rootCommand) Hugo(cfg config.Provider) (*hugolib.HugoSites, error) { if err != nil { return nil, err } - depsCfg := deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, LogOut: r.logger.Out(), LogLevel: r.logger.Level()} + depsCfg := r.newDepsConfig(conf) return hugolib.NewHugoSites(depsCfg) }) return h, err } +func (r *rootCommand) newDepsConfig(conf *commonConfig) deps.DepsCfg { + return deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, LogOut: r.logger.Out(), LogLevel: r.logger.Level(), ChangesFromBuild: r.changesFromBuild} +} + func (r *rootCommand) Name() string { return "hugo" } @@ -408,6 +416,8 @@ func (r *rootCommand) PreRun(cd, runner *simplecobra.Commandeer) error { return err } + r.changesFromBuild = make(chan []identity.Identity, 10) + r.commonConfigs = lazycache.New(lazycache.Options[int32, *commonConfig]{MaxEntries: 5}) // We don't want to keep stale HugoSites in memory longer than needed. r.hugoSites = lazycache.New(lazycache.Options[int32, *hugolib.HugoSites]{ diff --git a/commands/hugobuilder.go b/commands/hugobuilder.go index 32b7e1de8..99bd8a04a 100644 --- a/commands/hugobuilder.go +++ b/commands/hugobuilder.go @@ -43,6 +43,7 @@ import ( "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/hugolib/filesystems" + "github.com/gohugoio/hugo/identity" "github.com/gohugoio/hugo/livereload" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/watcher" @@ -343,6 +344,24 @@ func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*wa go func() { for { select { + case changes := <-c.r.changesFromBuild: + unlock, err := h.LockBuild() + if err != nil { + c.r.logger.Errorln("Failed to acquire a build lock: %s", err) + return + } + c.changeDetector.PrepareNew() + err = c.rebuildSitesForChanges(changes) + if err != nil { + c.r.logger.Errorln("Error while watching:", err) + } + if c.s != nil && c.s.doLiveReload { + if c.changeDetector == nil || len(c.changeDetector.changed()) > 0 { + livereload.ForceRefresh() + } + } + unlock() + case evs := <-watcher.Events: unlock, err := h.LockBuild() if err != nil { @@ -1019,6 +1038,19 @@ func (c *hugoBuilder) rebuildSites(events []fsnotify.Event) error { return h.Build(hugolib.BuildCfg{NoBuildLock: true, RecentlyVisited: c.visitedURLs, ErrRecovery: c.errState.wasErr()}, events...) } +func (c *hugoBuilder) rebuildSitesForChanges(ids []identity.Identity) error { + c.errState.setBuildErr(nil) + h, err := c.hugo() + if err != nil { + return err + } + whatChanged := &hugolib.WhatChanged{} + whatChanged.Add(ids...) + err = h.Build(hugolib.BuildCfg{NoBuildLock: true, WhatChanged: whatChanged, RecentlyVisited: c.visitedURLs, ErrRecovery: c.errState.wasErr()}) + c.errState.setBuildErr(err) + return err +} + func (c *hugoBuilder) reloadConfig() error { c.r.Reset() c.r.configVersionID.Add(1) |