3

After reading this thread: How to force browser to reload cached CSS/JS files?

I would like to know if there is any built-in function or easy way in Symfony that automatically forces a reload by appending a random querystring or timestamp to the link when it has discovered that javascript / css file has been modified. (Normally, people use the use_javascript function to generate the <script> tag)

Community
  • 1
  • 1
bobo
  • 8,439
  • 11
  • 57
  • 81

2 Answers2

3

There is no built-in mechanism, but a little creativity means you can do this just about anywhere in your code, from view.yml to layout.php to each individual action.

The view.yml method is easy enough:

apps/frontend/config/view.yml:

  stylesheets:    [main?v=<?php echo time() ?>, reset?v=<?php echo time() ?>, layout?v=<?php echo time() ?>]

Although I think this is a little too active, and I tend to use either the SVN revision or a overall project version number:

  stylesheets:    [main?v=<?php echo sfConfig('app_project_version') ?>, reset?v=<?php echo sfConfig('app_project_version') ?>, layout?v=<?php echo sfConfig('app_project_version') ?>]

where app_project_version is set in apps/frontend/config/app.yml. Methods for layout.php and actionSuccess.php should be easy enough from here:

<?php use_stylesheet('blah?v='.sfConfig::get('app_project_version')); ?>
Raise
  • 2,034
  • 1
  • 13
  • 12
  • But this would only have effects when using include_javascript / include_stylesheet. In my Symfony application, view.yml is not used at all, the use_javascript and use_stylesheet functions are used instead. – bobo Feb 25 '10 at 16:46
  • The principle is the same - these functions are practically synonyms for each other, just used in different contexts (YAML and PHP). In your case, simply use: `` I have updated the answer with this solution. – Raise Feb 25 '10 at 17:40
  • Thanks for your help. In the end, I adopted your idea and modified the javascript_path and stylesheet_path functions in AssetHelper.php of the framework to make this possible. But I would have to keep this in mind when doing the version upgrade. – bobo Feb 26 '10 at 21:28
2

instead of setting a version for each stylesheet you include, it is better to have it done automatically for all included stylesheets, no matter if you use view.yml or use_stylesheet() method. You need to implement this helper method and include the helper in your applications settings.yml, so that it becomes available to alle your actions.

`

function include_versioned_stylesheets()
{
    $response = sfContext::getInstance()->getResponse();
    sfConfig::set('symfony.asset.stylesheets_included', true);
    $html = '';
    foreach ($response->getStylesheets() as $file => $options) {
        $filepath = sfConfig::get('sf_web_dir') . '/' .  stylesheet_path($file);
        if(file_exists($filepath)) {
            $file .= '?v=' . filectime($filepath);
        }
        $html .= stylesheet_tag($file, $options);
    }
    echo $html;
}

`

in your layout.php call this inside your header area. make sure there is no further call to include_stylesheets(), as this is an extended version to it. same can be done with include_javascripts.

alexx
  • 41
  • 4