Minimizing CSS and JavaScript HTTP requests automatically on the CMS level

2011-02-16 13:45:32 GMT

Broadband Internet connections are pretty standard in the modern world nowadays, but despite of this many sites feel slow to load. Why is that? This is because of a multitude of reasons but the one that really hits home for me is excess HTTP requests. To be even more specific, I'd like to talk about excess CSS and JavaScript file requests. It's not unusual for sites to load about a dozen or more CSS and JavaScript files combined which I think is way overkill.

I came up with an algorithm that could be implemented by any CMS on the API level and it could dramatically reduce the load times of sites and relieve web servers significantly.

  1. Expose a dedicated API on the CMS level for plugins such as add_cached_css() / add_cached_js() .
  2. Execute the following points upon every page load:
  3. Check the modification times of all the CSS / JavaScript files.
  4. If the modification time of any file has been changed since the last page load or any new file has been added then go on, otherwise abort.
  5. Save the modification time of all the CSS / JavaScript files, concatenate the files and md5sum the concatenated files.
  6. Save the concatenated CSS / JavaScript files using their md5sums, such as 7c1735b79f2d13052454c196259ca511.css and 9fee0c4c4391bd75ca4269dac409a0aa.js
  7. Save the md5sums for the CMS to be able to reference the generated files from the main page.

A couple of things to note:

Let me know what you think.