Question

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)

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top