Caching with Vary: Accept

The support article currently shows that caching only supports Vary: Accept-Encoding. This means that many sites, especially ActivityPub sites, cannot cache dynamic content because Accept: application/activity+json and Accept: text/html, *; q=0.8 are treated as equivalent requests by Cloudflare’s cache.

Maybe a concrete example would help:

Here’s what the page looks like to browsers:

https://mastodon.lubar.me/@ben/102073792975728752

Whereas with Accept: application/activity+json, it needs to return the ActivityStreams version instead:

$ curl -sSLH 'Accept: application/activity+json' https://mastodon.lubar.me/@ben/102073792975728752 | jq .
{
  "@context": [
    "https://www.w3.org/ns/activitystreams",
    {
      "ostatus": "http://ostatus.org#",
      "atomUri": "ostatus:atomUri",
      "inReplyToAtomUri": "ostatus:inReplyToAtomUri",
      "conversation": "ostatus:conversation",
      "sensitive": "as:sensitive",
      "Hashtag": "as:Hashtag",
      "toot": "http://joinmastodon.org/ns#",
      "Emoji": "toot:Emoji",
      "focalPoint": {
        "@container": "@list",
        "@id": "toot:focalPoint"
      },
      "blurhash": "toot:blurhash"
    }
  ],
  "id": "https://mastodon.lubar.me/users/ben/statuses/102073792975728752",
  "type": "Note",
  "summary": null,
  "inReplyTo": null,
  "published": "2019-05-10T21:07:55Z",
  "url": "https://mastodon.lubar.me/@ben/102073792975728752",
  "attributedTo": "https://mastodon.lubar.me/users/ben",
  "to": [
    "https://www.w3.org/ns/activitystreams#Public"
  ],
  "cc": [
    "https://mastodon.lubar.me/users/ben/followers"
  ],
  "sensitive": false,
  "atomUri": "https://mastodon.lubar.me/users/ben/statuses/102073792975728752",
  "inReplyToAtomUri": null,
  "conversation": "tag:lubar.me,2019-05-10:objectId=4429380:objectType=Conversation",
  "content": "<p>Kinda wish <a href=\"https://community.cloudflare.com/t/caching-with-vary-accept/80109\" rel=\"nofollow noopener\" target=\"_blank\"><span class=\"invisible\">https://</span><span class=\"ellipsis\">community.cloudflare.com/t/cac</span><span class=\"invisible\">hing-with-vary-accept/80109</span></a> was fixed so I could actually have my public pages cached...</p>",
  "contentMap": {
    "en": "<p>Kinda wish <a href=\"https://community.cloudflare.com/t/caching-with-vary-accept/80109\" rel=\"nofollow noopener\" target=\"_blank\"><span class=\"invisible\">https://</span><span class=\"ellipsis\">community.cloudflare.com/t/cac</span><span class=\"invisible\">hing-with-vary-accept/80109</span></a> was fixed so I could actually have my public pages cached...</p>"
  },
  "attachment": [],
  "tag": [],
  "replies": {
    "id": "https://mastodon.lubar.me/users/ben/statuses/102073792975728752/replies",
    "type": "Collection",
    "first": {
      "type": "CollectionPage",
      "partOf": "https://mastodon.lubar.me/users/ben/statuses/102073792975728752/replies",
      "items": []
    }
  }
}

I can’t currently just do Cache-Control: public with Vary: Accept because Cloudflare ignores the Vary: Accept, and responding to either type of request with the wrong response will break things.

This means I effectively cannot use Cloudflare’s caching for most of my site.

It would be possible to make a workers script using the caching API, but I don’t imagine that’s what you’re looking for.

This is in feedback, so CF staff will likely see your suggestion.

1 Like

This topic was automatically closed after 14 days. New replies are no longer allowed.