Symfony2,加载没有缓存的css文件

时间:2013-01-26 16:02:25

标签: css caching symfony assetic

目前我正在做基于symfony2的网站设计,问题是如何禁用css文件的缓存?现在,如果我在css文件中更改某些内容 - 浏览器中没有任何更改。当我尝试缓存时:清除 - 仍然没有。

config.yml

# Assetic Configuration
assetic:
debug:          "%kernel.debug%"
use_controller: true
#bundles:        [ ]
#java: /usr/bin/java
filters:
    cssrewrite: ~
    #closure:
    #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
    yui_css:
        jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

并在 twig

    {% stylesheets filter="cssrewrite" output="css/general.css"
        "@EveCommonBundle/Resources/public/css/main.css" 
        "@EveCommonBundle/Resources/public/css/another.css" %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
    {% endstylesheets %}

我必须更改什么才能在浏览器中获取“最新”的css文件?

问题是,当我更改@EveCommonBundle/Resources/public/css/main.css时,web/css仍然是旧的(由终端转储),并且没有重新创建,这就是为什么在浏览器中没有显示“新更改”,并重新创建那个文件我只能通过终端...我如何在浏览器的每个F5上重新创建cf2文件(在web/css文件夹中)?

4 个答案:

答案 0 :(得分:4)

问题是......呃,甚至不知道什么。 当前的config.yml和console命令完成了他们的工作。 (意味着我在css中更改的内容将在浏览器中显示为“在线模式”)

assets:install web --symlink
assetic:dump web
来自config.yml的

部分

# Twig Configuration
twig:
    cache:            false

# Assetic Configuration
assetic:
    assets: 
        default_css:
            inputs:
                - '%kernel.root_dir%/Resources/public/css/default.css'

    debug:          "%kernel.debug%"
    use_controller: true
    bundles:        []
    #java: /usr/bin/java
    filters:
        cssrewrite: ~

也有帮助(可能,不知道)

// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information

umask(0000);

在树枝中它看起来像这样

{% stylesheets "@default_css" filter="cssrewrite" %}
    <link href="{{ asset_url }}" type="text/css" media="all" rel="stylesheet" />
{% endstylesheets %}   

有了这个参数,我可以创建\ edit \删除来自css的任何数据,它会立即在浏览器中显示。

PS:我显示代码“不常见”,config.yml中的其他设置我认为与基础设置相同。

答案 1 :(得分:3)

在您的开发环境中,即通过app_dev.php访问您的网站时,资源的路径会动态生成,因此每次更改都应立即可见。

您可以使用以下命令自动创建资产(有关详细信息,请参阅食谱,请参阅下面的链接),但通常这不是必需的:

php app/console assetic:dump --watch

使用assetic并且您希望在prod-environment中看到您的更改时,您必须首先转储资产以使其可访问:

php app/console assetic:dump --env=prod --no-debug

有关详情,请参阅食谱中如何使用Assetic for Asset Management 中的Dumping Asset Files部分。

如果所有其他方法都失败了,您可能需要在模板中使用以下内容:

{% if app.environment == 'prod' %}{% else %}{% endif %}

仅在生产环境中使用资产。

答案 2 :(得分:1)

缓存是一种标准的浏览器行为。您可以每次手动清除它,也可以设置缓存清单:https://developer.mozilla.org/en-US/docs/HTML/Using_the_application_cache?redirectlocale=en-US&redirectslug=Offline_resources_in_Firefox 这是HTML5,并不是所有地方都支持。

禁用文件缓存的一种简单方法是每次文件更改时更改网址:您可以将随机字符串或版本号附加到您的href:

<link rel="stylesheet" type="text/css" href="{{ asset_url }}?{{some_random_string_goes_here}}=0" />

<link rel="stylesheet" type="text/css" href="{{ asset_url }}?VERSION={{version_number}}" />

随机字符串用于调试目的更简单,因为每次更改css时都不需要手动更新版本号。但是根据字符串的大小,你可能会运气不好并获得相同的字符串两次......

答案 3 :(得分:0)

如果你在dev中使用symfony 2资产。环境,只需使用此命令:

php app/console assets:install
php app/console assetic:dump --watch

参考:http://symfony.com/doc/2.1/cookbook/assetic/asset_management.html#dumping-asset-files-in-the-dev-environment