Dynamic Assets
You can change assets during an application life cycle, for instance when an administrator is configuring your website. In this case, the assets cache should be busted properly. Symfony does not manage this case out-of-the-box but the Asset Component can be easily enhanced to support this feature.
The following samples of code show how to add dynamic versioning for any asset package.
For example, let us assume that acme asset package should use the dynamic versioning.
First, register the package. Use Resources/config/oro/app.yml in your bundle or config/config.yml:
framework: assets: packages: acme: version_strategy: 'Oro\Bundle\AssetBundle\VersionStrategy\BuildVersionStrategy'
Set DynamicAssetVersionStrategy for this package.
namespace Acme\Bundle\DemoBundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; use Oro\Bundle\UIBundle\DependencyInjection\Compiler\DynamicAssetVersionPass; class DemoBundle extends Bundle { /** * {@inheritdoc} */ public function build(ContainerBuilder $container): void { parent::build($container); $container->addCompilerPass(new DynamicAssetVersionPass('acme')); } }
The configuration is finished.
Update the package version when your assets are changed:
namespace Acme\Bundle\DemoBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class SomeController extends AbstractController { public function updateAction() { // ... /** @var Oro\Bundle\UIBundle\Asset\DynamicAssetVersionManager $assetVersionManager */ $assetVersionManager = $this->container->get('oro_ui.dynamic_asset_version_manager'); $assetVersionManager->updateAssetVersion('acme'); // ... } public static function getSubscribedServices(): array { return array_merge( parent::getSubscribedServices(), ['oro_ui.dynamic_asset_version_manager' => DynamicAssetVersionManager::class] ); } }
The usage of your assets is the same as other assets, for example by the well-known asset() Twig function:
{{ asset('test.js', 'acme') }}
{# the result may be something like this: test.js?version=123-2 #}
{# where #}
{# '123' is the static asset version #}
{# '2' is the dynamic asset version; this number is increased each time you call $assetVersionManager->updateAssetVersion('acme') #}
Keep in mind that the package name should be passed to the asset() function. This tells Symfony that the asset belongs to your package and that dynamic versioning strategy should be applied.