Reputation: 5986
As i can see Assetic made some progress on CacheBusting:
https://github.com/kriswallsmith/assetic#cache-busting
But i dont really understand how i should use this.
Can this be used from within twig:
{% stylesheets 'bundles/mybundle/css/fonts.css'
'bundles/mybundle/css/style.css'
'bundles/mybundle/css/screen.css'
filter='cssrewrite'
%}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}
And with the usual assetic:dump
command?
Where would i have to hook the CacheBustingWorker in?
Upvotes: 3
Views: 4710
Reputation: 662
Cache buster is now part of symfony/AsseticBundle (Version >= 2.5.0).
Change AsseticBundle version in composer.json like that:
"symfony/assetic-bundle": "2.5.0",
And activate cache busting for assets in your config.yml file like that
assetic:
workers:
cache_busting: ~
My JS files are now looking like that:
web/bundles/js/projectname-876f9ee.js
See https://github.com/symfony/AsseticBundle/pull/119#issuecomment-28877145
Upvotes: 23
Reputation: 5040
As the assetic code changed again, there is no need of Stategy on LazyAssetManager on the master branch.
Do not forget to change your composer.json
file:
{
"kriswallsmith/assetic": "dev-master@dev",
"symfony/assetic-bundle": "dev-master@dev"
}
You now just need this:
namespace YourSite\YourBundle\Factory;
use Symfony\Bundle\AsseticBundle\Factory\AssetFactory as BaseAssetFactory;
use Assetic\Factory\Worker\CacheBustingWorker;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class AssetFactory extends BaseAssetFactory
{
public function __construct(
KernelInterface $kernel,
ContainerInterface $container,
ParameterBagInterface $parameterBag,
$baseDir,
$debug = false
) {
parent::__construct($kernel, $container, $parameterBag, $baseDir, $debug);
// Add CacheBustingWorker
$this->addWorker(new CacheBustingWorker());
}
}
Do not forget to php app/console cache:clear -e prod
before dumping assets one time to avoid standard filenames to be generated.
Upvotes: 0
Reputation: 381
With the current implementation of assetic, I needed to update your code to the following to get this to work. Also note if you are using xdebug, you must up the max nesting level - xdebug.max_nesting_level = 200 to more than 100.
<?php
namespace YourSite\YourBundle\Factory;
use Symfony\Bundle\AsseticBundle\Factory\AssetFactory as BaseAssetFactory;
use Assetic\Factory\LazyAssetManager;
use Assetic\Factory\Worker\CacheBustingWorker;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class AssetFactory extends BaseAssetFactory
{
public function __construct(KernelInterface $kernel, ContainerInterface $container, ParameterBagInterface $parameterBag, $baseDir, $debug = false)
{
parent::__construct($kernel, $container, $parameterBag, $baseDir, $debug);
// Add CacheBustingWorker
$this->addWorker(new CacheBustingWorker(new LazyAssetManager(new BaseAssetFactory($kernel, $container, $parameterBag, $baseDir, $debug))));
}
}
Hope this helps someone
Upvotes: 4
Reputation: 236
I have been recently looking how to do the same.
The solution I came up with was to override Symfony's AssetFactory with my own class and add the CacheBustingWorker in its constructor. Basically you create a file like the following:
<?php
namespace YourSite\YourBundle\Factory;
use Symfony\Bundle\AsseticBundle\Factory\AssetFactory as BaseAssetFactory;
use Assetic\Factory\Worker\CacheBustingWorker;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class AssetFactory extends BaseAssetFactory
{
public function __construct(KernelInterface $kernel, ContainerInterface $container, ParameterBagInterface $parameterBag, $baseDir, $debug = false)
{
parent::__construct($kernel, $container, $parameterBag, $baseDir, $debug);
// Add CacheBustingWorker
$this->addWorker(new CacheBustingWorker(CacheBustingWorker::STRATEGY_CONTENT));
}
}
and then change the assetic.asset_factory.class parameter to point to this new class in your config. In my case I added the following to config.yml:
parameters:
assetic.asset_factory.class: YourSite\YourBundle\Factory\AssetFactory
Upvotes: 5