Worker routing from dynamic subdomain to cached subdomain

workers

#1

hello sir.

I have 1 subdomains using cloudflare with orange-cloud and all image already cache by cloudflare on this subdomain, example:
privatecdn.example.com

I create 3 subdomains using cloudflare with orange-cloud and this is new subdomain don’t have cache on cloudflare, example:
cdn1.example.com
cdn2.example.com
cdn3.example.com

on the 3 new subdomain it has a url that can access an image, and path url is same on privatecdn, example :
cdn1.example.com/1234567890.png => not yet cached on coudflare.
privatecdn.example.com/1234567890.png => already cached on cloudflare.

this is my worker code:

addEventListener('fetch', event => {
  event.respondWith(fetchAndApply(event.request))
})
async function fetchAndApply(request) {
  let privatecdn= "https://privatecdn.example.com";
  var accessUrl = request.url.replace(/https?:\/\/cdn[0-9]\.example\.com/, privatecdn);
  return fetch(accessUrl, request);
}

which I want to ask, when the worker requests to “privatecdn” whether to retrieve data from the edge cache or retrieve data from the origin server?

@KentonVarda or @ryan please help me.


#2

Hi @kputera410,

Requests you make with fetch() always go through cache. So your code will check for the file in edge cache before falling back to origin.