Rename dump from AsseticBundle

3 minute read

In your main template you probably should have assetic to manage your assets. In the production environment you would like to dump this to static files.

//base.html.twig


\{\% javascripts
 'bower/jquery/jquery.min.js'
 '@AcmeDemoBundle/Resources/public/js/*'
 '@AcmeBlogBundle/Resources/public/js/*'
\%\}
 <script type="text/javascript" src=""></script>
 \{\% endjavascripts \%\}

To dump the assets you run the following command:

php app/console assetic:dump --env=prod --no-debug

This will generate a file like root/web/js/53ad2515.js. This is awesome. This file should be cached with very generous expiration date. This file should also be handled by your CDN.

The problem is when you want to deploy a new version of your application, with lots of changes in your javascripts. The assetic:dump command will generate the root/web/js/53ad2515.js again which leads to that your returning visitor does not get the latest version of the file.

Symfony's solution

The solution suggested by the Symfony2 docs is that you use assets_version. You should update that config every time you made some changes in the assets. But that gives a new version of all the assets, including images, and forces my users to download them again.

My solution

I would like my users to download some assets every time I deploy. I start with specifying an output in my main template.

//base.html.twig


\{\% javascripts output='js/scripts.{version}.js'
'bower/jquery/jquery.min.js'
'@AcmeDemoBundle/Resources/public/js/*'
'@AcmeBlogBundle/Resources/public/js/*'
 vars=["version"]
\%\}
<script type="text/javascript" src=""></script>
\{\% endjavascripts \%\}

Since Assetic needs to precompile the assets it needs to know what the available values version may have. We just need one version.

# app/config/config.yml
# Assetic Configuration
assetic:
  variables:
    version: [%git_commit%]

I use the parameter %git_commit%. I want this parameter to change every time I deploy. Create a new file called app/config/version.yml and make sure to include it in app/config/config.yml.

# app/config/config.yml
imports:
  - { resource: parameters.yml }
  - { resource: version.yml }
# app/config/version.yml
parameters:
 git_commit: 'sha1'

You just have to do one more thing before this will work. You have to “give” that variable value to Assetic. You have to override the AsseticBundle\DefaultValueSupplier with a class of your own.

#AcmeDemoBundle/Resources/config/services.yml
parameters:
  assetic.value_supplier.class: Acme\DemoBundle\Assetic\CustomValueSupplier
<?php
// src/Acme/DemoBundle/Assetic/CustomValueSupplier.php
namespace Acme\DemoBundle\Assetic;


use Symfony\Bundle\AsseticBundle\DefaultValueSupplier;


class CustomValueSupplier extends DefaultValueSupplier
{
    /**
     * Get values for Assetic
     *
     * @return array
     */
    public function getValues()
    {
        //get the default values
        $values = parent::getValues();


        //get the git version as version
        $values['version']=$this->container->getParameter('git_commit');


        return $values;
    }
}

If you run assetic:dump now you will get a file called root/web/js/script.sha1.js. The tricky part now is to automatically change version.yml when you deploy. I make sure my deployment script executes the following bash script.

#!/bin/bash


FILE="app/config/version.yml"


echo "#This is an auto generated file that will be updated at every deploy" > $FILE


echo "parameters:
 git_commit: '$(git rev-parse --short HEAD)'" >> $FILE

With this little configuration I make sure that my JavaScripts and CSS are always up to date without me having to remember to bump the version in any configuration file.

Categories:

Updated:

Leave a Comment