views:

51

answers:

2

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");
+6  A: 

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

Claudio Redi
Not only is it not necessary, it is harmful because it will cause the javascript to be embedded in every HTML page making every HTML page download larger.
Erv Walter
@Claudio Redi could you show me how to do it in web.config ?
Omu
@Omu: which IIS version?
Claudio Redi
@Claudio Redi IIS 7.5 but my app is on some servers with IIS 6, so it's both
Omu
I'm afraid that you won't able to do it on web.config if IIS6 is used. Here you have a tutorial about how to enable cache on IIS5 but applies for IIS6 http://support.microsoft.com/kb/313561. IIS7 is similar http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache
Claudio Redi
@Claudio Redi there is explained how to set the expiration of the cache, is that means that the files are cached by default and there is no need to do this
Omu
The proper way to cache static files that won't change or change VERY infrequently is adding a long expiration header. It's little long to explain in 2 lines. Maybe you could check this cache tutorial http://www.mnot.net/cache_docs/#CONTROL. Just take into account that if you add a expiration header to your file, the only way to be sure that the client will get the new version is changing the file name.
Claudio Redi
+2  A: 

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.

Darin Dimitrov