1

I am serving Javascript vai a controller method that looks like this:

  [OutputCache(Duration = 120)]
  [Compress]
  public ActionResult JavascriptFile(String scriptName) {
     string content;
     if (HttpContext.IsDebuggingEnabled) {
        content = ReadApplicationScript(string.Format("~/scripts/{0}", scriptName));
        return Content(content, "application/javascript");
     }
     else {
        content = ReadApplicationScript(string.Format("~/scripts/Built/{0}", scriptName));
        return Content(content, "application/javascript");
     }
  }

The Compress attribute is from here.

When I run ySlow I get an F grade on "Add Expires headers". What can I do to add these?

Community
  • 1
  • 1
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228

1 Answers1

2

Add the following inside system.webServer section

<staticContent>
      <clientCache cacheControlMode="UseExpires"
         httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" />
    </staticContent>

Here the httpExpires value will be the expiration date.

Edit

You can also try adding the content to cache like this:

var cacheName = "someName";
var value = HttpRuntime.Cache.Get(cacheName) as ContentResult;

            if (value == null)
            {                
                var contentResult = ReadApplicationScript(string.Format("~/scripts/{0}",scriptName));
                System.Web.HttpContext.Current.Cache.Insert(cacheName, contentResult );
                return contentResult;
            }

            return value;
Prasanth
  • 3,029
  • 31
  • 44
  • I have this tag there already, I think the issue is that because I am serving the scripts dynamically it is not static content so this does not apply. – Aran Mulholland Oct 11 '12 at 04:54
  • Please try adding the content to cache as given. I didn't test the code. – Prasanth Oct 11 '12 at 05:28
  • @DhavalPanchal , you can use the first block of code in Web.config, under system.webServer section. If you are working with MVC, you can create a controller action to return a content result using second block of code. Hope this helps. – Prasanth Jan 06 '14 at 13:17
  • I'm using ASP.Net and I have already added the block in web.config. It is working fine for static content of my website but not for the reference added from third party script like my domain is: www.abc.com and the script reference is loading from www.xyz.com – Dhaval Panchal Jan 06 '14 at 13:25