[Solar-talk] Squished JS and CSS
Rodrigo Moraes
rodrigo.moraes at gmail.com
Sat Dec 23 10:36:51 PST 2006
Some new techniques for Javascript and CSS squishing have been poping
around lately:
http://www.ejeliot.com/blog/72 [1]
http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
http://bestpractical.typepad.com/worst_impractical/2006/09/smart_caching_f.html
I was thinking and playing with a version to work nicely with Solar.
These are some thoughts.
1. First, I need a helper to generate and cache file hashes for the
scripts / styles that would be worth to be combined. A group of
scripts / styles would be called as:
<script type="text/javascript" src="/js/scripts_{hash}.js">
<link rel="stylesheet" href="/css/styles_{hash}.css" type="text/css" />
2. Following the ideas on [1], I created some rewrite rules to
redirect the calls to a bootstrap:
RewriteRule js/scripts_([a-zA-Z0-9]+).js
combine.php?type=javascript&hash=$1 [L]
RewriteRule css/styles_([a-zA-Z0-9]+).js combine.php?type=css&hash=$1 [L]
3. combine.php is a bootstrap that starts a mini-solar instance just
to execute Tipos_Compressor.php. It reads the hash and checks if the
files are cached, and if not it:
a) combine the files into one
b) compresses them (optional, you can hook something like JsMin to it)
c) gzips the contents
d) cache the contents
e) sets etag and encoding headers and serves the squished results
4) The next time the same request is done, Tipos_Compressor.php just
serves the squished results. The whole thing can be turned on/off for
production/development.
Gzipped javascript/css is easy to achive with mod_gzip or mod_deflate
(I have just turned mod_deflate on on my server to serve php and
javascript, and content-lenghts are now 40% smaller). The idea above
includes the gzip utility and also has benefit to serve only one file,
be hable to group them and hook a compressor to be used automatically
when files are changed. Too much for such a simple thing? Any
thoughts?
rodrigo
More information about the solar-talk
mailing list