Adding custom-header to a cloudflare cache

I have an empty file on amazon s3, which I’m caching through cloudflare using a DNS CNAME record and page rules. Currently when I hit: speedtest-c0.waveform.com, this empty file gets downloaded, which is what I want.

I want to do a fetch on this file to figure out what is the latency, and I’m using ResourceTimingApi to find out the time to first byte ( ttfb ) but in order to do that I need to have the Timing-Allow-Origin: * header on the response. Amazon’s S3 doesn’t have support for that header though… so I was wondering if I could add that header through cloudflare caches.

I can see here that I can interact with the cache using a worker, but that seems to be for caching requests that are hitting the worker itself. How would I modify the cache key for a request that is not going to my “worker” and is going to a different location.

My worker: bufferbloat.waveform.com
My empty file sitting on S3: speedtest-c0.waveform.com

Does the caches API control the same cache that would get used for speedtest-c0.waveform.com?

You need to have a Worker in-between the S3 bucket and your user. This might be an useful example.

https://developers.cloudflare.com/workers/examples/alter-headers