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 (, 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:

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:

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 (from 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 ;)


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


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.


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 :) )


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() );
            parent::output($collection, $callback, $type);


Is it still regenerated every time?


Can you please describe how do you use this method? Where do you call it? I have $this->assets->outputCss() in the view... Sorry for the dummy question. I am new to Phalcon. Thank you

Issue will hardly get fixed because if you use filemtime to check for modification everytime, every file, it will add an extra overhead to the php script. Slowly, automated things will render Phalcon slower and slower.

One approach I use is to check for cache existence, if it doesn't exist generate it. It is good for development. But in production, best way is to use a cache builder shell script and bind it to a git hook. This will kill as much overhead as possible.


use Phalcon\Mvc\Controller;
use Phalcon\Assets\Filters\Jsmin;

class ControllerBase extends Controller {

//    public function onConstruct() {
//    }
    // Executed after every found action
    public function afterExecuteRoute($dispatcher) {
        $asset = $this->assets

        //if file exist in cache serve it directly
        if (ENV_PROD && is_file('assets/all.min.js'))
        else {
            //order matter
                    ->addFilter(new Jsmin());

        //autodetect controller specific script
        $controller = $this->dispatcher->getControllerName();

        $asset = $this->assets->collection('controller')

        if (ENV_PROD && is_file("assets/c.$controller.min.js"))
        else {
                    ->addFilter(new Jsmin());

        //autodetect action specific script
        $action = $this->dispatcher->getActionName();

        $asset = $this->assets->collection('action')

        if (ENV_PROD && is_file("assets/$controller/a.$action.min.js"))
        else {
            //create dir if not exist
            if (!is_dir("assets/$controller")) {
                chmod("assets/$controller", 0770);

                    ->addFilter(new Jsmin());