How to set Cache-Control for images?


All the images served by Grav have “Cache-Control: no-cache” on them. This gives us a bad performance rating. The configuration documentation suggests you could set the cache_control setting, but I’ve tried cache_control: 'private, max-age=31536000' but that doesn’t do anything. I actually doubt if that is still a valid configuration parameter, since it was not included by default in my system.yaml.

How do I set the cache control instructions correctly? And should I do that in Grav at all or in Nginx?



Pragma: no-cache

In my system.yaml there is a section for this:

  default_image_quality: 85
  cache_all: true
  cache_perms: '0755'
  debug: true
  auto_fix_orientation: true

Is it in yours?


Yes, it is, but that does not affect cache-control settings. In addition, cache-control settings do not only apply to images, but also to other assets, like JS and CSS files. So there should be a way to cover this as well. It’s unfortunate and strange that this isn’t documented anywhere, since this is very important for performance and performance rankings.


I’ve decided to try and see if NGINX could be used to set the proper cache-control. I ran into this sample NGINX config file and used the rules for assets, images and fonts to add cache-control headers to the responses. That works! I still would like to know if this is the preferred way of doing this with Grav (and NGINX). And if so, I think it would be very valuable is this was added to the documentation somewhere.