skip to Main Content

I’m looking to set a cache control policy on some of the images on my website to appease the Google lords, however I don’t want all the images on my website to be cached. While I’m a bit fuzzy on how the Cache control policy works, my understanding is that I place it in the header of my page, and all the images, JS, and CSS on the page gets cached for the set amount of time specified.

But if I set my cache control policy to:

Cache-Control: Max-age=31536000

How do I keep a specific asset or assets from being cached for that years time. I have banners and images on my page that are updated every so often that I can’t have cached and left static for a years time.

2

Answers


  1. While I’m a bit fuzzy on how the Cache control policy works, my understanding is that I place it in the header of my page, and all the images, js, and css on the page gets cached for the set amount of time specified.

    No, cache headers only apply to the specific response being returned, not any other resources linked to by the page. All the other assets need their own cache headers, which of course allows you to choose a specific cache time for each resource.

    Login or Signup to reply.
  2. Why don’t you want to cache images? Images at static objects; if you’re creating different images then they should have different names, or extension types.

    With that being said, if you’re using a a CDN, most allow for configuration of custom TTLs on resources. I’d recommend reaching out to CDN support to figure out how to set up rule.

    Also, most common applications (Rails, Express, etc) and Web Servers (Nginx, Apache) allow for overriding the headers as well. Server side configuration may be more flexible then the CDN option since you control the logic and not dependent on a third party controlling it.

    Here’s a link to Mozilla’s spec on the cache-control header

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search