21

I cannot get the image files to cache. I have tried everything that I have found on this site and others and still cannot get them to cache.

Web config setting that I have tried

    <staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
    </staticContent>
    <httpProtocol allowKeepAlive="true" />


    <caching enabled="true" enableKernelCache="true">
    <profiles>
    <add extension=".png" policy="CacheUntilChange" />
    <add extension=".jpg" policy="CacheForTimePeriod" duration="12:00:00" />
    </profiles>
    </caching>  

Here is the response headers for 1 of the images

    Key Value
    Response    HTTP/1.1 200 OK
    Cache-Control   no-cache
    Content-Type    image/png
    Last-Modified   Thu, 16 Dec 2004 18:33:28 GMT
    Accept-Ranges   bytes
    ETag    "a1ca4bc9de3c41:0"
    Server  Microsoft-IIS/7.5
    X-Powered-By    ASP.NET
    Date    Fri, 18 May 2012 13:21:21 GMT
    Content-Length  775
apaderno
  • 28,547
  • 16
  • 75
  • 90
user1167466
  • 333
  • 1
  • 4
  • 14
  • 1
    IIS caches static content by default. When you check the headers, ensure your dev tools don't have the option set to disable the cache. – ajbeaven May 27 '15 at 21:41

2 Answers2

36

The following should cause the browsers to cache your images:

<staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>
<httpProtocol>
    <customHeaders>
        <add name="Cache-Control" value="public" />
    </customHeaders>
</httpProtocol>

The <caching>...</caching> block is for server-side caching, not client side caching.

Marco Miltenburg
  • 6,123
  • 1
  • 29
  • 29
  • 2
    Does this enable client cache header on every static file (.js, .css, .jpg, etc)? How can I control the extension? Thanks – Andres May 21 '13 at 14:00
  • 3
    It enables the cache headers for all static content (that is enabled in IIS) in the folder (and sub folders) with a `web.config` file with this enabled. So if you your images are in `/images` you should make a `web.config` with the above and place that in the `/images` folders so only the images are affected by this. You can not enable/disable it per file extension. – Marco Miltenburg May 22 '13 at 10:59
  • 1
    @Digerkam A 500.19 error indicates a configuration error. Make sure web.config is valid XML file. Check for any missing open or closing tags and make sure the block is in `...`. – Marco Miltenburg Jan 20 '16 at 22:20
  • [this](http://www.galcho.com/blog/post/2008/02/27/IIS7-How-to-set-cache-control-for-static-content.aspx) site says `...` is client side , now who is right ? what you think ? – Shaiju T Feb 29 '16 at 12:22
  • @storm I believe there is some wrong information on the page you linked to. If you follow his steps the content is not (yet) cached on the server as he claims at the end of step 2. When you add the lines from step 3 then it is indeed **also** cached on the server (in user space). The `location="Client"` only adds a `Cache-Control: private` to the response to prevent it from being cached in proxy servers. Caching by browser is however triggered by the `max-age` in the `Cache-Control` header. – Marco Miltenburg Mar 03 '16 at 15:30
  • @MarcoMiltenburg : I've tried the way you have mentioned but when I am testing the site in pagespeed it is still giving me the same "Add Expires headers" in red. – DLV Feb 12 '18 at 05:52
  • @DLV : If true, that sounds like a bug to me. According to the HTTP 1.1 specification (section 14.21) the Cache-Control response header should override the Expires header. So if a Cache-Control header is present it should be happy. – Marco Miltenburg Mar 04 '18 at 16:52
  • I keep getting no-cache appended – Hussein Zawawi Feb 25 '20 at 03:43
1

In case anyone needs to configure your site as Chrome Audits or GTMetrix require I've configured my environments with the following (thanks to Marco's answer):

<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />

using 365 days and both tools took that value as acceptable for a cache time.

Facundo Colombier
  • 3,487
  • 1
  • 34
  • 40