<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cory O&#039;Daniel - These are just words &#187; autoversioning</title>
	<atom:link href="http://coryodaniel.com/index.php/tag/autoversioning/feed/" rel="self" type="application/rss+xml" />
	<link>http://coryodaniel.com</link>
	<description>Software development, thoughts, and randomness</description>
	<lastBuildDate>Thu, 17 Nov 2011 21:18:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MovingParts: Auto versioning your Javascript and CSS</title>
		<link>http://coryodaniel.com/index.php/2009/03/17/movingparts-auto-versioning-your-javascript-and-css/</link>
		<comments>http://coryodaniel.com/index.php/2009/03/17/movingparts-auto-versioning-your-javascript-and-css/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 21:37:55 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[autoversioning]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[rubyyui]]></category>

		<guid isPermaLink="false">http://blog.vokle.com/?p=219</guid>
		<description><![CDATA[This tutorial has a few moving parts.  It could be easily recreated with any stack, but for this post I used Nginx and Merb.
Why?
We like to keep things fast.  Two ways we do that is compress the hell out of our js/css and set the cache time on our assets to max.  This is cool [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial has a few moving parts.  It could be easily recreated with any stack, but for this post I used Nginx and Merb.</p>
<p><strong>Why?</strong></p>
<p>We like to keep things fast.  Two ways we do that is compress the hell out of our js/css and set the cache time on our assets to max.  This is cool to do and obvious, the thing that sucks is when its time to roll out new javascript or css making sure the clients get the updates.</p>
<p><strong>What?</strong></p>
<p>For this example I am using merb and merb-assets (<a href="http://github.com/wycats/merb/commit/9226ab290ff9ddf70aff0296b80960c5c47ecc93" target="_blank">w/ my latest merb patch</a>), nginx and <a href="http://github.com/coryodaniel/ruby-yui/tree" target="_blank">ruby-yui.</a></p>
<p><strong>Step1 - Add version info to assets with merb-assets</strong></p>
<p>In our code base we have a version for our site, set in a variable like WWW_VERSION.  Using this variable it is easy (with the newest merb-assets plugin) to modify the path of all of your assets on the fly.</p>
<p>Stick this in your init.rb...</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
WWW_VERSION = <span style="color:#996600;">&quot;0.1.0&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># this will make require_js &quot;application&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># =&gt; /javascripts/application.0.1.0.js</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># instead of /javascripts/application.js</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">Merb::Plugins</span>.<span style="color:#9900CC;">config</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:asset_helpers</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:js_suffix</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;.#{WWW_VERSION}&quot;</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">Merb::Plugins</span>.<span style="color:#9900CC;">config</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:asset_helpers</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:css_suffix</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;.#{WWW_VERSION}&quot;</span></pre></td></tr></table></div>

<p> </p>
<p>At this point in time merb is going to start outputting modified paths to your assets that contain version numbers.  This is cool because now when we modify javascript or css and bump the version number it will cause the clients that have cached the static assets to redownload them since the file path is different.  The part that sucks is renaming a bunch of javascript and css files.</p>
<p><strong>Step2 - Lying about file names</strong></p>
<p>Renaming a bunch of files does suck!  So don't do it.  Instead have that sneaky russian Nginx lie about it for you.</p>
<p>Stick this in you nginx.conf...</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">&nbsp;
location ~ ^<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>javascripts<span style="color: #000000; font-weight: bold;">|</span>stylesheets<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
&nbsp;
  rewrite ^<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>javascripts<span style="color: #000000; font-weight: bold;">|</span>stylesheets<span style="color: #7a0874; font-weight: bold;">&#41;</span>\<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>^\d<span style="color: #7a0874; font-weight: bold;">&#93;</span>+<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>?:\.<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>+<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>\.<span style="color: #7a0874; font-weight: bold;">&#40;</span>js<span style="color: #000000; font-weight: bold;">|</span>css<span style="color: #7a0874; font-weight: bold;">&#41;</span>$ <span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">/</span>$2.$<span style="color: #000000;">3</span>;
&nbsp;
  expires max;
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">break</span>;
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>This rewrite rule will make nginx route any javascript/stylesheet to the original file if it contains a version number.  So a request like to '/javascripts/application.0.1.0.js' will be rewritten to '/javascripts/application.js'.  Nifty.</p>
<p> </p>
<p><strong>Step3 - Compress those assets</strong></p>
<p>You can compress your assets in a number of ways, I'll go with shameless self promotion and recommend <a href="http://blog.vokle.com/index.php/2008/12/15/ruby-yui/" target="_blank">RubyYUI</a>.  RubyYUI is a ruby wrapper for the Java-based YUI  Compressor.  It lets you glob paths instead of doing one file at a time and add suffixes to files.  All sorts of things.</p>
<p>You can do something like this in a vlad or capistrano task during deployment.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'ruby-yui'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># this will cause the files to be compressed in place</span>
&nbsp;
Yui.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;./public/javascripts&quot;</span>,:stomp <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:suffix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">compress</span>
&nbsp;
Yui.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;./public/stylesheets&quot;</span>,:stomp <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:suffix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#ff3333; font-weight:bold;">:type</span><span style="color:#006600; font-weight:bold;">=&gt;</span>:css<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">compress</span></pre></td></tr></table></div>

<p> </p>
<p>Now you have compressed assets in place in your deployed app and nginx will rewrite any versioned paths to your compressed files.  Bump your version number and all your clients will start getting the new compressed files.  Wee.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=MovingParts%3A+Auto+versioning+your+Javascript+and+CSS+http://coryodaniel.com/?p=219" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2009/03/17/movingparts-auto-versioning-your-javascript-and-css/&amp;title=MovingParts%3A+Auto+versioning+your+Javascript+and+CSS" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2009/03/17/movingparts-auto-versioning-your-javascript-and-css/&amp;t=MovingParts%3A+Auto+versioning+your+Javascript+and+CSS" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2009/03/17/movingparts-auto-versioning-your-javascript-and-css/&amp;title=MovingParts%3A+Auto+versioning+your+Javascript+and+CSS" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2009/03/17/movingparts-auto-versioning-your-javascript-and-css/&amp;title=MovingParts%3A+Auto+versioning+your+Javascript+and+CSS" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2009/03/17/movingparts-auto-versioning-your-javascript-and-css/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

