Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 assets versioning by file

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 %}
  
like image 861
Martin Borthiry Avatar asked Jul 22 '13 18:07

Martin Borthiry


1 Answers

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.

like image 157
Martin Borthiry Avatar answered Oct 19 '22 22:10

Martin Borthiry