When setting custom cache-control headers in the _headers file, pgs.sh returns two cache-control headers in the HTTP response:
- The default cache-control from Souin middleware (e.g.,
max-age=60, must-revalidate)
- The custom cache-control from
_headers file (e.g., public, max-age=31536000, immutable)
This creates ambiguity for browsers and CDNs about which cache directives to follow.
Expected Behavior
Only one cache-control header should be present, with user-defined headers in _headers taking precedence over the middleware defaults.
Actual Behavior
Two cache-control headers are returned:
cache-control: max-age=60, must-revalidate
cache-control: public, max-age=31536000, immutable
Root Cause Analysis
The issue occurs because:
- Souin cache middleware sets a default
CacheControl value
- User headers from
_headers file are added using w.Header().Add(), which appends without overwriting
- The default cache control is configured from
PGS_CACHE_CONTROL env var or defaults to max-age=600
Suggested Solution
When applying user headers from _headers, use w.Header().Set() instead of w.Header().Add() for the cache-control header to override the middleware's default. This would ensure user-defined cache directives take precedence while preserving the ability to set other custom headers.
When setting custom
cache-controlheaders in the_headersfile, pgs.sh returns twocache-controlheaders in the HTTP response:max-age=60, must-revalidate)_headersfile (e.g.,public, max-age=31536000, immutable)This creates ambiguity for browsers and CDNs about which cache directives to follow.
Expected Behavior
Only one
cache-controlheader should be present, with user-defined headers in_headerstaking precedence over the middleware defaults.Actual Behavior
Two
cache-controlheaders are returned:Root Cause Analysis
The issue occurs because:
CacheControlvalue_headersfile are added usingw.Header().Add(), which appends without overwritingPGS_CACHE_CONTROLenv var or defaults tomax-age=600Suggested Solution
When applying user headers from
_headers, usew.Header().Set()instead ofw.Header().Add()for thecache-controlheader to override the middleware's default. This would ensure user-defined cache directives take precedence while preserving the ability to set other custom headers.