Omu
Omu

Reputation: 71188

asp.net-mvc: javascript caching

I'm thinking whether it makes sense in order to increase the speed of the website to do some strategy of caching the js files that are being included ?

one idea that I have is to do something like this:

[OutputCache(Location = OutputCacheLocation.Any, Duration = 3600)]
public JsController : Controller
public ActionResult JQuery()
{
//I would have a ascx with the entire jquery script inside
return View();
} 

and on the site.master:

<%=Html.RenderAction("JQuery","JsController");

Upvotes: 3

Views: 3177

Answers (3)

Vlad Bezden
Vlad Bezden

Reputation: 89499

I don't think you have to use OutputCache in order to cache content files. You can use config file:

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

This way web server will tell browser to cache static content (JS, CSS and images) and not to check for new content for 10 days.

Also by default any browser should cache static content. You can see all content that is cached in Chrome by typing in address bar chrome://cache

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

The client browser already caches included javascript files. For standard libraries such as jQuery you could use a CDN as chances are it is already cached in the client browser.

Upvotes: 3

Claudio Redi
Claudio Redi

Reputation: 68400

that's not necessary. You can specify cache strategy for JS (and any static files) on the web.config and on the IIS.

For jQuery in particular, you could reference the library from google CDN.

http://code.google.com/apis/ajaxlibs/documentation/#jquery

Upvotes: 7

Related Questions