A little coding and some usage questions -


Cloudflare Workers look really cool :slight_smile:

Question 1

I want to use the script from CF that replaces some text, but I have two separate texts to replace.

So I need something like: worker becomes minion and slacker becomes slouch.

The script from CF that replaces one item is below, can you show me how I would change it to replace two?

Question 2

I also want to bypass the cache for my logged in wordpress users. I’ve had to really jump through some hoops to do that in the past - I’m very excited to be able to do that through CF Workers.

I see CF has a ready made script for that (also posted below).

Would I somehow put the scripts together as one? Or is it better or more efficient to have them as two separate workers? If together… can you show me how as well or am I asking for too much :slight_smile:

Thanks for any help you can provide!


//-----this is the replacement script---------//
// A Service Worker which replaces the word "Worker" with
// "Minion" in all site content.
addEventListener("fetch", event => {

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

  // Make sure we only modify text, not images.
  let type = response.headers.get("Content-Type") || ""
  if (!type.startsWith("text/")) {
    // Not text. Don't modify.
    return response

  // Read response body.
  let text = await response.text()

  // Modify it.
  let modified = text.replace(
      /Worker/g, "Minion")

  // Return modified response.
  return new Response(modified, {
    status: response.status,
    statusText: response.statusText,
    headers: response.headers

//------this is the bypass cache for logged in users script-----//

// A Service Worker which skips cache if the request contains
// a cookie.
addEventListener('fetch', event => {
  let request = event.request
  if (request.headers.has('Cookie')) {
    // Cookie present. Add Cache-Control: no-cache.
    let newHeaders = new Headers(request.headers)
    newHeaders.set('Cache-Control', 'no-cache')
    event.respondWith(fetch(request, {headers: newHeaders}))

  // Use default behavior.

I’m also experimenting with Workers.

  1. In playing with in the Playground, my search/replace doesn’t loop, so it only replaces the first instance. So I don’t know if your code will repeat. I have almost no experience with Javascript.
  2. Unless you’re Enterprise, you only get one script, as per the page below:

Hi @chris5,

Question 1

You could change:

let modified = text.replace(/Worker/g, "Minion")


let modified = text.replace(/Worker/g, "Minion").replace(/slacker/g, "slouch")

Question 2

I have some bad news here: The example script I wrote in my September blog post doesn’t actually work, because the Cloudflare cache does not honor the Cache-Control header. This is for security: you don’t want end users to be able to set a header to bypass your cache.

We plan to add an API for bypassing cache soon, similar to the flags described here: https://developers.cloudflare.com/workers/reference/Cloudflare-features/

On the bright side, we recently released a feature (separate from Workers) specifically to address your use case of bypassing cache for logged-in Wordpress users. See: https://support.cloudflare.com/hc/en-us/articles/236166048

KentonVarda, thank you, and you too Sdayman.

Re: bypassing cache for logged in WP users… is it the “Bypass Cache on Cookie” part you are referring too?

I’m a Pro user, so that might not apply to me. I do need a way to always show logged in users the fresh, dynamic, page that they get as subscribers, while displaying the cached version to non-logged in users.

I’ve got kinda a sloppy work-a-round script that adds a ?cache-n to the url if there is a cookie set, and a page rule that bypasses urls with that string, but adding that to the url doesn’t work pretty or consistently.

I was excited to see your script, shame it won’t work as a worker.

If you have any other solutions available at the Pro level please let me know. I’ve been keeping an eye out for a good solution for years. Tried to explain the need for it to Cloudflare a long time ago, but they didn’t seem to understand. It’s great to hear someone there (you) understands the concern :slight_smile: