bitten
bitten

Reputation: 2543

Caching all images on external site through Cloudflare

Here is my situation:

I have a webapp that uses a lot of images on a remote server. My webapp is behind Cloudflare, although the server that the images are hosted on are not.. and this server can be very slow. It can sometimes take about 5 seconds per image.

I would like to use Cloudflare to proxy requests to this external server, but also cache them indefinitely, or at least as long as possible. The images never change so I do not mind them having a long cache life.

Is this something I should set up in a worker? As a page rule? Or just not use CLoudflare in this way?

Upvotes: 0

Views: 1786

Answers (2)

T W
T W

Reputation: 6317

If you can't change origin server headers, you could try following snippet in your worker:

fetch(event.request, { cf: { cacheTtl: 300 } })

As per docs:

This option forces Cloudflare to cache the response for this request, regardless of what headers are seen on the response. This is equivalent to setting two page rules: “Edge Cache TTL” and “Cache Level” (to “Cache Everything”).

Upvotes: 3

Zack Bloom
Zack Bloom

Reputation: 8417

I think you generally just want a very long caching header on your images. Something like:

Cache-Control: public; max-age=31536000

Upvotes: 0

Related Questions