Unable to purge from cache if request was done in worker

Hey guys,

Would be very grateful if somebody could assist with this since the Worker documentation is pretty scarce at the moment.

It appears to me that cache purging by individual URL doesn’t have an effect if I fetch() from my origin and then return a response within my worker function, eg:

let response = await fetch(request, { cf: { cacheEverything: true, cacheTtl: 10800 } })
response = new Response(response.body, response)
response.headers.set(‘Set-Cookie’, newCookie)
return response

If instead I just do:

return fetch(request, { cf: { cacheEverything: true, cacheTtl: 10800 } })

Cache purging works fine, the URL is the same in both cases. Purge Everything works fine in both cases but I don’t want to purge everything every time :wink:

It almost feels that the content is cached under a different key or something, when I fetch() it within my function

Thanks in advance!

Same issue - URL purge doesn’t seem to work at all for any pages that have been cached as a result of fetch() in worker functions?

Hi @denis5,

I’m a bit stumped by the difference between the two code snippets. Both of them should have the exact same interaction with our cache – the eyeball would happen to see a Set-Cookie header for the former snippet, and no Set-Cookie header for the latter. That might affect the eyeball’s local cache, but not Cloudflare’s.

Could you provide a complete, minimal script that has the problem, and another that does not have the problem? That’d help a lot.

Harris

1 Like

Dear @harris,

Sure, it’s a very basic function:

async function handleRequest(event) {
  const { request } = event
  let url = new URL(request.url)
  const cookie = request.headers.get('cookie')
  let params = new URLSearchParams()

  if (cookie && cookie.includes(`my-cookie=1`)) { 
    params.append('parameter', '1')
  }

  url.search = params; 
  let request2 = new Request(url, request)

  return fetch(request2, { cf: { cacheTtl: 10800, cacheEverything: true }})
}

All it does is adding a query string parameter if a specific cookie is present in request.
According to the documentation fetch() should work with the CloudFlare CDN cache, cache that can be purged via web and API.

If I associate this worker function with “/something” URL, I can see how it correctly caches it for 10800 seconds but “Purge by URL” will have no effect on “/something”

Please ignore the snippet with Set-Cookie, it seems that I can’t purge regardless of any header modifications

In fact, it appears that even if you leave the function as simple as:

async function handleRequest(event) {
  const { request } = event
 
  return fetch(request, { cf: { cacheEverything: true }})
}

It will prevent “Purge by URL” from working. By now I’ve checked for a number of times - I can correctly purge any other URL. But if I associate a worker with an URL and use fetch() in the worker - purging no longer works (except Purge Everything)

2 Likes

Got it. I’ve reproduced the behavior, and it appears to be a regression. We have an internal ticket open for fixing it.

Could you file a support ticket, and reference this thread? Then we can notify you when it’s fixed.

In the meantime, I believe a temporary workaround may be to set a Cache Everything page rule covering the URLs in question – that worked in my case.

Harris

1 Like

@harris thanks, just did that!

I created a page rule and removed “cf” object from fetch() - yes, this helped, thanks!

1 Like

For the record I also encountered this issue over at Cache SPA html + purge via API
Creating a Cache Everything page rule that matches the URL which the worker will request works, no need to remove the cf object from the request from what I can see.

FYI: Cloudflare recently deployed a fix for this that removed the need for the page rule workaround.