cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Make assets manager recompile files only if needed

Open Swader opened this issue 13 years ago • 15 comments

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.

Swader avatar Jul 25 '13 08:07 Swader

+1

iforp avatar Jul 25 '13 14:07 iforp

absolutely, totally +1

temuri416 avatar Jul 26 '13 18:07 temuri416

Example implementation on my own Assets Manager (compatible with Phalcon's Cssmin and Jsmin): http://forum.phalconphp.com/discussion/707/alternative-assets-manager

Swader avatar Aug 04 '13 21:08 Swader

Thanks, noted

ghost avatar Aug 04 '13 23:08 ghost

cool, good job...

fishjerky avatar Aug 05 '13 14:08 fishjerky

+1

philippgerard avatar Mar 25 '14 11:03 philippgerard

+1

seb777 avatar Mar 26 '14 09:03 seb777

+1

ghost avatar Jan 14 '16 07:01 ghost

+1

lubberscorrado avatar Feb 09 '16 13:02 lubberscorrado

+1

Graphmatic avatar Jun 02 '16 20:06 Graphmatic

good idea +1

mzf avatar Jun 03 '16 11:06 mzf

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.

Swader avatar Jun 03 '16 11:06 Swader

+1

fenixphp avatar Jul 02 '16 13:07 fenixphp

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.

FaimMedia avatar Sep 01 '16 14:09 FaimMedia

Closing in favor of https://github.com/phalcon/cphalcon/issues/1756

sergeyklay avatar Oct 27 '17 12:10 sergeyklay