Skip to content

SWR Cache-Control Header Invalid #83

Description

@mrfrase3

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions