Snippets tagged "js" Snippets tagged "js"

SVN based CSS/JS Caching trick

You want to be sure client (browser or real one) uses the very last css/js file everytime you update svn repository.

Here's my proposition :

Make a sfSVN.class.php

<?php
/**
 *  sfSVN : sf+SVN based CSS/JS caching trick
 *
 * @author [MA]Pascal <pborreli@sqli.com>
 */
class sfSVN
{
    /**
     * returns the last revision number
     *
     * @return integer
     */
    public static function getHeadRevision ()
    {
        if (! file_exists($file = sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR . '.svn/entries')) {
            return date('Ymd');
        }
        $svn = file($file);
        return isset($svn[3]) ? (int) $svn[3] : date('Ymd');
    }
}

Tweak your view.yml

stylesheets:
    - style.css?<?php echo sfSVN::getHeadRevision().PHP_EOL ?>
  javascripts:
    - functions.js?<?php echo sfSVN::getHeadRevision().PHP_EOL ?>
    - set.js?<?php echo sfSVN::getHeadRevision().PHP_EOL ?>

Clear the cache

$ symfony cc

The result

<script type="text/javascript" src="/js/functions.js?158"></script>
<script type="text/javascript" src="/js/set.js?158"></script>
<link rel="stylesheet" type="text/css" media="screen" href="/css/style.css?158" />

Notes

This little script go read inside SVN file what's the last SVN revision number of your project everytime you clear the cache so it doesn't add much i/o.

Help me to get this script better if you have any idea :)

Cheers,

[MA]Pascal

by Pascal Borreli on 2008-10-21, tagged caching  css  js  subversion  svn