5

We have a fairly high-volume IIS7.0 site (about 1 million requests a day), a lot of which are Images/CSS/JS.

As a quick way to reduce this, I'm considering setting the common http response header to expire web content some fixed date way in the future, and an wondering what possible disadvantages we could get with this.

Looking at the web.config change resulting from setting this, it adds:

<staticContent>
    <clientCache cacheControlMode="UseExpires" cacheControlMaxAge="1.00:00:00" httpExpires="Thu, 01 Oct 2020 00:00:00 GMT" />
</staticContent>

My worry is that if we wanted to change one of the CSS/JS/Images after setting this, clients wouldn't pick up the changes and whilst images probably won't change, CSS/JS certainly will. Does that mean we should only set this on for folders containing only images? Or does this mean we need to introduce versioned URLs for our CSS/JS?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Kram
  • 4,099
  • 4
  • 39
  • 60

1 Answers1

7

You should always cache static content (Images, CSS, JS)

Even when cached, most browsers will still politely inquire the server as to whether a newer version is available (If-Modified-Since) and the server will reply with a Not Modified. (e.g. IE defaults to "automatically check for new versions of pages")

One caveat : if there are other proxies in between your server and clients, then these proxies may elect strictly to adhere to your cache settings, and could serve 'stale' content, so you will need to apply some thought to the optimal duration of caching.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Thanks. So if modern browsers ask anyway, then is there actually any benefit to setting static content to expire way in the future? All I can see is the possible disadvantage that you point out over the proxy. – Kram Oct 10 '11 at 15:35
  • AFAIK even if the cache time has expired, the server will know whether or not the content has changed (since the browsers If-Modified-Since) and would still return Not Modified and save bandwidth by not serving the content, so you should get most of the benefit by just caching for a few hours. But use a tool like Fiddler just to be on the safe side. – StuartLC Oct 10 '11 at 15:42
  • Checking on Chrome and IE9, they both seem to work the same with caching turned on or off. Perhaps it's relevant to older browsers/proxies, but I can't see that it makes any difference for modern browsers. Guess I could turn it and and "see", but I'd hoped for something a bit more clear-cut than than. – Kram Oct 10 '11 at 16:09
  • On IE under Tools | Internet Options | General | Browsing history | Settings | Check for newer versions of stored pages, turn this to 'Never' and IE should then obey your server cache settings religiously (won't check for new versions until the expiry date). You might need to empty your cache out first. – StuartLC Oct 10 '11 at 17:24
  • 1
    Thanks... however, whilst this may be true, it's not relevant to our site as 99.9% of people will have the defaults. Gotta say, I'm inclined not to enable static caching, as I struggle to see any benefit and only possible problems. – Kram Oct 10 '11 at 18:20
  • How to do it via IIS. https://blogs.msdn.microsoft.com/rakkimk/2007/07/09/iis7-how-to-enable-content-expiration/ – Tyler S. Loeper Apr 05 '18 at 16:57