May I ask if you are going to use a custom domain, or rather sub-domain, or not at all for the particular R2 bucket?
Connect the custom domain to your R2 bucket, then add the missing Access-Control-Allow-Origin HTTP header by using Transform Rules → Modify Response Header as follows:
Note the first two properties. Allow-Headers: Content-Type is needed for the frontend to send JSON on the response. And allowed methods are everything allowed + OPTIONS for the preflight request.
Then I look at the ‘Origin’ header and make sure it ends with the main domain. This way any subdomain works. I then set Access-Control-Allow-Origin to that origin:
if (!isNil(origin) && endsWith(origin, "ethang.dev")) {
store.setOrigin(origin); // Set header that will be on **Response**
}
When the header is OPTIONS, I send back a OK response with a null body. This is for the CORS check.
After that all responses add the above headers with the origin of the original request. The full updated code is in the links in the original post.