For Workes & Pages, what is the name of the domain?
sh-cdn.co.uk
What is the issue or error you’re encountering
Hi, We have a cloudflare worker which does some caching based on urls. The worker performs a HTTP Head request to an azure storage account to validate an azure SAS token. If the token is valid then we cache the response from the storage account if the url matches one we want to cache. The cloudflare analytics show we have a high cache percentage however I can see in the azure storage analytics that the egress from the storage account matches the data served analytic in cloudflare which is not what I would expect. Going off the cloudflare cache percentage I’d expect my azure storage account to have very low egress but this is not the case.
Here is a rough copy of my worker code:
export default {
async fetch(request, env, ctx) {
const cacheUrl = new URL(request.url);
const response = await fetch(request.url, { method: 'HEAD' })
async function gatherResponse(response) {
return response.status;
}
const result = await gatherResponse(response);
if (result != "200") {
return new Response("Unauthorised", { status: 401 });
}
const urlWithoutQueryParams = request.url.split('?sv=')[0]
const cacheKey = new Request(urlWithoutQueryParams, request);
console.log(
`Cache Key: ${urlWithoutQueryParams}`
);
const cache = caches.default;
if (urlWithoutQueryParams.includes('redacted')) {
console.log('redacted file so ignoring cache')
return await fetch(request.url, { cf: { cacheTtl: 0 } });
}
else {
let cResponse = await cache.match(cacheKey);
if (!cResponse) {
console.log(
`Response for request url: ${urlWithoutQueryParams} not present in cache. Fetching and caching request.`
);
// If not in cache, get it from origin
cResponse = await fetch(request.url);
// Must use Response constructor to inherit all of response's fields
cResponse = new Response(cResponse.body, cResponse);
// Any changes made to the response here will be reflected in the cached value
cResponse.headers.append("Cache-Control", "s-maxage=86400");
ctx.waitUntil(cache.put(cacheKey, cResponse.clone()));
} else {
console.log(`Cache hit for: ${urlWithoutQueryParams}.`);
}
return cResponse;
}
}
}
More research has led me to believe this may not be working as my worker didn’t have a custom domain added. I’ve now added a custom domain to my worker, am I right in believing the caching should behave correctly now? Thanks