Symfony2, loading css files without cache

I am currently designing a symfony2 based website, the question is how to disable the css file cache? Now if I change something in the css file nothing changes in the browser. When I try to cache: clear - still nothing.

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"

      

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

      

What should I change to get the "latest" css file in the browser?

The problem is that when I change @EveCommonBundle/Resources/public/css/main.css

, in web/css

is still old (dumped by terminal) and it is not recreated, so why "new changes" do not show up in browsers and recreate this file, I can only on terminal ... like me can I get sf2 to recreate the css files on each F5 in the browser (in a folder web/css

)?

+3


source to share


4 answers


The problem was ... Hmm, I don't even know what it was. The current config.yml and console commands did their job. (means that whenever I change in css it will show as "online mode" in the browser)

assets:install web --symlink
assetic:dump web

      

parts from 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: ~

      

Also helps (maybe I don't know)



// 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);

      

In a branch it looks like

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

      

With these options, I can create \ edit \ remove any data from the css and it will be shown immediately in the browser.

PS: I showed the code "not normal", other settings in config.yml, I think the same as the basic one.

+4


source


In your dev environment, that is, when you access your site via app_dev.php

, asset paths are dynamically generated and therefore every change should be immediately visible.

You can use the following command to automatically create assets (more on this can be found in the cookbook, see link below), but this is usually not necessary:

php app/console assetic:dump --watch

      

When using assetic and want to see your changes in the prod environment, you must first dump the assets to make them available:

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

      



For more information, read the Dumping Asset Files section in Using Assetic to Manage Assets in the cookbook.

If all else fails, you can use the following in your templates:

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

      

use only averaging in a production environment.

+3


source


Caching is standard browser behavior. You can manually clear it every time or customize the cache manifest: https://developer.mozilla.org/en-US/docs/HTML/Using_the_application_cache?redirectlocale=en-US&redirectslug=Offline_resources_in_Firefox This is HTML5, although not supported everywhere.

An easy way to disable file caching is to change the url every time the file changes: you can add an arbitrary string or version number to your href:

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

      

or

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

      

A random string is easier for debugging purpose as you don't have to update the version number manually every time you change your css. But depending on the size of the string, you might be out of luck and end up with the same string twice ...

+1


source


if you are working with symfony 2 assets in dev. environment, just use this command:

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

      

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

0


source







All Articles