3

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");
2
  • Any reason jQuery isn't just a static file? Commented May 25, 2010 at 13:04
  • @balpha no reason, I just wanted to use OutputCache attribute to cache it's conent, don't know whether it makes sense Commented May 25, 2010 at 13:16

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@Claudio Redi IIS 7.5 but my app is on some servers with IIS 6, so it's both
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 support.microsoft.com/kb/313561. IIS7 is similar iis.net/ConfigReference/system.webServer/staticContent/…
@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
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 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.
3

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.