19

I just ran Google's Page Speed application against our site and one of the recommendations was to Leverage browser caching. Expanding this revealed the following:

The following cacheable resources have a short freshness lifetime: Specify an expiration at least one week in the future for the following resources:

<a long list of images >
<some javascript files >

How do I go about lengthening the "freshness lifetime" of particular images?

It's an ASP.NET project running on IIS7.5

madth3
  • 7,275
  • 12
  • 50
  • 74
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289

3 Answers3

50

I found the answer to my question elsewhere on this site. Woot! (Not sure why it didn't appear when I first posted this, but never mind, I got there in the end.)

For those interested, the answer was this (as posted by Gabriel McAdams):


You do that in IIS. If you are using IIS 7, you can add the header in your web.config. It’s in the system.webServer section.

<staticContent>
    <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
</staticContent>

This will cause all static content to have an expires HTTP header set to the year 2020. Static content means anything that isn’t served through the ASP.NET engine such as images, script files and styles sheets.

Or to use a relative expiration, use this:

<staticContent>
    <clientCache cacheControlMaxAge ="2.00:00:00" cacheControlMode="UseMaxAge" />
</staticContent>

This will cause all static content to have an expires HTTP header set to 2 days.

Community
  • 1
  • 1
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
  • 3
    Our site speed just increased by about 400%. Huzzah! – Chuck Le Butt Jun 11 '10 at 10:51
  • Did i gave you a different answer than this? The configuration that i have written does the same exact thing. And moreover your answer was specific to particular images. – sushil bharwani Jun 11 '10 at 17:10
  • 9
    Um, what? 1. You didn't explain anything, you just posted someone else's answer from another question. 2. You didn't explain what content would be affected, even when I specifically asked. Edit: Looks like you just added that. 3. Setting a date in the future is not a helpful solution my situation. I can't believe you marked my answer down. That is incredibly lame. But whatever. – Chuck Le Butt Jun 11 '10 at 20:56
4

You will have to add Expires Header to your Static content including images, html, js, css files. You can easily add the expires header in your web.config’s system.webServer section using IIS7:

<staticContent>
    <clientCache httpExpires="Mon, 1 May 2020 05:00:00 GMT" cacheControlMode="UseExpires" />
</staticContent>
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
  • Unfortunately your answer has left me bewildered :( Will this affect ALL content we deliver to the user? How do we specify particular images, as I asked in my question? (Although I guess it *might* be useful to get the browser to cache everything...) – Chuck Le Butt Jun 10 '10 at 18:58
  • Wish I could have given you an answer on this sorry for my limitations on ASP and IIS platform. I know there is .htaccess file for this on apache. But anyways why do you just want few of your images to be cached and not all of them. What difference does that makes. – sushil bharwani Jun 10 '10 at 19:24
  • I'm afraid you didn't answer my question :( Will this effect ALL the content we deliver to the user? Thanks. – Chuck Le Butt Jun 11 '10 at 09:33
2

What you typically want is to cache all assets (css,js and images), the html file will have çache busting links so that you can still update the images.

In order for this to work, the html files cannot be part of staticContent. adding a new handler for html files prevents them from being permanently cached.

    <staticContent>
        <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
    </staticContent>
    <handlers>
      <add
        name="HtmlHandler"
        path="*.html"
        verb="*"
        type="System.Web.Handlers"
        preCondition="integratedMode"
      />
    </handlers>
Miss.Vy
  • 151
  • 7