Skip to content

Commit d28c84a

Browse files
authoredMar 14, 2025··
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
1 parent 61c39ae commit d28c84a

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
 

Diff for: ‎cache/httpcache/httpcache.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (gm *GlobMatcher) CompilePredicate() (func(string) bool, error) {
188188
return p, nil
189189
}
190190

191-
func DecodeConfig(bcfg config.BaseConfig, m map[string]any) (Config, error) {
191+
func DecodeConfig(_ config.BaseConfig, m map[string]any) (Config, error) {
192192
if len(m) == 0 {
193193
return DefaultConfig, nil
194194
}
@@ -214,5 +214,16 @@ func DecodeConfig(bcfg config.BaseConfig, m map[string]any) (Config, error) {
214214
c.Cache.For = DefaultConfig.Cache.For
215215
}
216216

217+
for pci := range c.Polls {
218+
if c.Polls[pci].For.IsZero() {
219+
c.Polls[pci].For = DefaultConfig.Cache.For
220+
c.Polls[pci].Disable = true
221+
}
222+
}
223+
224+
if len(c.Polls) == 0 {
225+
c.Polls = DefaultConfig.Polls
226+
}
227+
217228
return c, nil
218229
}

Diff for: ‎cache/httpcache/httpcache_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"testing"
1818

1919
qt "github.com/frankban/quicktest"
20+
"github.com/gohugoio/hugo/config"
2021
)
2122

2223
func TestGlobMatcher(t *testing.T) {
@@ -40,3 +41,33 @@ func TestGlobMatcher(t *testing.T) {
4041
c.Assert(p("foo/bar/foo.css"), qt.IsFalse)
4142
c.Assert(p("foo/bar/foo.xml"), qt.IsTrue)
4243
}
44+
45+
func TestDefaultConfig(t *testing.T) {
46+
c := qt.New(t)
47+
48+
_, err := DefaultConfig.Compile()
49+
c.Assert(err, qt.IsNil)
50+
}
51+
52+
func TestDecodeConfigInjectsDefaultAndCompiles(t *testing.T) {
53+
c := qt.New(t)
54+
55+
cfg, err := DecodeConfig(config.BaseConfig{}, map[string]interface{}{})
56+
c.Assert(err, qt.IsNil)
57+
c.Assert(cfg, qt.DeepEquals, DefaultConfig)
58+
59+
_, err = cfg.Compile()
60+
c.Assert(err, qt.IsNil)
61+
62+
cfg, err = DecodeConfig(config.BaseConfig{}, map[string]any{
63+
"cache": map[string]any{
64+
"polls": []map[string]any{
65+
{"disable": true},
66+
},
67+
},
68+
})
69+
c.Assert(err, qt.IsNil)
70+
71+
_, err = cfg.Compile()
72+
c.Assert(err, qt.IsNil)
73+
}

0 commit comments

Comments
 (0)
Please sign in to comment.