views:

59

answers:

1

Hello. I am working for an intranet application. Therefore I have some control on the client machines. The JavaScript library I am using is somewhat big in size. I would like to pre-install OR pre-load OR cache the JavaScript library on each machine (each browser as well) so that it does not travel for each request. I know that browsers do cache a JavaScript library for subsequent requests but I would like the library to be cached once for all subsequent requests, sessions and users.

What is the best mechanism to achieve this?

+3  A: 

You don't need access to the client machines for this. Best practices are all serverside:

  • GZip everything;
  • Minify all Javascript and CSS;
  • Minimize the number of external HTTP requests. Try to keep these to, say, 1-5 JS files, 1-5 CSS files and a few images. If you have lots of images you may want to use CSS spriting;
  • Version you images, CSS and Javascript; and
  • Use a far futures Expires header for all images, CSS and Javascript.

The last two points mean the content is cached until it changes. You can use an actual version number for official releases (eg jquery-1.4.2.min.js, the 1.4.2 is a version number). For code for your own application, often you'll use something like the mtime (last modified time) of the file as a query string. For example:

<script type="text/javascript" src="/js/code.js?1233454356"></script>

After the ? is generated from the modified time of the file. The client downloads it once and because of the far future Expires header it won't be downloaded again until the version number changes, which won't happen until the file changes.

cletus
Thanks. This helps.
Kabeer
@Kabeer you're welcome
cletus