Symfony2 assets versioning by file

10,196

Solution 1

After many days searching I found the packages option on AsseticBundle

http://symfony.com/doc/2.0/reference/configuration/framework.html#full-default-configuration

Using that config option I could do something like this:

{% javascripts file package='packageName' %}

or

{{asset(file,packageName)}}

Sample:

config.yml

framework:

    templating:
        engines: ['twig']
        assets_version: %assets_version%
        assets_version_format:  "stv%%2$s/%%1$s"
        packages:
             css:
                version: 6.1
                version_format: "stv%%2$s/%%1$s"
             jsApp:
                version: 4.2
                version_format: "stv%%2$s/%%1$s"

sometemplate.html.twig

<link rel=stylesheet href='{{ asset('bundles/webapp/css/funCommon.css','css') }}'>

{% javascripts 'bundles/webapp/js/app.js'
               'bundles/webapp/js/utils.js'
                filter='?closure'
                package='jsApp'
 %}
    <script src="{{ asset_url }}"></script>
 {% endjavascripts %}

The output of this is:

<link rel=stylesheet href="http://static.domain.com/stv6.1/css/HASH.css">

<script src="http://static.domain.com/stv4.2/js/HASH.js"></script>

For me that was the simplest way to manage assets version by file.

Solution 2

If you're trying to use the assets_version parameter with the javascripts or stylesheets helpers, you still need to use the asset helper as well.

{% javascripts 'bundles/webapp/app.js'
               'bundles/webapp/utils.js'
               filter='?closure' %}
    <script src="{{ asset(asset_url) }}" type="text/javascript"></script>
{% endjavascripts %}

It is not added to asset_url automatically (which is a good thing).

Solution 3

An easy and quick workaround is something like this:

{% set asset_version = 'xyz' %}

{% javascripts 'bundles/webapp/js/app.js'
           'bundles/webapp/js/utils.js'
            filter='?closure' %}
    <script src="{{ asset_url }}?{{ asset_version }}"></script>
{% endjavascripts %}

But you might want to move the logic to a twig extension receiving asset_url as argument.

The normal procedure would be to generate hashes of the files which will then be stored in a user cache.

You could then compare all the hashes against their current ones in a custom command and append the latest hash or something else to the filename to force a cache update.

Share:
10,196

Related videos on Youtube

Martin Borthiry
Author by

Martin Borthiry

Fan of Force.com platform. Developer and Administrator certified. I'm WPO (Web Performance Optimization) fan. I love improving frontend performance on webapps. Javascript Ninja :) keep it simple and keep it fast

Updated on June 04, 2022

Comments

  • Martin Borthiry
    Martin Borthiry almost 2 years

    Question

    Is it possible on Symfony2 use assets_version by file?

    Background

    We are using assets_version and assets_version_format to manage the files version and force the cache update on CDN's and browser cache.

    This is working like charm!, but, we found that there is only one assets_version parameters for all the static resources used.

    That is a problem since our webapp, has a long amount of static resources and we are deploying changes to prod environment daily. This situation kills the cache. :(

    This is our current config:

    config.yml

    framework:
       
        templating:
            engines: ['twig']
            assets_version: %assets_version%
            assets_version_format:  "stv%%2$s/%%1$s"
    
    # Assetic Configuration
    assetic:
        debug:          %kernel.debug%
        use_controller: false
        # java: /usr/bin/java
        filters:
             cssrewrite: ~
             closure:
                 jar: %kernel.root_dir%/java/compiler.jar
             yui_css:
                 jar: %kernel.root_dir%/java/yuicompressor-2.4.6.jar
    

    sometemplate.html.twig

        {% stylesheets 'bundles/webapp/css/funCommon.css'
                           'bundles/webapp/css/funMobile.css'
                           filter='?yui_css'
            %}
            <link rel=stylesheet href='{{ asset_url }}'>
            {% endstylesheets %}
        
        {% javascripts 'bundles/webapp/js/app.js'
                       'bundles/webapp/js/utils.js'
                        filter='?closure'  %}
            <script src="{{ asset_url }}"></script>
            {% endjavascripts %}
    
    {% javascripts 'bundles/webapp/js/moduleX.js'
                       'bundles/webapp/js/utilsX.js'
                        filter='?closure'  %}
            <script src="{{ asset_url }}"></script>
            {% endjavascripts %}
    

    When I change any css file or a module JS or any other file, all paths are changed.

    I would like to manage the version parameter of assets_version_format by parameter of javascript/stylesheet twig tag.

    This is what I'm looking for:

    {% javascripts 'bundles/webapp/js/app.js'
                   'bundles/webapp/js/utils.js'
                    filter='?closure' **version='XX'** %}
        <script src="{{ asset_url }}"></script>
        {% endjavascripts %}
      
    
  • Martin Borthiry
    Martin Borthiry over 10 years
    Thank you for your reply. That was my first approach, but I thought that maybe there exist a plugin to avoid this issue. I'll your to extend the twig extension
  • Martin Borthiry
    Martin Borthiry over 10 years
    I've found an out-of-the-box practical solution.
  • ZeeCoder
    ZeeCoder about 10 years
    Excellent, just what I needed.
  • Patrick Allaert
    Patrick Allaert about 9 years
    Thank you very much @rodnaph! Didn't understood why assets_version was not used.
  • penetra
    penetra almost 9 years
    Curious what process you'd use to ensure the version number gets updated when you go live? I wondering because most of the apps I work on, we're releasing changes multiple times a week and it'd be a pain to have to remember to do this each time.
  • Martin Borthiry
    Martin Borthiry almost 9 years
    Not really, that was automatic. The same script that move this to pre staging and production increment the statics version attribute.