Cache SPA html + purge via API

We use the worker code below to rewrite non-asset requests into directly requesting our SPA html file. This greatly improves cache HITs for our SPA, and has worked great coupled with the Cloudflare API which allows us to purge cache entries by URL.
Up until last Thursday/Friday that is - since then our purge calls to clear https://$HOST/ (host redacted) doen’t seem to work anymore.
The only working way to purge the cache for this URL now appears to be via the Purge Everything button in the web interface. Using Custom Purge and pasting in the same URL doesn’t work either.
Other URLs can be purged via the API just fine (/favicon.ico for example), but the / appears to be un-purgeable.

  1. Is this a newly introduced bug or intended change in behaviour?
  2. Is there another better/currently working way to implement this?
  3. Do fetch()es to URLs that match the worker route trigger a nested cloudflare worker request, or will the request immediately be forwarded the origin server?

Any help is greatly appreciated!

Worker code

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url)

  if (['GET', 'HEAD'].includes(request.method) && !/\.[a-z0-9]{2,12}$/.test(url.pathname)) {
    console.log('fetching /')
    return fetch(new URL('/', url).toString(), {
      cf: { cacheTtl: 2592000 }
    })
  }

  console.log('fetching', url.toString())
  return fetch(request)
}

Purge API Call

curl -X POST "https://api.cloudflare.com/client/v4/zones/$CF_ZONE/purge_cache" \
  -H "X-Auth-Email: $CF_AUTH_EMAIL" -H "X-Auth-Key: $CF_AUTH_TOKEN" -H "Content-Type: application/json" \
  --data '{"files": [ "https://$HOST/" ]}'

Cache HIT test

Should respond with MISS or a low age if the purge call worked.

curl -D - -o /dev/null -s https://$HOST/ | grep -E '^(cf-cache-status|age): '

Hi @mogel, I’m afraid you’re encountering the same bug described in this thread. A temporary workaround is to set a Cache Everything page rule covering the URLs in question. I would also recommend filing a support ticket so you can be notified when the bug is fixed.

Subrequests to the same zone as the worker is running on are forwarded to the origin server after going through a few intermediate proxies, including the cache. Subrequests to other zones on Cloudflare (whether or not they are owned by the same account) will invoke any workers on those routes.

Harris

1 Like

Thank you for the quick reply @harris!
Yes it looks like the same issue. Adding a Page Rule for the rewritten URL and enabling Cache Everything on it appears to work :+1:
I filed a support ticket linking to both threads, hopefully you guys find a fix soon :slight_smile:

FYI: Cloudflare deployed a fix for this now that removed the need for the work around.

@mogel So now we dont have to apply the cache everything pagerule?

That’s correct.

I am using cloudflare workers for caching all the assets of my wordpress site but how can I use it effectively along with the KV ??