"stale-while-revalidate" won't work when using fetch API?

I’m trying to implement dynamic-rendering on Cloudflare Workers.

Here is my code:

function handleFetchEvent(request: Request): Promise<Response> {
  const { url, headers } = request
  const prerenderUrl = `https://example.rendertron.com/render/${url}` // "example.rendertron" is managed by Cloudflare also
  const headersToSend = new Headers(headers)

  headersToSend.set('User-Agent', 'rendertron')

  const prerenderRequest = new Request(prerenderUrl, {
    headers: headersToSend,
    redirect: 'manual',

  // should get a response with header "Cache-Control: max-age=1800, s-maxage=7200, stale-while-revalidate=86400"
  return fetch(prerenderRequest, { cf: { cacheEverything: true } }) 

addEventListener('fetch', (event) => {

I want to cache the result of fetch for each URL, the s-maxage is working as expected, but after 7200 seconds later when I try to send another GET request to my workers site, I got CF-Cache-Status: MISS in my response header.

By the way, I’ve turned " Always Online" feature off already.

Anyone what’s wrong with my code?


Best to Cross-post into the Developer Discord as well since it’s a more focused user group there :slight_smile: .

Feel free to join here: Cloudflare Workers

Short answer: Cloudflare doesn’t (and has no plans to) support SWR, you can read more here: When will Cloudflare fully support stale-while-revalidate with asynchronous background revalidation?

inb4 “but they do”: no, they don’t support it. They think they do because they can’t understand the specification. :slight_smile: