How to serve stale-while-revalidate using a worker?

Hi there,

Since Cloudflare doesn’t support stale-while-revalidate, we are trying to make this work using the Cache API + Worker.

What we are trying to do is something like this:

  1. First access:

If cache status != HIT -> Deliver Stale -> Fetch the origin server in Background -> Save New Cache.

  1. Second Access:

Deliver Cached content until expired and then repeat the process.


My question is: how do i make a worker deliver the STALE content? I couldn’t find that option in Cache API docs.

Also, has anyone done this with a worker before?

Ps: Please Cloudflare, make SWR work the way it should!

I’ve contacted the support about this, and here is the final considerations:

  1. CloudFlare doesn’t support serving “stale-while-revalidate” in ANY way;
  2. Stale cache is only used in case a second request comes at the same time, before the first had the chance to generate a fresh page;
  3. You can’t change this using the API either, because there’s no way to serve stale content at all (even with a worker);
  4. They plan on implementing this feature, but again, no ETA. The only official response from the engineering team is that “this is too complex to be done” and is not a priority right now.

Other CDNs (like Fastly) already support SWR flag. Nginx itself has the “proxy_background_update” option, so i really don’t get why CF haven’t done it before.

For a huge site like mine (over 100k pages), it’s the only way to serve 100% cached pages on the edge. Exporting the entire site everytime we make a minor change in one of our scripts is simply not viable.

This would be a very important feature to increase SEO score and to make the website more reliable.