Via G. Falcone 5, Pollenza (MC), Italy
+39 0733 203595

Assets versioning with Symfony and Capifony

Just a quick tip for assets versioning with Symfony2.
Let’s imagine you have an asset, called “beautifulAsset.js”, in your app.
As soon as it’s requested, the user browser caches it.
So far so good.
Problems arise when you change the code inside “beautifulAsset.js” and deploy your application. If you don’t use a cache buster, your end user will keep on requesting the old “beautifulAsset.js” from your server. And since user browser caches it, the user won’t get your changes!

Symfony gives you a simple and elegant way to solve the problem: use the “assets_version” key of the FrameworkBundle.
http://symfony.com/doc/current/reference/configuration/framework.html#assets-version

Specifying in your configuration file the “assets_version” key, as stated in the link above, let you append the supplied value to your assets. This way your end user will request something like “beautifulAsset.js?mycustomAssetsVersion123456789”. And this will refresh browser cache.

Everything works fine, but there’s a drawback: you need to remember to update the “assets_version” key each time you change a single line of code in your assets.

We don’t want to do that. We use Capifony as deploy tool and there’s just a Capifony task for that: “symfony:assets:update_version”.
Using this task let you update your assets version each time you deploy. No need to manually change the “assets_version” key anymore.

This works as soon as you have your configuration under “app/confing/config.yml” file. Sadly this path is hardcoded in “symfony:assets:update_version” task.
If you have your configuration on a different path you can simply create your own task, as shown below, and use it in your deploy script.

 

Related Posts

Leave a reply