Cranio
Cranio

Reputation: 9847

Serving CSS and JS in single files, possible advantanges and drawbacks

I noticed some time ago that many big sites sometimes serve their CSS and JS files in a surprisingly small number (even one single file) and with strange alphanumeric names. What I thought was that these filenames were hashes, which in turn lead me to suspect that:

Some internet research showed me that I was right and this approach is somewhat existent and valued, and I would like to implement it in a WAMP/LAMP site I'm developing - a site that, for its modular plugin structure, serves often a lot of tiny and medium-sized CSS and JS files.


These are my questions:

  1. how can I update the cache automatically on file update? The solution I came up with was to create a sort of "signature string" for every page, joining the file paths AND the last modification time, then hash the signature with MD5 and feed it to the cache to determine if the hash/filename is existent. Is it a correct approach? any drawbacks?
  2. the next logical step would be minification. Minifying CSS files is rather simple; is JS minification reliable too? Any good class out on the internet?
  3. another problem: what about already minified scripts? The only solution aside some form of parsing I thought of was to compare size before and after minification, if size after < size before then use the PHP-minified form. Computational overhead should be solved by caching, right?
  4. The most important issue: CSS images. Here I really don't have a clue: collecting various CSS in different places breaks all the relative image links. I cannot use absolute paths (break site relocability). I'd prefer not using a standard directory structure for CSS images (f.ex. placing all CSS referenced images under a css/img dir) because of potential filename conflicts. This would mean also parsing the CSS in some way. How can I solve this?
  5. Compression: do I have to implement always a software PHP based GZIP compression, or (very ignorant in that field) can it be enforced automatically on an Apache server?

Thank you for your attention.

Upvotes: 0

Views: 182

Answers (1)

Dmytro Kadyrov
Dmytro Kadyrov

Reputation: 26

I using is a very good library https://github.com/mrclay/minify for this. Its library automaticaly minify css and js files into one file and rebuild cache on modification files.

Upvotes: 1

Related Questions