3

正如我所看到的,Assetic 在 CacheBusting 上取得了一些进展:
https ://github.com/kriswallsmith/assetic#cache-busting

但我真的不明白我应该如何使用它。
可以在树枝内使用吗:

{% 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 %}

并使用通常的assetic:dump命令?

我必须将 CacheBustingWorker 挂在哪里?

4

5 回答 5

23

Cache buster 现在是 symfony/AsseticBundle(版本 >= 2.5.0)的一部分。

像这样在 composer.json 中更改 AsseticBundle 版本:

"symfony/assetic-bundle": "2.5.0",

并像这样为您的 config.yml 文件中的资产激活缓存清除

assetic:
    workers:
        cache_busting: ~

我的 JS 文件现在看起来像这样:

web/bundles/js/projectname-876f9ee.js

https://github.com/symfony/AsseticBundle/pull/119#issuecomment-28877145

于 2014-10-22T12:54:35.660 回答
5

我最近一直在寻找如何做同样的事情。

我想出的解决方案是用我自己的类覆盖 Symfony 的 AssetFactory,并在其构造函数中添加 CacheBustingWorker。基本上,您创建一个如下所示的文件:

<?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));
    }
}

然后更改assetic.asset_factory.class 参数以指向配置中的这个新类。就我而言,我在 config.yml 中添加了以下内容:

parameters:
  assetic.asset_factory.class: YourSite\YourBundle\Factory\AssetFactory
于 2013-02-02T12:19:45.917 回答
4

使用当前的资产实现,我需要将您的代码更新为以下内容以使其正常工作。另请注意,如果您使用的是 xdebug,则必须将最大嵌套级别 - xdebug.max_nesting_level = 200 提高到 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))));
    }
}

希望这可以帮助某人

于 2013-06-07T11:15:37.537 回答
0

由于资产代码再次更改,主分支上的 LazyAssetManager 上不再需要 Stategy。

不要忘记更改您的composer.json文件:

{
    "kriswallsmith/assetic": "dev-master@dev",
    "symfony/assetic-bundle": "dev-master@dev"
}

你现在只需要这个:

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());
    }
} 

不要忘记php app/console cache:clear -e prod在转储资产之前避免生成标准文件名。

于 2013-10-21T13:15:10.087 回答
0
assetic:
    workers:
        cache_busting: ~

是答案。

于 2019-07-10T13:14:42.583 回答