Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggressive JavaScript caching

I've run into a problem where I make changes to a few JavaScript files that are referenced in an HTML file, but the browser doesn't see the changes. It holds onto the copy cached in the browser, even though the web server has a newer version.

Not until I force the browser to clear the cache do I see the changes.

Is this a web-server configuration? Do I need to set my JavaScript files to never cache? I've seen some interesting techniques in the Google Web Toolkit where they actually create a new JavaScript file name any time an update is made. I believe this is to prevent proxies and browsers from keeping old versions of the JavaScript files with the same names.

Is there a list of best practices somewhere?

like image 988
Brian Kelly Avatar asked Sep 10 '08 15:09

Brian Kelly


People also ask

What is aggressive cache?

time to read 2 min | 360 words. RavenDB's aggressive caching allows RavenDB Clients to skip going to the server and serve requests directly off the client cache. That means that you can answer queries very quickly, because you never even have to leave your process memory space.

Can JavaScript be cached?

In general, most modern browsers will cache JavaScript files. This is standard practice for modern browsers and ensures an optimized loading experience. Cached assets such as JavaScript will typically be served from the browser's cache instead of making another request for a resource that has already been retrieved.

How do you stop JavaScript from caching?

Answers. You can't prevent IE or for that matter any browser from caching Java Script file, you can only direct browser to always fetch latest version of Javascript file from the web server using the solution provided above. Tools->Internet Options. general tab->Settings and select the option "Every time ..."

What is JavaScript caching?

Code caching (also known as bytecode caching) is an important optimization in browsers. It reduces the start-up time of commonly visited websites by caching the result of parsing + compilation. Most popular browsers implement some form of code caching, and Chrome is no exception.


3 Answers

We append a product build number to the end of all Javascript (and CSS etc.) like so:

<script src="MyScript.js?4.0.8243">

Browsers ignore everything after the question mark but upgrades cause a new URL which means cache-reload.

This has the additional benefit that you can set HTTP headers that mean "never cache!"

like image 95
Jason Cohen Avatar answered Sep 24 '22 02:09

Jason Cohen


It holds onto the copy cached in the browser, even though the web server has a newer version.

This is probably because the HTTP Expires / Cache-Control headers are set.

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

I wrote about this here:

http://www.codinghorror.com/blog/archives/000932.html

This isn't bad advice, per se, but it can cause huge problems if you get it wrong. In Microsoft's IIS, for example, the Expires header is always turned off by default, probably for that very reason. By setting an Expires header on HTTP resources, you're telling the client to never check for new versions of that resource -- at least not until the expiration date on the Expires header. When I say never, I mean it -- the browser won't even ask for a new version; it'll just assume its cached version is good to go until the client clears the cache, or the cache reaches the expiration date. Yahoo notes that they change the filename of these resources when they need them refreshed.

All you're really saving here is the cost of the client pinging the server for a new version and getting a 304 not modified header back in the common case that the resource hasn't changed. That's not much overhead.. unless you're Yahoo. Sure, if you have a set of images or scripts that almost never change, definitely exploit client caching and turn on the Cache-Control header. Caching is critical to browser performance; every web developer should have a deep understanding of how HTTP caching works. But only use it in a surgical, limited way for those specific folders or files that can benefit. For anything else, the risk outweighs the benefit. It's certainly not something you want turned on as a blanket default for your entire website.. unless you like changing filenames every time the content changes.

like image 23
Jeff Atwood Avatar answered Sep 26 '22 02:09

Jeff Atwood


@Jason and Darren

IE6 treats anything with a query string as uncacheable. You should find another way to get the version number into the url, such as a fake directory:

<script src="/js/version/MyScript.js"/>

and just remove that first directory level after js on the server side before fulfilling the request.

EDIT: Sorry all; it is Squid, not IE6, that won't cache with a query string. More info here.

like image 44
Chris Marasti-Georg Avatar answered Sep 27 '22 02:09

Chris Marasti-Georg