Dan Fabulich
Dan Fabulich

Reputation: 39593

Does Cloudflare support stale-while-revalidate?

Cloudflare documents a list of directives for the Cache-Control header, including stale-while-revalidate.

  • stale-while-revalidate=<seconds>
    When present in an HTTP response, the stale-while-revalidate Cache-Control extension indicates that caches MAY serve the response in which it appears after it becomes stale, up to the indicated number of seconds since the object was originally retrieved.

I set my Cache-Control header to public, max-age=0, stale-while-revalidate=30 but I never seem to get a cache hit. Does Cloudflare actually support this?

Upvotes: 7

Views: 1768

Answers (3)

Dan Fabulich
Dan Fabulich

Reputation: 39593

No, Cloudflare doesn't support stale-while-revalidate.

The last word from them was in a blog post from April 2024.

https://blog.cloudflare.com/browser-rendering-api-ga-rolling-out-cloudflare-snippets-swr-and-bringing-workers-for-platforms-to-our-paygo-plans/

Coming soon: asynchronous revalidation with stale-while-revalidate

One of the features most requested by our customers is the asynchronous revalidation with stale-while-revalidate (SWR) cache directive, and we will be bringing this to you in the second half of 2024.

There's a forum thread about it here: https://community.cloudflare.com/t/support-for-stale-while-revalidate/496788

Upvotes: 3

David
David

Reputation: 1103

Ran into this question / issue today, and the Cloudflare documentation is not very clear, but there is a clue under their examples section on this page.

Cache-Control: max-age=600, stale-while-revalidate=30

This configuration indicates the asset is fresh for 600 seconds, and can be served stale for up to an additional 30 seconds to parallel requests for the same resource while the initial synchronous revalidation is attempted.

So, Cloudflare does support the directive, however their implementation is a bit different than one might expect. Essentially the first request made to your server once the cached content is expire will result in a synchronous request to your Origin, which that client must wait for (the behavior most people don't want).

However, there is some support because any parallel requests (e.g. made by other clients) for that same resource will get the old / cached content instead (with a CF-Cache-Status: UPDATING). Tested this and was able to confirm behavior.

In this way, if you have a very slow origin response, or a very popular cached resource, only a single client / user is held up, and the rest get served up the stale cached copy from Cloudflare. Better than nothing.

Upvotes: 2

Michał Zaborowski
Michał Zaborowski

Reputation: 4387

If you put max-age=0, then effectively nothing is cached, if so, then there is nothing to serve. Try with max-age=1 - that should give some results in that 30 sec. period.

Upvotes: 0

Related Questions