开发者

How best to pre-install OR pre-load OR cache JavaScript library to optimize performance?

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 fo开发者_如何学Gor 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?


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.


Separate logic from execution on your JSs, then you can load scripts (logic) on the first user visit, then with a session/cookie you don't longer load those files, but they will be on cache, then, when you need the scripts just include them and they will lode from cache.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜