Sadly Cloudflare still doesn’t properly support stale-while-revalidate (SWR).
The correct way for it to be implemented is for the client to always be served a cached object if it’s within the time set for SWR, and Cloudflare should fetch a fresh copy of the object from the server asynchronously if the object is stale.The only time a client should be served an object from the server is if the object in Cloudflare’s cache is older than the time set for SWR.
Cloudflare’s current implementation is: If an object is stale, it will not serve the stale object to the client while it asynchronously fetches an update to the cache. Instead it simply forces the client to wait for a fresh copy of the object from the server. If there is a second request made at the same time for the same object, that’s when it will serve the stale copy (so I’ve heard, I actually haven’t seen this happen yet).
The spec can be found here RFC 5861 - HTTP Cache-Control Extensions for Stale Content Fastly implemented this in 2014: https://www.fastly.com/blog/stale-while-revalidate-stale-if-error-available-today
There are a number of threads on the community going back over quite a few years asking for this. Some don’t even have any response from Cloudflare.
- Does stale-while-revalidate work?
- Better support with stale-while-revalidate
- Stale-while-revalidate is not supported
- Asynchronous stale-while-revalidate
- Stale-While-Revalidate Support - June 2020
- Stale-While-Revalidate Support
I’d like to hear the latest from Cloudflare:
- Will you be implementing stale-while-revalidate fully?
- If yes, is there a timeline?
- If no, why is that the case? What are the blockers?
Cloudflare states its mission as so:
Cloudflare is on a mission to help build a better Internet.
If we look at the SWR spec, we find this definition.
The stale-while-revalidate HTTP Cache-Control extension allows a cache to immediately return a stale response while it revalidates it in the background, thereby hiding latency (both in the network and on the server) from clients.
The part in bold I believe highlights why fully implementing this standard should be a priority for Cloudflare as it fits perfectly with the mission of the company. One of the main goals of any CDN is to reduce latency for clients, I’m struggling to understand why this feature hasn’t been adopted yet.