Environment
N/A (logic goes back to nitro)
Reproduction
https://github.com/unjs/ocache/blob/main/src/http.ts#L156-L181
Describe the bug
Currently SWR attempts to set headers to guide a CDN like Cloudflare to do SWR in its caching layer, it does this by setting s-maxage= and stale-while-revalidate, under the assumption that s-maxage is a CDN-only maxage.
e.g.
{
swr: true,
cache: {
maxAge: 3 * 60 * 60, // 3 hours
staleMaxAge: 1 * 60 * 60, // 1 hour
},
}
Produces: Cache-Control: s-maxage=10800, stale-while-revalidate=3600
As-per Cloudflare's documentation, setting s-maxage invalidates the stale-while-revalidate directive as it's against spec, so it is only setting maxage for the cache. (It is also missing the public directive)
CDN Caching directives should use the CDN-Cache-Control header, who then ignore and pass Cache-Control as-is to the browser.
E.g. a correct response would be:
Cache-Control: public, maxage=0, must-revalidate
CDN-Cache-Control: public, max-age=10800, stale-while-revalidate=3600
It might be a good idea to allow exposing the browser cache policy to the developer by adding a browserMaxAge field.
Additional context
https://developers.cloudflare.com/cache/concepts/revalidation/#directives-that-disable-stale-while-revalidate
https://developers.cloudflare.com/cache/concepts/cache-control/
https://developers.cloudflare.com/cache/concepts/cdn-cache-control/
Logs
Environment
N/A (logic goes back to nitro)
Reproduction
https://github.com/unjs/ocache/blob/main/src/http.ts#L156-L181
Describe the bug
Currently SWR attempts to set headers to guide a CDN like Cloudflare to do SWR in its caching layer, it does this by setting
s-maxage=andstale-while-revalidate, under the assumption thats-maxageis a CDN-only maxage.e.g.
Produces:
Cache-Control: s-maxage=10800, stale-while-revalidate=3600As-per Cloudflare's documentation, setting
s-maxageinvalidates thestale-while-revalidatedirective as it's against spec, so it is only settingmaxagefor the cache. (It is also missing thepublicdirective)CDN Caching directives should use the
CDN-Cache-Controlheader, who then ignore and passCache-Controlas-is to the browser.E.g. a correct response would be:
It might be a good idea to allow exposing the browser cache policy to the developer by adding a
browserMaxAgefield.Additional context
https://developers.cloudflare.com/cache/concepts/revalidation/#directives-that-disable-stale-while-revalidate
https://developers.cloudflare.com/cache/concepts/cache-control/
https://developers.cloudflare.com/cache/concepts/cdn-cache-control/
Logs