Skip to content

Commit 3e46ba5

Browse files
committed
cache/httpcache: Add respectCacheControlNoStoreInResponse and respectCacheControlNoStoreInRequest options
This cache is used by `resources.GetRemote`. Default values are: * respectCacheControlNoStoreInResponse: false * respectCacheControlNoStoreInRequest: true This is a slightly breaking change, but the current behaviour is confusing, as: * Many servers set the `no-store` header without much consideration, see https://developer.chrome.com/docs/web-platform/bfcache-ccns for more context * We almost always want to cache the `resources.GetRemote` to disk. Fixes #13990
1 parent 4d13035 commit 3e46ba5

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

‎cache/httpcache/httpcache.go‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525

2626
// DefaultConfig holds the default configuration for the HTTP cache.
2727
var DefaultConfig = Config{
28+
RespectCacheControlNoStoreInRequest: true,
29+
RespectCacheControlNoStoreInResponse: false,
2830
Cache: Cache{
2931
For: GlobMatcher{
3032
Excludes: []string{"**"},
@@ -42,7 +44,13 @@ var DefaultConfig = Config{
4244

4345
// Config holds the configuration for the HTTP cache.
4446
type Config struct {
45-
// Configures the HTTP cache behavior (RFC 9111).
47+
// When enabled and there's a Cache-Control: no-store directive in the request, response will never be stored in disk cache.
48+
RespectCacheControlNoStoreInRequest bool
49+
50+
// When enabled and there's a Cache-Control: no-store directive in the response, response will never be stored in disk cache.
51+
RespectCacheControlNoStoreInResponse bool
52+
53+
// Enables HTTP cache behavior (RFC 9111) for these resources.
4654
// When this is not enabled for a resource, Hugo will go straight to the file cache.
4755
Cache Cache
4856

@@ -57,7 +65,9 @@ type Cache struct {
5765
}
5866

5967
func (c *Config) Compile() (ConfigCompiled, error) {
60-
var cc ConfigCompiled
68+
cc := ConfigCompiled{
69+
Base: *c,
70+
}
6171

6272
p, err := c.Cache.For.CompilePredicate()
6373
if err != nil {
@@ -127,6 +137,7 @@ func (gm GlobMatcher) IsZero() bool {
127137
}
128138

129139
type ConfigCompiled struct {
140+
Base Config
130141
For predicate.P[string]
131142
PollConfigs []PollConfigCompiled
132143
}

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ require (
3737
github.com/gobwas/glob v0.2.3
3838
github.com/gohugoio/go-i18n/v2 v2.1.3-0.20230805085216-e63c13218d0e
3939
github.com/gohugoio/hashstructure v0.5.0
40-
github.com/gohugoio/httpcache v0.7.0
40+
github.com/gohugoio/httpcache v0.8.0
4141
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0
4242
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1
4343
github.com/gohugoio/locales v0.14.0

‎go.sum‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ github.com/gohugoio/hashstructure v0.5.0 h1:G2fjSBU36RdwEJBWJ+919ERvOVqAg9tfcYp4
268268
github.com/gohugoio/hashstructure v0.5.0/go.mod h1:Ser0TniXuu/eauYmrwM4o64EBvySxNzITEOLlm4igec=
269269
github.com/gohugoio/httpcache v0.7.0 h1:ukPnn04Rgvx48JIinZvZetBfHaWE7I01JR2Q2RrQ3Vs=
270270
github.com/gohugoio/httpcache v0.7.0/go.mod h1:fMlPrdY/vVJhAriLZnrF5QpN3BNAcoBClgAyQd+lGFI=
271+
github.com/gohugoio/httpcache v0.8.0 h1:hNdsmGSELztetYCsPVgjA960zSa4dfEqqF/SficorCU=
272+
github.com/gohugoio/httpcache v0.8.0/go.mod h1:fMlPrdY/vVJhAriLZnrF5QpN3BNAcoBClgAyQd+lGFI=
271273
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0 h1:dco+7YiOryRoPOMXwwaf+kktZSCtlFtreNdiJbETvYE=
272274
github.com/gohugoio/hugo-goldmark-extensions/extras v0.5.0/go.mod h1:CRrxQTKeM3imw+UoS4EHKyrqB7Zp6sAJiqHit+aMGTE=
273275
github.com/gohugoio/hugo-goldmark-extensions/passthrough v0.3.1 h1:nUzXfRTszLliZuN0JTKeunXTRaiFX6ksaWP0puLLYAY=

‎resources/resource_factories/create/create.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ func New(rs *resources.Spec) *Client {
111111
ShouldCache: func(req *http.Request, resp *http.Response, key string) bool {
112112
return shouldCache(resp.StatusCode)
113113
},
114+
CanStore: func(reqCacheControl, respCacheControl httpcache.CacheControl) (canStore bool) {
115+
if httpCacheConfig.Base.RespectCacheControlNoStoreInResponse {
116+
if _, ok := respCacheControl["no-store"]; ok {
117+
return false
118+
}
119+
}
120+
if httpCacheConfig.Base.RespectCacheControlNoStoreInRequest {
121+
if _, ok := reqCacheControl["no-store"]; ok {
122+
return false
123+
}
124+
}
125+
return true
126+
},
114127
MarkCachedResponses: true,
115128
EnableETagPair: true,
116129
Transport: &transport{

0 commit comments

Comments
 (0)