Search code examples
symfonyasseticsymfony-3.2

Asset cache busting by parametrized url with Assetic


How can I configure asset versioning via Assetic in Symfony 3.2?

In Symfony 2 this could have been achieved by configuring packages version in framework:templating, but this is no longer the case in 3.

I've tried to use the following configuration:

framework:
  assets:
    packages:
      css:
        version: '2'
        version_format: '%%s?version=%%s'

While using this in the template:

{% stylesheets output="css/global.css" "@AppBundle/Resources/assets/scss/frontend.scss" filter="scss" filter="?uglifycss" package="css" %}
    <link rel="stylesheet" href="{{ asset(asset_url) }}">
{% endstylesheets %}

Unfortunately, this does not append the version parameter to the asset url, despite what Symfony's official documentation on assets suggests.


Solution

  • I've managed to solve this after some further research: one needs to specify the package name when calling the asset() function, like so:

    {% stylesheets output="css/global.css" "@AppBundle/Resources/assets/scss/frontend.scss" filter="scss" filter="?uglifycss" %}
         <link rel="stylesheet" href="{{ asset(asset_url, 'css') }}">
    {% endstylesheets %}
    

    The package name "css' needs to be defined in config:

    framework:
      assets:
      packages:
        css:
          version: '2'
          version_format: '%%s?version=%%s'