kotx / render

Cloudflare Worker to proxy and cache requests to R2
MIT License
384 stars 85 forks source link

Way to invalidate cache when object at R2 path changes? #34

Closed scottmas closed 1 year ago

scottmas commented 1 year ago

Hey there, this is a super well done library. Thank you!

I'm not sure if I'm missing something in your documentation or if it's just not possible, but how can I invalidate a url when the object at a R2 path changes? For example, when I'm updating the contents of my static website.

kotx commented 1 year ago

Theoretically modifying the object in R2 will update the last-modified header and/or the etag value, which will cause the user-agent/browser to refetch the object if they use the if-modified-since, if-none-match request headers. Are you seeing different results on your end?

Erisa commented 1 year ago

To add, if you're referring to cached assets the Cache API will take regular cache purges for one-off invalidations of zones or URLs: https://developers.cloudflare.com/cache/how-to/purge-cache/

Or you can disable cache entirely by setting CACHE_CONTROL to no-store in render, which will also immediately stop serving from cache due to special handling of that value.

scottmas commented 1 year ago

To be specific, I'm talking about the Cloudflare Cache API. Browser caching won't help us in this case. Nor should modifying the R2 objects since the Cloudflare Cache API will still think everything is unchanged unless I tell them about it. And the entire zone invalidation would work, but it's not as fine grained as I need. So I think the purge API is the best option like so

curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache" \
  -H "X-Auth-Email: <EMAIL>" \
  -H "X-Auth-Key: <API_KEY>" \
  -H "Content-Type: application/json" \
  --data '{"files":[{"url":"http://my.website.com/"}, {"url":"http://my.website.com/page_2"}]}'

I think the above is the answer. Or else appending some cache buster to the url I add to the cache API.

Thanks for all you do!