[NFR] Make assets manager recompile files only if needed
Currently, the assets manager recompiles all the added assets on every request. This might get inefficient with a lot of resources.
I suggest the following approach which I've personally been using for a while now in my own asset manager:
Request 1: Read filemtime of all files. Generate hash from all these times. Append hash to output file name. Check if file already exists. It doesn't. Compress and minify, serve file.
Request 2 (if no files have changed): Read filemtime of all files. Generate hash from times, append to output file name. Check if file exists. It does - no recompile needed. Output existing file.
Request 2 (some changes were done - either new file added, or file changed): Read filemtime of all files. Append times hash to output file. Check if file exists - it doesn't because hash is different. Recompile and serve file.
Aside from eliminating overhead from recompiling on every request, this also has the side benefit of making sure your clients ALWAYS have the most up to date JS and CSS loaded, because when changes occur the file name is different and cannot be stuck in the browser's cache.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
+1
absolutely, totally +1
Example implementation on my own Assets Manager (compatible with Phalcon's Cssmin and Jsmin): http://forum.phalconphp.com/discussion/707/alternative-assets-manager
Thanks, noted
cool, good job...
+1
+1
+1
+1
+1
good idea +1
For the interested, I've additionally implemented this outside of Phalcon waters at nofw - just a combo of the Robo task runner, Mini-asset for compiling, and that's about it.
+1
I agree (partially) with this setup, however I think a better solution would be to get the highest filemtime from all sources, append this to 'cache' file and check from there. Hashing would be less efficient and (personally) I find a url with a timestamp cleaner then a hash.
Closing in favor of https://github.com/phalcon/cphalcon/issues/1756