6

I've recently been looking at the Safari performance audits/recommendations. One section refers to adding Cache-Control: public header to some png resources.

I've heard that said elsewhere and wondered how it can be done.

In fact I'm not even sure technically speaking what needs to be done to achieve this . Is it that this text is inserted into the image file somehow?

T9b
  • 3,312
  • 5
  • 31
  • 50

2 Answers2

3

That's an HTTP header; you need to configure your web-server to provide it. (HTML offers a way to specify HTTP-header-equivalents inside a document, but PNG does not.)

You haven't mentioned what web-server you're using, so it's hard to answer it in general, but for example, if you're using Apache, you can use the "header" directive in mod_headers: http://httpd.apache.org/docs/2.2/mod/mod_headers.html#header

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • I didn't mention the web-server because I didn't know it was implicated. As a matter of interest it's Apache2. – T9b Oct 06 '11 at 11:45
  • 1
    Does your answer mean that every file with the same type will have the header sent with it? i.e. it's not file specific? It seems odd that the recommendation in Safari pointed to specific file names (in Safari 5 - if you start "Auditing" it provides hints to improve files. – T9b Oct 06 '11 at 13:22
  • 1
    You can control that with or other directives (see [http://httpd.apache.org/docs/2.2/mod/core.html#files]). It's quite possible that you will actually want this for all *.png files -- you might as well do it for any *.png file that will never change, provided it's "public" (in that access to it is not restricted, say, to certain users). But if you want to set Cache-Control only for certain files, or only for files in certain directories, you can. – ruakh Oct 07 '11 at 00:00
2

Its not text included as part of an image. Its a HTTP header that precedes the image in the server's response.

This is typically configured in the web-server config (Can also be controlled through .htaccess if on apache). If you're using apache, I'd recommend reading about mod_expires.

jman
  • 11,334
  • 5
  • 39
  • 61
  • The Apache caching guide — though useful — is really about server-side caching (mod_cache and so on), so doesn't directly relate to Cache-Control. (I mean, Apache's server-side caching does _obey_ that header; but it doesn't _set_ it, which is what T9b is asking about.) – ruakh Oct 05 '11 at 23:52
  • @skjaidev please see my comments to `ruakh` below. – T9b Oct 06 '11 at 13:23