summaryrefslogtreecommitdiffstats
path: root/cache
diff options
context:
space:
mode:
authorkhayyam <ksaleem@digitalocean.com>2025-03-14 09:37:26 -0400
committerGitHub <noreply@github.com>2025-03-14 14:37:26 +0100
commitd28c84a871937dfed1e429136d28676df9d60c93 (patch)
tree501b7b8f5aa5aedc0d362662121c43337de88c56 /cache
parent61c39ae63b62667d965c2ff96d085f4eda59bcb2 (diff)
downloadhugo-d28c84a871937dfed1e429136d28676df9d60c93.tar.gz
hugo-d28c84a871937dfed1e429136d28676df9d60c93.zip
cache: Apply httpcache defaults for polling config
Previously, compiling the config with partial or missing poll configs would introduce a panic. This ensures that the default poll configs are applied in such scenarios to ensure config is valid. Fixes #13471
Diffstat (limited to 'cache')
-rw-r--r--cache/httpcache/httpcache.go13
-rw-r--r--cache/httpcache/httpcache_test.go31
2 files changed, 43 insertions, 1 deletions
diff --git a/cache/httpcache/httpcache.go b/cache/httpcache/httpcache.go
index 2e8d9b8b4..bd6d4bf7d 100644
--- a/cache/httpcache/httpcache.go
+++ b/cache/httpcache/httpcache.go
@@ -188,7 +188,7 @@ func (gm *GlobMatcher) CompilePredicate() (func(string) bool, error) {
return p, nil
}
-func DecodeConfig(bcfg config.BaseConfig, m map[string]any) (Config, error) {
+func DecodeConfig(_ config.BaseConfig, m map[string]any) (Config, error) {
if len(m) == 0 {
return DefaultConfig, nil
}
@@ -214,5 +214,16 @@ func DecodeConfig(bcfg config.BaseConfig, m map[string]any) (Config, error) {
c.Cache.For = DefaultConfig.Cache.For
}
+ for pci := range c.Polls {
+ if c.Polls[pci].For.IsZero() {
+ c.Polls[pci].For = DefaultConfig.Cache.For
+ c.Polls[pci].Disable = true
+ }
+ }
+
+ if len(c.Polls) == 0 {
+ c.Polls = DefaultConfig.Polls
+ }
+
return c, nil
}
diff --git a/cache/httpcache/httpcache_test.go b/cache/httpcache/httpcache_test.go
index e3659f97b..60c07d056 100644
--- a/cache/httpcache/httpcache_test.go
+++ b/cache/httpcache/httpcache_test.go
@@ -17,6 +17,7 @@ import (
"testing"
qt "github.com/frankban/quicktest"
+ "github.com/gohugoio/hugo/config"
)
func TestGlobMatcher(t *testing.T) {
@@ -40,3 +41,33 @@ func TestGlobMatcher(t *testing.T) {
c.Assert(p("foo/bar/foo.css"), qt.IsFalse)
c.Assert(p("foo/bar/foo.xml"), qt.IsTrue)
}
+
+func TestDefaultConfig(t *testing.T) {
+ c := qt.New(t)
+
+ _, err := DefaultConfig.Compile()
+ c.Assert(err, qt.IsNil)
+}
+
+func TestDecodeConfigInjectsDefaultAndCompiles(t *testing.T) {
+ c := qt.New(t)
+
+ cfg, err := DecodeConfig(config.BaseConfig{}, map[string]interface{}{})
+ c.Assert(err, qt.IsNil)
+ c.Assert(cfg, qt.DeepEquals, DefaultConfig)
+
+ _, err = cfg.Compile()
+ c.Assert(err, qt.IsNil)
+
+ cfg, err = DecodeConfig(config.BaseConfig{}, map[string]any{
+ "cache": map[string]any{
+ "polls": []map[string]any{
+ {"disable": true},
+ },
+ },
+ })
+ c.Assert(err, qt.IsNil)
+
+ _, err = cfg.Compile()
+ c.Assert(err, qt.IsNil)
+}