Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update parameters based on vcs-status #39

Open
rvanlaak opened this issue Nov 12, 2013 · 8 comments
Open

Update parameters based on vcs-status #39

rvanlaak opened this issue Nov 12, 2013 · 8 comments

Comments

@rvanlaak
Copy link

I'm using Assetic in combination with compass and uglifycss to manage my assets. One of the configuration parameters of Assetic is assets_version in order to handle cache busting.

It would be nice if the ParameterHandler could update a parameter based on the current revision of a version control system (like svn or git). That would result in cache busting based on the code version, and would not bust the cache in case the code hasn't been updated but just some composer packages are updated.

@hacfi
Copy link

hacfi commented Nov 18, 2013

@rvanlaak Dealing with the same problem I came up with the following solution:

app/config/config.yml:

imports:
    - { resource: assets_version.php }
...
framework:
    ...
    templating:
        engines: ['twig']
        packages:
            style:
                version_format: %%s?v=%%s

app/config/assets_version.php

$container->loadFromExtension(
    'framework',
    array(
        'templating' => array(
            'packages' => array(
                'style' => array(
                    'version' => substr(
                        file_exists(__DIR__.'/../../web/css/style.css')
                            ? sha1_file(__DIR__.'/../../web/css/style.css')
                            : md5(time())
                        ,
                        0,
                        6
                    )
                ),
                ...
            )
        ),
    )
);

This way the assets version only changes if there are actual changes in the file.

@cordoval
Copy link
Contributor

very nice @hacfi this is the central css file that gets finally generated from all others right?

@stof
Copy link
Member

stof commented Nov 18, 2013

@rvanlaak ParameterHandler would not be able to get the asset version properly IMO.

a far better way to deal with it is to use the cache busting feature in Assetic. See symfony/assetic-bundle#240 for the PR adding it in the bundle.

@hacfi
Copy link

hacfi commented Nov 18, 2013

@cordoval Yes, in my template I got

  {% stylesheets '@ProjectAppBundle/Resources/less/make.less' filter='lessphp,?yui_css' output="css/style.css" package="style" -%}
    <link rel="stylesheet" href="{{ asset_url }}" />
  {% endstylesheets %}

I actually have more than this one css file but the main styling is that one big file. Instead of adding all the css/less files in my twig template I prefer doing this in the make.less.

@lyrixx
Copy link

lyrixx commented Jan 7, 2014

you can use env var to achive this one:

ASSET_VERSION=`git log -1 --pretty=%h` composer install --no-interaction

and of course, edit the composer.json as decribe in the README.

@rvanlaak
Copy link
Author

rvanlaak commented Jan 7, 2014

Both using an env var and editing the composer.json do not seem appropriate to me, because then I'm unable to access it in for example my Twig templates.

I solved this by a basic parameter in config.yml, which I also put in the Twig globals config section. Hereby I can use it in my entire application. And experience of the last weeks told me, sometimes the asset_version does not has to be incremented based on the revision number, because no css/jss/twig-files were committed and no cache busting was needed.

@pborreli
Copy link
Contributor

pborreli commented Jan 7, 2014

not sure if it could help you but i have a working easy example :
pborreli/composer-service#51 (comment)

@stof
Copy link
Member

stof commented Jan 8, 2014

@rvanlaak Making the variable available in Twig is about using the parameter. It is not related to the way you define it.

and then, your original report was describing assets_version as an AsseticBundle config, which is wrong. It is a FrameworkBundle config. AsseticBundle indeed uses it by default. And it is now able to do proper cache busting as the PR has been merged. But the asset version can still be useful for other assets (images for instance).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants