I need to understand,use of s-maxage
header for caching, are there any example to help me understand in which cases i should use both s-maxage
and max-age
?
s-maxage=seconds — Indicates that in shared caches, the maximum age specified by this directive overrides the maximum age specified by either the max-age directive or the Expires header field. The s-maxage directive also implies the semantics of the proxy-revalidate response directive. Browsers ignore s-maxage .
A cache max-age is a positive integer, expressing a number of seconds. Cache max-ages are passed around as individual integers, because a given cache item can only logically have a single max-age. Examples: 60 means cacheable for 60 seconds. 100 means cacheable for 100 seconds.
max-age. The max-age directive states the maximum amount of time in seconds that fetched responses are allowed to be used again (from the time when a request is made). For instance, max-age=90 indicates that an asset can be reused (remains in the browser cache) for the next 90 seconds.
As suggested by this comment:
For reverse proxies or other caches on servers, it can actually make sense to have shorter
s-maxage
thanmax-age
.
A reverse caching proxy will be able to serve the same resource to any number of clients for as long as s-maxage
. The load on the proxy will increase with the number of clients, so it may be worth having the proxy's single check (s-maxage
) be more frequent than the requests from downstream clients (e.g. browsers) (max-age
).
To give a more concrete example: you might want to suggest that a CDN should check regularly (hourly) for an updated resource on an origin server. However, it may be fine for any end user to keep using the response they get for days; users with cold caches will get the updated version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With