Bypassing Cloudflare Cache

Hey,

(how) is it possible to bypass the Cloudflare cache (which has a rule for “Cache Everything” set) for specific requests with a worker?
e.g. if IP is xyz I want to always go to origin (I found examples how to block, but no examples how to bypass the cache)

Additionally, the Cache Everything rule will also apply to requests fetched by a worker? This means that any request I fetch with a worker, the page rules will apply & cache it, correct?

I can’t recall now exactly how to invalidate the cache, I know there is a way, but maybe someone remembers how…

For the Worker I can tell you that it always runs, there is no way to cache it.

fetch(request, { cf: { cacheTtlByStatus: { "200-299": -1, 404: 1, "500-599": 0 } } })

3 Likes

Thanks, thats how I can not cache the response, but the question is:. how to bypass the Cloudflare cache for specific requests (if that page is already in Cloudflare cache)

Normally:
Request => Cloudflare Worker => Cloudflare Cache => Origin

With my worker
if IP === 1234 {
Request => Cloudflare Worker => Origin
}

Hi, I think i have the exactly same need as you, have you solved this out?

Yes. As far as I remember we modified the query string of the request to bypass the cloudflare cache

wow brilliant, you mean you modified the query string to match another page rule which have the Bypass Cache Level setting?

But I afraid I have no so many page rules that could be used to finish this configuration, since there are about 10 or more individual requests to be processed, and they have nothing common in the their path.

—edit

Thanks for your fabulous solution, I have made this done. XD

Yes exactly.

No, you only need 1 page rule (or if you set the cacheTTL in worker to 0 OR if you enable “Respect Origin Response Headers” in settings and provide proper headers, you don’t need any page rule) which matches your “cache bust” query string.

@muxafjdt could you please specify where to find the "Respect Origin Response Headers” setting?

Here.

https://dash.cloudflare.com/?to=/:account/:zone/caching/configuration

Then Browser Cache TTL.

3 Likes

It is not working for me, I have this condition in my worker:

let wcApiParam = url.searchParams.get('wc-api');

  //bypass cache if wc-api param is in the URL
  if( wcApiParam != 'auth'  ){
    //force cache
    response = await fetch( newRequest, { cf: { cacheTtl: 43200 } } );
  } else {
      //bypass cache
      response = await fetch( newRequest, { cf: { cacheTtl: 0 } } );
  }

Here is the response I get:

What could be wrong?? Im specifying in my worker cacheTTL to 0 when the url param is present but for some reason it still caches, the response is 302 (from disk cache)

What could be wrong with my worker?

@sebas.velandia27

The “cache-control” header is still set to “max-age=14400, must-revalidate”. This is why your web browser caches the response. In your worker, you may modify the response headers to change the cache-control value to “no-cache, max-age=0”.

1 Like

But, shouldn’t it be enough with a

await fetch(url, {cache: 'no-cache'})

or sth ?