Sample find/replace script, does it hit the origin server every time or use the cache?


#1

I’m trying to understand and learn from the sample find/replace script posted by @KentonVarda at https://blog.cloudflare.com/introducing-cloudflare-workers/

Looking at this part:

  async function fetchAndReplace(request) {
  // Fetch from origin server.
  let response = await fetch(request)

Does the script contact the origin server (my server) each time the worker runs, or will it use the cached version of the page if available, and then run the worker on the cached version?

Thanks,

Chris


#2

Assuming your site is properly configured for caching, then a cached version will be used. fetch() calls check the cache before proceeding to origin.

Note that by default Cloudflare does not consider HTML pages to be cacheable, since on many sites they contain dynamic content. You can override this by setting the “Cache Level” page rule to “Cache Everything”, or you can specify this override on a specific request from Workers like so:

await fetch(request, {cf: {cacheEverything: true}})

#3

Perfect!

I do have cache everything set, so everything is good to go :slight_smile:

Thank you!