<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>Latest snippets tagged css</title>
  <link rel="alternate" href="http://snippets.prendreuncafe.com/snippets/tagged/css/order_by/date"></link>
  <id>http://snippets.prendreuncafe.com/snippets/tagged/css/order_by/date</id>
  <updated>2008-10-21T20:41:09Z</updated>
  <author>
    <name>Symfony</name>
    <author_email>noreply@symfony-project.com</author_email>
  </author>
<entry>
  <title>SVN based CSS/JS Caching trick</title>
  <link href="http://snippets.prendreuncafe.com/snippet/96"></link>
  <updated>2008-10-21T20:41:09Z</updated>
  <id>96</id>
  <summary type="html">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__

[code php]
&lt;?php
/**
 *  sfSVN : sf+SVN based CSS/JS caching trick
 *
 * @author [MA]Pascal &lt;pborreli@sqli.com&gt;
 */
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');
    }
}
[/code]

__Tweak your view.yml__

[code yml]
    stylesheets:
    - style.css?&lt;?php echo sfSVN::getHeadRevision().PHP_EOL ?&gt;
  javascripts:
    - functions.js?&lt;?php echo sfSVN::getHeadRevision().PHP_EOL ?&gt;
    - set.js?&lt;?php echo sfSVN::getHeadRevision().PHP_EOL ?&gt;
[/code]

__Clear the cache__
[code]
$ symfony cc
[/code]

__The result__

[code html]
&lt;script type=&quot;text/javascript&quot; src=&quot;/js/functions.js?158&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;/js/set.js?158&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; href=&quot;/css/style.css?158&quot; /&gt;
[/code]

__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</summary>
</entry>
</feed>
