Does Cloudflare support stale-while-revalidate?
Asked Answered
B

3

6

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?

Biegel answered 6/1, 2018 at 4:59 Comment(4)
sorry for asking here, but just saw your question and its locked community.cloudflare.com/t/does-stale-while-revalidate-work/… do you know if they are suppring it now? I tried to test and it seems they still not support it but I am not sureGourmand
@AmirBar I have just done some testing myself, and i cant get it to serve stale. I can get it to serve me a stale response IF i open another browser and hit same URL. But the initiator still doesn't get a stale response, thus it is blocked. This behaviour is the same as if you dont include the "stale-while-revalidate" setting in the Cache-Control header.Iasis
I've tried all day to get it working. Even without this header I managed to get STALE responses if I take my server down - unless I send must-revalidate. But I couldn't get stale-while-revalidate to do anything different.Demon
Latest word from them is that they don't support it (despite mentioning it in their docs in a way that suggests otherwise), but it is scheduled for 2024 H2 blog.cloudflare.com/… see halfway down the page.Escape
B
2

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

Biegel answered 28/7, 2023 at 23:34 Comment(0)
I
1

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.

Ideatum answered 23/5, 2021 at 15:51 Comment(1)
That's not stale-while-revalidate. That's just Cloudflare's default behavior. That feature (which is genuinely interesting) is on by default, and isn’t affected by the SWR header. (Even setting stale-while-revalidate=0 doesn’t turn the feature off!) I did a lengthy analysis of that feature here: community.cloudflare.com/t/does-stale-while-revalidate-work/…Biegel
G
0

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.

Goolsby answered 18/2, 2018 at 13:23 Comment(2)
That's a good guess! But it doesn't work. After waiting a full second between requests, I never get a cache hit.Biegel
I have tested this with max-age=1, and a stale-while-revalidate=30. I still dont get a STALE response from Cloudflare. It seems like this is not supported.Iasis

© 2022 - 2025 — McMap. All rights reserved.