kotx / render

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

Turn off cache? #15

Closed davej closed 2 years ago

davej commented 2 years ago

If an object in R2 is deleted then the cache still serves the old file. Even if I upload a new file at the same path then the old file is still served.

I suggest a way to either (a) opt-out of the cache completely, or (b) provide some way to manually invalidate the cache. Any ideas? Particularly ideas for (b)?

kotx commented 2 years ago

a), in wrangler.toml:

# The `cache-control` header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control.
# Optional, the `cache-control` header is omitted if unset, which would NOT disable caching: https://developers.cloudflare.com/workers/runtime-apis/cache/#headers
# For example, you can disable all cache by setting this to `no-store`.
CACHE_CONTROL = "max-age=86400"

So just set it like CACHE_CONTROL = "no-store"

b) You can manually invalidate the cache by going to the Cloudflare dashboard and purging the cache for URL(s) in a zone like so: image

davej commented 2 years ago

@kotx thanks! I think (b) is for the Cloudflare CDN cache and not the worker’s internal cache.

kotx commented 2 years ago

@kotx thanks! I think (b) is for the Cloudflare CDN cache and not the worker’s internal cache.

Are you sure the cache is not shared? The Cache API that the worker uses should allow at least purging by tag, host, or prefix. Purging by URL should also work, but I'm not sure on the wording. Take a look at the documentation for details.

davej commented 2 years ago

I'm not sure at all, to be honest, the docs suggested to me that they were different. If they are the same cache then they could probably be deleted programmatically using the Cloudflare API. Something like this:

await fetch(
  "https://api.cloudflare.com/client/v4/zones/aa7f1c06205063c0a3b298060d716e7f/purge_cache",
  {
    body: JSON.stringify({
      files: [pathToDelete],
    }),
    headers: {
      "Content-Type": "application/json",
      // auth headers
    },
    method: "POST",
  }
);