27

I'm trying to get YSlow to give me an A on the "Add Expires header" section by setting the web.config file.

I've been looking around and this is what I put in based on what's out there:

<staticContent>
    <clientCache httpExpires="15.00:00:00" cacheControlMode="UseExpires"/>
    </staticContent>
</system.webServer>

This is what I'm seeing in Firebug:

Response Headers

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Sun, 28 Aug 2011 13:54:50 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: image/jpeg
Content-Length: 24255
Connection: Close


Request Headersview source
Host    localhost:50715
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0
Accept  image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection  keep-alive
Referer http://localhost:50715/MySite/SiteHome.html
Pragma  no-cache
Cache-Control   no-cache

However, when I look at it in Firefox, Yslow is still giving an F on this, even after a Crtl-F5

What am I missing?

Thanks.

3
  • Are you using the built-in web server in Visual Studio (it looks like it from your headers)? Have you tried this in IIS7 or IIS Express?
    – patmortech
    Aug 28, 2011 at 15:14
  • yes, I'm using the built-in VS server, haven't tried in IIS. Do you think this is where the problem comes from?
    – frenchie
    Aug 28, 2011 at 15:30
  • yes. I don't believe the built-in server will use those settings you mention above.
    – patmortech
    Aug 28, 2011 at 15:40

3 Answers 3

35

From .NET Daily, I successfully applied this to a PHP site on IIS. It sets the max age to 30 days from now, rather than having to specify an explicit date.

Add this to your web.config file:

<system.webServer>
  <staticContent>
    <clientCache cacheControlMaxAge="30.00:00:00" cacheControlMode="UseMaxAge"/>
  </staticContent>
</system.webServer>

This configuration satisfies both PageSpeed "Leverage browser caching" and YSlow "Add Expires headers". YSlow requires a value greater than 7 days. PageSpeed requires between 30 days and 1 year.

1
  • Hi. I want to ask. If I put the script in my web config. How long will it take for the process to get grade A? And whether the script is still valid now?
    – moses toh
    Sep 16, 2019 at 8:42
19

From the clientCache documentation

The value for the httpExpires attribute must be a fully-formatted date and time that follows the specification in RFC 1123. For example: Fri, 01 Jan 2010 12:00:00 GMT

So, if you want to use the http expires headers for your static content, set it like this:

<staticContent>
  <clientCache cacheControlMode="UseExpires" httpExpires="Sun, 1 Jan 2017 00:00:00 UTC" />
</staticContent>

Update (to above comments): This will most probably still not work in the built in VS server. I'm not sure if it supports expires headers at all. AFAIK this is an IIS setting.

2

I believe the recommendation is to add expires on static content rather than ASPX pages. Make sure you are checking the request headers for static content such as images and not the ASPX file.

Check out :

http://developer.yahoo.com/performance/rules.html

1
  • 1
    Yes, that's what I'm looking to do, should have been more clear. It's indeed caching for the static content (pictures, css and js files) that I'm trying to get to work; not the entire aspx page.
    – frenchie
    Aug 28, 2011 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.