Assets minification, regenerated every time?

Hey all,

Very happy with the direction Phalcon is taking. I've used it with success on a test internal tool, and we're about to use it on a major project.

With the recent asset minification fix (https://github.com/phalcon/cphalcon/issues/811), it appears the final file is generated every time the page loads. Is this supposed to happen, or am I missing anything? I know the Jsmin/Cssmin are fast as they are part of Phalcon core, but I am worried every landing will result in going through the join/filters.

Is there a way for Phalcon to distinguish between currently valid minified files and the ones that should be regenerated?

I had suggested an approach to this which automatically generates the minified file from a hash sum of the last modified time of all included files. That way, if any one of the files has a different "last modified" time, the minifier would know it needs to be recompiled, otherwise, it would just load the min file that's already there. The entire discussion is here: https://github.com/phalcon/cphalcon/issues/549

Essentially, this is how it would work:

$assets->addCss('file.css'); $assets->addCss('file2'.css);

Compiled file would be output file name + md5(filemtime('file.css').filemtime('file2.css'));, so something like 'minified_3q4un9028tb26782638794c238462.css'.

On next request, the file name is recalculated. If the filemtime is the same, the hash stays the same, and we can do a check like if (is_readable($outputFile)) { // echo this minified CSS file } however, if the filemtime is different, the hash is different, and the file automatically doesn't exist. This signals the minifier that a recompile is in order and it regenerates everything.

This ensures the minification is rerun only after every file change (in other words only when needed) and also makes sure your clients get the most up to date CSS and JS at all times, because the filename (the hash part) changes on every change and isn't stuck in cache - basically, this is like versioning. This is how I've had my own minifier for years and it's never let me down.

I've created a new issue for it here: https://github.com/phalcon/cphalcon/issues/925

Thanks Bruno, I had seen your comment in the ticket #549, but didn't see if it may have been implemented. Thanks for creating a new ticket, although wondering, is it similar to https://github.com/phalcon/cphalcon/issues/639 (from https://github.com/phalcon/cphalcon/issues/549#issuecomment-18561130 bullet point #3)?

Indeed. It doesn't seem like it was added, though, or I just can't find a more in-depth explanation on the topic.

Not every feature request is immediately added ;)



29.3k

So what's the current behaviour - is it regenerated every time or not?

Thanks!

It is regenerated every time as clearly indicated by the status "open" of the NFR request mentioned some posts earlier. It would, however, be a simple task to implement the desired behavior in PHP.



5.2k

My approach is much simpler. I only check if the file exists and I return the path. Doing hashes when having, say, 20 files is very punishing on the server cpu. I just attach to the filename the release version(which auto-updates itself) so I don't have to manualy erase the generated files(which is still a valid option though :) ) ```php <?php

namespace KyoLib\Assets;

use Phalcon\Assets\Manager as PhManager;

class Manager extends PhManager {

public function output($collection, $callback, $type=null){

    if( is_file($collection->getTargetPath())) {
        if ( $type == 'css' ) {
            echo \Phalcon\Tag::stylesheetLink( $collection->getTargetUri() );
        }
        if ( $type == 'js' ) {
            echo \Phalcon\Tag::javascriptInclude( $collection->getTargetUri() );
        }
    }else{
        parent::output($collection, $callback, $type);
    }
}

} ```



88

Is it still regenerated every time?