Page Rules cache do not work but work in Workers

Hello,

Based on this DOC: https://developers.cloudflare.com/cache/how-to/purge-cache#general-workflow-for-cache-tags

Page rules should work after a worker generates the response.

I have a case where a worker should go to AWS S3 origin and on response 403, a worker should generate a new response (some JSON file).

I wrote:

async function handleRequest(event) {

   const cache = caches.default

   let cacheUrl = new URL(event.request.url)
   let request = new Request(cacheUrl)
   let cacheKey = new Request(cacheUrl.toString(), request)
    
    let response = await cache.match(cacheKey)

    if (!response) {
        let response = await fetch(cacheUrl)
        
        if(response.status != 200){
               response = new Response(JSON.stringify(default_Config))          
         }
         else{
           response = new Response(response.body, response)      
         }
        
           response.headers.set("Cache-Control", "public, max-age=86400")

          event.waitUntil(cache.put(cacheKey, response.clone()))    
     }
      return response
}

It works Properly.


The main problem of a cache algorithm inside the workers: you cannot purge the cache through an API. Purge cache works only for Page rules.

So I removed a cache logic from a worker :

async function handleRequest(event) {

l    et cacheUrl = new URL(event.request.url)
    
    let response = await fetch(cacheUrl)
    
    if(response.status != 200){
           response = new Response(JSON.stringify(default_Config))          
     }
     else{
       response = new Response(response.body, response)      
     }
    
       response.headers.set("Cache-Control", "public, max-age=86400")
      return response
}

However, the Page Rules I created never worked for me.

My Rules are:

https://staging-cdn-testsettings.domain.com/*
Browser Cache TTL: an hour, Cache Level: Cache Everything, Edge Cache TTL: 2 days

Any ideas?

Solved.

The rules work only when Worker goes to origin (S3 in my case).
If the origin returns 403, I use Worker’s cache

1 Like