We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Browser cache when using minification

Hi.

i use asset manager to create collections of css and one collection for js files. When i use minification it works well, but i see that the minified files never gets loaded using the browser cache.

when i dont use minification it loads all the files leveragning browser cache. My question is how and if possible how to get browsers load the minified version of my collection fra internal browser cache.

Regards Sindre



17.5k

I think you need to build a minified file and load that file. If the filename is constant, the browser will cache it. I don't believe the browser caches raw script tags with minified code in it if that's what you're trying to do.

The filename of the minified file is always the same: js.js

The same goes for css minification.

Here is the code:

$this->assets
            ->collection('DefaultJS')
            ->addJs('js/full/jquery-2.2.4.js')
            ->addJs('js/full/jquery-ui.js')
            ->addJs('js/full/bootstrap.js')
            ->addJs('js/full/app.js')
            ->addJs('js/full/base.js')
            ->addJs('js/full/notification/SmartNotification.js')
            ->addJs('js/full/jquery.validate.js')
            ->setTargetPath('js/min/js.js')
            ->setTargetUri('js/min/js.js')
            ->join(true)
            ->addFilter(new \Phalcon\Assets\Filters\Jsmin());

If I understand you want the new minified files are loaded instead of the old without minification is it? If so, try this.

//In your service.php
$di->setShared('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_',

                //ADD THIS LINE BELOW
                'compileAlways' => true
            ));

            return $volt;
        },
        '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
    ));

    return $view;
});