<?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>Bindle Blog</title>
	<atom:link href="http://bindle.me/index.php/feed" rel="self" type="application/rss+xml" />
	<link>http://bindle.me/blog</link>
	<description>Writings about Bindle, gear, and life</description>
	<lastBuildDate>Tue, 27 Nov 2012 00:39:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Augmenting Meteor: plugging gaps with external services.</title>
		<link>http://bindle.me/blog/index.php/726/augmenting-meteor-plugging-gaps-with-external-services</link>
		<comments>http://bindle.me/blog/index.php/726/augmenting-meteor-plugging-gaps-with-external-services#comments</comments>
		<pubDate>Thu, 19 Jul 2012 06:20:16 +0000</pubDate>
		<dc:creator>Tom Coleman</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[meteor]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=726</guid>
		<description><![CDATA[Meteor is still in it&#8217;s infancy. There&#8217;s a good chance you&#8217;ll want to do something it is not mature enough to achieve right now. What&#8217;s a poor developer to do? When you can&#8217;t roll your own solution, you could consider using an external service that talks to your meteor server to get things done. In [...]]]></description>
			<content:encoded><![CDATA[<p>Meteor is still in it&#8217;s infancy. There&#8217;s a good chance you&#8217;ll want to do something it is not mature enough to achieve right now. What&#8217;s a poor developer to do? When you can&#8217;t roll your own solution, you could consider using an external service that talks to your meteor server to get things done. In this article I&#8217;ll describe a couple of ways we did this to augment League, our sport-management application.</p>
<p><span id="more-726"></span></p>
<h3>Sending emails</h3>
<p>As a seasoned Rails developer, I&#8217;m used to the nicer things when it comes to emails. ActionMailer is pretty good these days, and the <a href="https://github.com/fphilipe/premailer-rails3">premailer</a> gem allows you to use (most) of your styles freely within your emails.</p>
<p>Things are a little more raw and exciting in the meteor world; <a href="https://github.com/Marak/node_mailer">Node</a> <a href="https://github.com/eleith/emailjs">modules</a> exist for sending emails, but integrating node modules into a meteor deployment can be a challenge, and it doesn&#8217;t seem like a premailer replacement is out there.</p>
<p>So, we took the &#8220;easy&#8221; road, and set up a simple rails project with a single controller + action: to fire off emails:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> MailsController <span style="color:#006600; font-weight:bold;">&gt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    mail = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:mail</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_sym</span>
    data = <span style="color:#6666ff; font-weight:bold;">ActiveSupport::JSON</span>.<span style="color:#9900CC;">decode</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:data</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">with_indifferent_access</span>
&nbsp;
    Notifications.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>mail, data<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">deliver</span>
    head <span style="color:#ff3333; font-weight:bold;">:ok</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Calling out to the mailer from meteor easy enough:</p>

<div class="wp_syntax"><div class="code"><pre class="coffeescript" style="font-family:monospace;">Meteor.http.call 'POST', LeagueMailerConfig.url, options</pre></div></div>

<h3>Serving calendars via <code>.ics</code></h3>
<p>We wanted to make keeping track of a League team easier by integrating with calendaring apps via the <code>.ics</code> format. It isn&#8217;t possible using pure meteor right now because there is no server side routing at the moment (it serves up the same <code>html|js|css</code> mix whatever URL you hit).</p>
<p>We decided to re-purpose the league-mailer rails server mentioned above and create an end-point that communicates to the league app via DDP &#8211; meteor&#8217;s distributed data protocol. DDP is a pretty simple protocol and it wasn&#8217;t difficult to create a <a title="Ruby DDP Client" href="https://github.com/tmeasday/ruby-ddp-client">ruby DDP client</a>. Here&#8217;s the <a title="Ruby DDP Client Gem" href="http://rubygems.org/gems/ruby-ddp-client">gem</a> if you want to use it yourself.</p>
<p>Then we just needed to use event-machine to subscribe to our <code>games</code> publication to fill out a <code>games</code> collection and get our results:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> LeagueServer
  <span style="color:#008000; font-style:italic;"># pretty simplistic, but it seems to work</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">get_games</span><span style="color:#006600; font-weight:bold;">&#40;</span>team_ids<span style="color:#006600; font-weight:bold;">&#41;</span>
    games = <span style="color:#0000FF; font-weight:bold;">nil</span>
    EM.<span style="color:#9900CC;">run</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      league_client = <span style="color:#9966CC; font-weight:bold;">if</span> ::Rails.<span style="color:#9900CC;">env</span>.<span style="color:#9900CC;">production</span>?
        <span style="color:#6666ff; font-weight:bold;">RubyDdp::Client</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'beta.getleague.com'</span>, <span style="color:#006666;">80</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#6666ff; font-weight:bold;">RubyDdp::Client</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'localhost'</span>, <span style="color:#006666;">3000</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      league_client.<span style="color:#9900CC;">onconnect</span> = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>event<span style="color:#006600; font-weight:bold;">|</span>
        league_client.<span style="color:#9900CC;">subscribe</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'games'</span>, <span style="color:#006600; font-weight:bold;">&#91;</span>team_ids<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>result<span style="color:#006600; font-weight:bold;">|</span>
          games = league_client.<span style="color:#9900CC;">collections</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'games'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
          EM.<span style="color:#9900CC;">stop_event_loop</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    games
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>From there, it&#8217;s standard rails routing and some harnessing of the excellent <a href="http://icalendar.rubyforge.org/">iCalendar gem</a> to get the right data published at a URL of your choice!</p>
<p>In the future I hope to pull both of these services inside the League meteor app.</p>
]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/726/augmenting-meteor-plugging-gaps-with-external-services/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Page Transitions in Meteor: getleague.com</title>
		<link>http://bindle.me/blog/index.php/679/page-transitions-in-meteor-getleague-com</link>
		<comments>http://bindle.me/blog/index.php/679/page-transitions-in-meteor-getleague-com#comments</comments>
		<pubDate>Thu, 21 Jun 2012 07:08:36 +0000</pubDate>
		<dc:creator>Tom Coleman</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[meteor]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=679</guid>
		<description><![CDATA[Update: I&#8217;ve released my reactive router and the (complete) transitioner class. Enjoy! Note: dear readers from the future!: at the time of writing the released meteor version is 0.3.7; as Meteor is a rapidly changing framework, it is likely this post is out of date! I’d like to briefly walk through the process that we [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Update: I&#8217;ve released my reactive <a href="https://github.com/tmeasday/meteor-router">router</a> and the (complete) <a href="https://github.com/tmeasday/meteor-transitioner">transitioner</a> class. Enjoy!</strong></em></p>
<p><em>Note: dear readers from the future!: at the time of writing the released meteor version is 0.3.7; as Meteor is a rapidly changing framework, it is likely this post is out of date!</em></p>
<p>I’d like to briefly walk through the process that we used to implement page-&gt;page transitions in the League project [<a href="http://bindle.me/blog/index.php/679/page-transitions-in-meteor-getleague-com#footnote_0_679" id="identifier_0_679" class="footnote-link footnote-identifier-link" title="currently in alpha testing at http://beta.getleague.com; check it out if you like but please be kind">1</a>].</p>
<p><span id="more-679"></span></p>
<p>Meteor apps are single page, client side Javascript applications, but this doesn’t mean you can’t have more than one screen. Smartphone users are more than familiar with the sense of ‘space’ created by having pages slide into one another. In this article I’ll describe how I used a <code>Transitioner</code> class in meteor to achieve page transitions.</p>
<h4>HTML Setup</h4>
<p>Our main <code>index.html</code> looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">  &lt;div class=&quot;container current_page {{current_page}}&quot;&gt;
    {{#if_with current_page}}{{&gt; one_screen}}{{/if_with}}
  &lt;/div&gt;
  &lt;div class=&quot;container next_page {{next_page}}&quot;&gt;
    {{#if_with next_page}}{{&gt; one_screen}}{{/if_with}}
  &lt;/div&gt;</pre></div></div>

<p>The <code>one_screen</code> template as you may guess, draws one screen, much like:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;template name=&quot;one_screen&quot;&gt;
  {{#if_equals &quot;teams&quot; this}}{{&gt; teams }}{{/if_equals}}
  {{#if_equals &quot;players&quot; this}}{{&gt; players }}{{/if_equals}}
  ...
&lt;/template&gt;</pre></div></div>

<p>The two variables <code>current_page</code> and <code>next_page</code> represent the two pages in the current transition. So if we are transitioning from the teams to the players page, we simply render the teams page into <code>.current_page</code> and players into <code>.next_page</code> and trigger a transition which animates <code>.current_page</code> out to the left and <code>.next_page</code> in from the right:</p>
<div style="width: 404px; height: 400px; margin: 0 auto; overflow:hidden; white-space: nowrap;">
<div style="position: relative; left: 0; display: inline-block; text-align: center; width: 400px; height: 400px; background: #6699cc; border: 2px solid #3366cc; -webkit-transition: left 500ms ease-out; -moz-transition: left 500ms ease-out;" onclick="jQuery(this).parent().children().css('left', '-404px');">First page: Click me!</div>
<div style="position: relative; left: 0; display: inline-block; text-align: center; width: 400px; height: 400px; background: #ffcc99; border: 2px solid #ff9966; -webkit-transition: left 500ms ease-out; -moz-transition: left 500ms ease-out;" onclick="jQuery(this).parent().children().css('left', '0');">Second page: Click me!</div>
</div>
<p>&nbsp;</p>
<p>Here&#8217;s an action shot of it happening live:</p>
<div id="attachment_680" class="wp-caption aligncenter" style="width: 610px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/06/league-transition.png"><img class="size-large wp-image-680" title="League Transition in Action" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/06/league-transition-1024x888.png" alt="League Transition in Action" width="600" height="520" /></a><p class="wp-caption-text">There are some nice touches, like the page that is leaving fading out as it slides out.</p></div>
<p>&nbsp;</p>
<h4>Meteor Setup</h4>
<p>So where do these variables <code>current_page</code> and <code>next_page</code> come from? The <code>Transitioner</code>. Let’s start this story at the <code>Router</code>. It’s actually a little bit complex, but the short story is that the router sets up a reactive variable representing the current page. Here’s some abbreviated code which uses <a href="https://github.com/meteor/meteor/pull/193" title="My deps-extensions package">deps-extensions</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">AuthenticatedRouter <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">Router</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Meteor.<span style="color: #660066;">deps</span>.<span style="color: #660066;">add_reactive_variable</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> ‘page’<span style="color: #339933;">,</span> ‘loading’<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  goto<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>page<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">page</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span>page<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Route functions call <code>goto()</code> to inform the rest of the application of which page should be visible. The <code>Transitioner</code> basically wraps <code>Router.page()</code> with a delay:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  Transitioner <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>Router<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Meteor.<span style="color: #660066;">deps</span>.<span style="color: #660066;">add_reactive_variable</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> ‘current_page’<span style="color: #339933;">,</span> Router.<span style="color: #660066;">page</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Meteor.<span style="color: #660066;">deps</span>.<span style="color: #660066;">add_reactive_variable</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> ‘next_page’<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Meteor.<span style="color: #660066;">deps</span>.<span style="color: #660066;">await</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> Router.<span style="color: #660066;">page</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">current_page</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">transition_to</span><span style="color: #009900;">&#40;</span>Router.<span style="color: #660066;">page</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
  Transitioner.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">transition_to</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>page<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    self.<span style="color: #660066;">next_page</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span>page<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// we defer so that the next page has rendered + is attached before we add the class</span>
    Meteor.<span style="color: #660066;">defer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span>‘body’<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>‘transitioning’<span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">one</span><span style="color: #009900;">&#40;</span>‘transitionend’<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          self.<span style="color: #660066;">transition_end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
  Transitioner.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">transition_end</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    self.<span style="color: #660066;">current_page</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">next_page</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    self.<span style="color: #660066;">next_page</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span>‘body’<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>‘transitioning’<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The magic of reactive programming huh? Simple [<a href="http://bindle.me/blog/index.php/679/page-transitions-in-meteor-getleague-com#footnote_1_679" id="identifier_1_679" class="footnote-link footnote-identifier-link" title="actually it&rsquo;s a bit more complex than this as some of my CSS transitions also require the &amp;lt;body&amp;gt; to have the current and next page set as classes. Also there are edge cases to deal with. But the core idea is there.">2</a>]. I like it.</p>
<h4>CSS Setup</h4>
<p>This bit isn’t really meteor specific, but you might want to know how to achieve a sliding transition. It’s quite easy if you always want to go left to right:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.current_page</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.next_page</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
  <span style="color: #a1a100;">@include transition( all 500ms cubic-bezier(0.51, 0, 0.6, 1) 0ms);</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.current_page</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.next_page</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.transitioning</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #6666ff;">.current_page</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-100%</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.next_page</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Things are more complex when you want different page transitions to have different animations, check out <a href="https://github.com/tmeasday/league/blob/master/client/stylesheets/base/layout/_transition.scss" title="League's transition stylesheet."><code>_transition.scss</code></a> if you want to see how we achieved League’s other animations.</p>
<p>Let me know what you think about my techniques and if you have a better way to do anything! </p>
<ol class="footnotes"><li id="footnote_0_679" class="footnote">currently in alpha testing at <a href="http://beta.getleague.com">http://beta.getleague.com</a>; check it out if you like but please be kind</li><li id="footnote_1_679" class="footnote">actually it’s a bit more complex than this as some of my CSS transitions also require the <code>&lt;body&gt;</code> to have the current and next page set as classes. Also there are edge cases to deal with. But the core idea is there.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/679/page-transitions-in-meteor-getleague-com/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Animations in Meteor: state of the game</title>
		<link>http://bindle.me/blog/index.php/658/animations-in-meteor-state-of-the-game</link>
		<comments>http://bindle.me/blog/index.php/658/animations-in-meteor-state-of-the-game#comments</comments>
		<pubDate>Thu, 21 Jun 2012 06:06:41 +0000</pubDate>
		<dc:creator>Tom Coleman</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[meteor]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=658</guid>
		<description><![CDATA[Note: dear readers from the future! at the time of writing the released meteor version is 0.3.7; as Meteor is a rapidly changing framework, it is likely this post is out of date! UPDATE: David Greenspan posted some detailed info about the heuristics that meteor uses to update DOM nodes. The important takeaway: you can [...]]]></description>
			<content:encoded><![CDATA[<p><em>Note: dear readers from the future! at the time of writing the released meteor version is 0.3.7; as Meteor is a rapidly changing framework, it is likely this post is out of date!</em></p>
<p><em><strong>UPDATE: David Greenspan <a href="https://groups.google.com/d/msg/meteor-talk/Em6cNrA3nH8/4_0bOEHNfLQJ">posted</a> some detailed info about the heuristics that meteor uses to update DOM nodes. The important takeaway: you can achieve animations the &#8216;meteor way&#8217; if you set an <code>id</code> on your DOM nodes (or a <code>name</code> on form elements). More details below.</strong></em></p>
<p>A common question that people have as they begin to use Meteor for developing a javascript application is how to implement animations. This isn’t surprising as the example applications don’t really use them and the docs make no mention of it. Yet for a client-side responsive JS app, animating JS changes makes a lot of sense.<br />
<span id="more-658"></span></p>
<p>In this post I&#8217;d like to discuss the animation of a simple expanding div, as seen below.</p>
<div style="text-align: center; margin-left: 100px; width: 200px; height: 200px; background: #ffcc99; border: 2px solid #ff9966; -webkit-transition: width 500ms ease-out; -moz-transition: width 500ms ease-out;" onclick="jQuery(this).css('width', '400px');">Click me!</div>
<h4><strong>Using CSS transitions</strong></h4>
<p>The HTML5 way to achieve this is using the CSS3 transition property. CSS transitions work when the class changes on an element. The browser will animate the transition of a CSS property between the old value (as specified by the first class) and the new.</p>
<h4><strong>1. Reactively: the Meteor way</strong></h4>
<div>
<p>It fits with Meteor’s reactive setup to force the class on an element to change via a variable change. For instance, with the correct CSS setup, we could do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;{{open}}&quot; id=&quot;clickme&quot;&gt;Click me!&lt;/div&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Template.<span style="color: #660066;">foo</span>.<span style="color: #000066;">open</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Session.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>‘<span style="color: #000066;">open</span>’<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> ‘<span style="color: #000066;">open</span>’<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Template.<span style="color: #660066;">foo</span>.<span style="color: #660066;">events</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>‘click #clickme’<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
  Session.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span>‘<span style="color: #000066;">open</span>’<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The only challenge in this technique is naming the session variable to use to store the state of the <code>&lt;div&gt;</code> [<a href="http://bindle.me/blog/index.php/658/animations-in-meteor-state-of-the-game#footnote_0_658" id="identifier_0_658" class="footnote-link footnote-identifier-link" title="the nice thing about using a session var is that the &amp;lt;div&amp;gt; will remain open if there is a hot-code change. If this isn&amp;#8217;t what you want, you might consider adding a reactive variable to your data object">1</a>]. However, it&#8217;s usually sensible to call it something like <code>this._id + '-open'</code>, depending on exactly what you are doing. In the future, meteor will have ways to attach reactive variables to the &#8220;template invocation&#8221; that&#8217;s currently rendering&#8212;this will avoid using the session in ways that it&#8217;s not really intended for.</p>
<p>Please note that this technique <em><strong>only works if you set an <code>id</code> on your <code>&lt;div&gt;</code></strong></em>. This is because it isn&#8217;t easy for meteor to tell when you&#8217;ve simply changed the class on an element&#8212;from meteor&#8217;s perspective you may have deleted the original <code>&lt;div&gt;</code> and created a totally new <code>&lt;div&gt;</code> which is identical except for the classname change. Setting an <code>id</code> tells meteor that, no, in fact that is the same element after all. (Similarly, you can just set the <code>name</code> attribute on a form element).</p>
<h4><strong>2. Directly: the jQuery way</strong></h4>
<div>The more direct, jQuery way to do it is to simply go</div>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Template.<span style="color: #660066;">foo</span>.<span style="color: #660066;">events</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>‘click .<span style="color: #660066;">somewhere</span>’<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
  $<span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>‘<span style="color: #000066;">open</span>’<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<div>
<p>This technique <strong><em>will</em></strong> work, however, be very careful. If for some reason the template needs to be reactively re-drawn (for instance the underlying data is updated), the class-name will be lost as the element will be re-rendered. There are techniques to try and isolate meteor&#8217;s re-rendering (for instance using <code>Meteor.ui.chunk</code> in a helper, the <code>{{#each}}</code> helper, or a using separate template). However, you are basically swimming upstream with this approach; for this reason I think doing things this way is likely to be a maintenance nightmare. Use at your own risk.</p>
<p>What other techniques are people using for animation?
</p></div>
<ol class="footnotes"><li id="footnote_0_658" class="footnote">the nice thing about using a session var is that the <code>&lt;div&gt;</code> will remain open if there is a hot-code change. If this isn&#8217;t what you want, you might consider adding a reactive variable to your data object</li></ol>]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/658/animations-in-meteor-state-of-the-game/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>When the Panda strikes &#8212; Why you can&#8217;t afford to ignore SEO</title>
		<link>http://bindle.me/blog/index.php/615/when-the-panda-strikes-why-you-cant-afford-to-ignore-seo</link>
		<comments>http://bindle.me/blog/index.php/615/when-the-panda-strikes-why-you-cant-afford-to-ignore-seo#comments</comments>
		<pubDate>Thu, 29 Mar 2012 02:23:41 +0000</pubDate>
		<dc:creator>Tom Coleman</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[duplicate content]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[keyword limit]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[serp]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=615</guid>
		<description><![CDATA[You&#8217;re too busy for SEO. You’ve got a hundred things in the queue to get your latest site off the ground, and time spent optimizing for a robot is at the bottom of the list. That’s fair. Search engines shouldn’t require us to do anything. Just build a good website, make some content, get incoming [...]]]></description>
			<content:encoded><![CDATA[<p><strong>You&#8217;re too busy for SEO</strong>. You’ve got a hundred things in the queue to get your latest site off the ground, and time spent optimizing for a robot is at the bottom of the list. That’s fair.</p>
<p>Search engines shouldn’t require us to do anything. Just build a good website, make some content, get incoming links (so people can find you!), and, if it’s interesting enough, it should appear on the first page. Sounds reasonable?</p>
<p>Well, unfortunately, neither of these things are true. Here’s the story of how we discovered we weren’t appearing in Google searches for ‘Bindle’ and the journey to find a solution. Along the way we learn the importance of restraint to keep the GoogleBot happy.<br />
<span id="more-615"></span></p>
<h3><strong>Where are we?: The Problem.</strong></h3>
<p>Just three months ago when you tried to find <em>Bindle</em> via Google search we’d be absent from the results… until page 38. The only hint that our site existed was passing reference from blogs that had featured us. Bing, on the other hand, proudly displayed us at #6 on their results list. What was going on?</p>
<div id="attachment_619" class="wp-caption alignleft" style="width: 288px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-01-13-at-10.25.52-AM.png"><img class="size-medium wp-image-619" title="Google Search results for Bindle" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-01-13-at-10.25.52-AM-300x171.png" alt="Where are we?" width="278" height="171" /></a><p class="wp-caption-text">2 relatively minor blogs linking to our homepage, but no sign of that homepage itself.</p></div>
<div id="attachment_620" class="wp-caption alignleft" style="width: 288px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-01-14-at-11.27.35-AM.png"><img class="size-medium wp-image-620" title="Bing Search Results" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-01-14-at-11.27.35-AM-300x158.png" alt="We're there on Bing" width="278" height="158" /></a><p class="wp-caption-text">In Bing, on the other hands, we appear at #6.</p></div>
<p>&nbsp;</p>
<div id="attachment_618" class="wp-caption aligncenter" style="width: 310px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-01-12-at-8.01.07-AM.png"><img class="size-medium wp-image-618" title="Google Webmaster Tools" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-01-12-at-8.01.07-AM-300x57.png" alt="Webmaster Tools shows we are result #380" width="300" height="57" /></a><p class="wp-caption-text">Here Google&#39;s Webmaster Tools says we should appear on page #38, although I never actually found us.</p></div>
<p>&nbsp;</p>
<p>After remedying most of the obvious SEO missteps documented widely on the internet, our rank was unchanged. For a <em>creator </em>it’s absolutely frightening to feel that the product is out of your control. Thus you might imagine that we were both stunned and worried that somehow the almighty GoogleBot had forgotten about Bindle altogether. Could this be the infamous Panda update in action?</p>
<h3><strong>Loading time</strong></h3>
<p>The Bindle team was left with delving into the smoke-and-mirror optimizations that are far from well-defined. The first course of action was to resolve any issues with our loading times.</p>
<p><a title="Google Webmaster Guidelines" href="http://support.google.com/webmasters/bin/answer.py?hl=en&amp;answer=35769">Google cares about the speed of the internet</a>: Pages need download quickly. We thought the previous load time was fairly fast. Nevertheless the challenge was undertaken and now we fully utilize caches, CDNs, gzip, everything you can think of. Did it fix it? Nope, but the upside is Bindle is faster than ever.</p>
<h3><strong>Duplicate Content</strong></h3>
<p><a title="Duplicate Content -- Google Webmaster Guidelines" href="http://support.google.com/webmasters/bin/answer.py?hl=en&amp;answer=66359">Google doesn’t like it</a> if more than one URL has similar content. The previous iteration of Bindle had invitation links that rendered custom copy depending on the invite, we couldn’t have these invites showing up in the index so <a title="Link rel Canonical" href="http://support.google.com/webmasters/bin/answer.py?hl=en&amp;answer=139394">we needed</a> a <code>link[rel=canonical]</code>, otherwise our friendly gBot might think we’re up to no good.</p>
<p>Bindle has two web accessible servers: a staging &amp; production. We assumed staging to be obfuscated because there were no inbound links, sitemaps or anything that noted its existence. So when we learned our staging server was somehow found and indexed by google it was clear that a staging specific <code>robots.txt</code> was in order.</p>
<p>Did either of these solutions fix it? Nope, but we can rest easy knowing we’ve resolved our duplicate content problem.</p>
<h3><strong>Keyword limit culprit</strong></h3>
<p>We stumbled upon the <a title="Google Keyword Limit" href="http://www.seomoz.org/blog/lessons-learned-by-an-over-optimizer-14730?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed%3A+seomoz+%28SEOmoz+Daily+Blog%29">solution</a> serendipitously. Without informing us of the fact (or why) the gBot had decided we were spamming the keyword “bindle” and that it would be best not show us in the results for any query involving it. Which, obviously, made it kind of hard to find us.</p>
<p>After some fairly mindless deletions of the word “bindle” from the homepage, we achieved the holy grail:</p>
<div id="attachment_628" class="wp-caption aligncenter" style="width: 600px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-03-02-at-11.15.20-AM.png"><img class="size-full wp-image-628" title="Fixed Google Search" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-03-02-at-11.15.20-AM.png" alt="There we are! #6 baby" width="590" height="553" /></a><p class="wp-caption-text">Finally, after some spurious deletions of &#39;bindle&#39; from the homepage, we appear in our rightful spot at #6 in google!</p></div>
]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/615/when-the-panda-strikes-why-you-cant-afford-to-ignore-seo/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bindle redesigned</title>
		<link>http://bindle.me/blog/index.php/588/bindle-redesigned</link>
		<comments>http://bindle.me/blog/index.php/588/bindle-redesigned#comments</comments>
		<pubDate>Mon, 26 Mar 2012 06:49:30 +0000</pubDate>
		<dc:creator>Dominic H. Nguyen</dc:creator>
				<category><![CDATA[Bindle]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=588</guid>
		<description><![CDATA[Every so often in the life cycle of a website it&#8217;s evident that change is necessary to push the product to next level. This time around we&#8217;ve made design changes that refine your experience down to the smallest detail – a big deal. It&#8217;s our pleasure to announce a major refresh that opens the site to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Every so often</strong> in the life cycle of a website it&#8217;s evident that change is necessary to push the product to next level. This time around we&#8217;ve made design changes that refine your experience down to the smallest detail – a big deal. It&#8217;s our pleasure to announce a major refresh that opens the site to all, features a new design, and showcases the improved collage.</p>
<p><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/iconse_open_sign_160x90.gif"><img class="alignleft size-full wp-image-592" title="iconse_open_sign_160x90" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/iconse_open_sign_160x90.gif" alt="" width="160" height="90" /></a></p>
<h4><strong>Open to all</strong></h4>
<p>With the newest updates, we&#8217;ve opened Bindle up to everyone. Share Bindle with your friends, no invitation required!<br />
<br />
<span id="more-588"></span><br />
<a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/iconse_new_160x90.gif"><img class="alignleft size-full wp-image-593" title="iconse_new_160x90" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/iconse_new_160x90.gif" alt="" width="160" height="90" /></a></p>
<h4><strong>New design</strong></h4>
<p>We&#8217;ve made a big leap that streamline the creation process, speed up browsing, and really simplify your experience. Find out what&#8217;s inside a Bindle in a glance with collage thumbnails.<br />
<br />
<a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/iconse_collage2_160x90.jpg"><img class="alignleft size-full wp-image-591" title="iconse_collage2_160x90" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/iconse_collage2_160x90.jpg" alt="" width="160" height="90" /></a></p>
<h4><strong>Collage 2.0</strong></h4>
<p>Thought our collage was cool? We&#8217;ve introduced improvements we&#8217;re sure you won&#8217;t find anywhere else such as magical item-outlining, slideshows, and smart panning.<br />
</p>
<h4><strong>Mobile friendly</strong></h4>
<p>As an added benefit for folks browsing Bindle on their mobile devices and tablets. We&#8217;ve made the site more friendly for any screen size. Take a look for yourself.</p>
<p><em>Ever wanted to touch Bindles on your iPad? Now you can</em>&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/588/bindle-redesigned/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rack Middleware for SEO, fun and profit</title>
		<link>http://bindle.me/blog/index.php/304/rack-middleware-for-seo-fun-and-profit</link>
		<comments>http://bindle.me/blog/index.php/304/rack-middleware-for-seo-fun-and-profit#comments</comments>
		<pubDate>Tue, 20 Mar 2012 00:25:00 +0000</pubDate>
		<dc:creator>Tom Coleman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=304</guid>
		<description><![CDATA[Thank god for Ruby and DSLs. Gone are the days of developers resigned to using apache and having to mess with it&#8217;s arcane configuration syntax. The plethora of webservers used these days combined with simplicity and power of Rack middleware makes for a much simpler life for us rubyists. Thanks to a variety of Rack [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Thank god for Ruby and DSLs</strong>. Gone are the days of developers resigned to using apache and having to mess with it&#8217;s arcane configuration syntax. The plethora of webservers used these days combined with simplicity and power of Rack middleware makes for a much simpler life for us rubyists.</p>
<p>Thanks to a variety of Rack middleware,  Bindle is <em>faster, more efficient, and has better access for the Googlebot.</em> This post will give you some tips to do the same.</p>
<p><span id="more-304"></span></p>
<p>We have a pretty standard setup:</p>
<ul>
<li>Rails 3.1</li>
<li>Ruby 1.9</li>
<li>Heroku Cedar</li>
</ul>
<h4><strong>rack/cache</strong></h4>
<p>Got some assets on your site that are expensive to generate? Thinking about the CPU cost of repeated access? Well worry no more, a reverse proxy cache [<a href="http://bindle.me/blog/index.php/304/rack-middleware-for-seo-fun-and-profit#footnote_0_304" id="identifier_0_304" class="footnote-link footnote-identifier-link" title="I&amp;#8217;m still trying to work out if us Australians, who pronounce it &amp;#8216;kay-shh&amp;#8217; rather than &amp;#8216;cash&amp;#8217; are wrong. I feel we probably are&amp;#8212;it&amp;#8217;s a French word after all.">1</a>] will make that problem disappear. For some examples, consider dragonfly, reverse_proxy and split_assets below. As a bonus: googlebot stays happy when things are fast.</p>
<p>We use memcached to actually keep the cache around, which is <a title="Memcached on Heroku" href="http://devcenter.heroku.com/articles/memcache">easily accomplished on Heroku</a>. Though also Heroku adds <code>rack/cache</code> for you, others may want to do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">use</span> <span style="color:#6666ff; font-weight:bold;">Rack::Cache</span></pre></div></div>

<h4><strong>rack/deflater</strong></h4>
<p><a title="Yahoo performance guidelines" href="http://developer.yahoo.com/performance/rules.html">GZipped assets are good</a>. It&#8217;s really this simple to get it going on your server, simply add:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">insert_before</span> <span style="color:#6666ff; font-weight:bold;">Rack::Cache</span>, <span style="color:#6666ff; font-weight:bold;">Rack::Deflater</span></pre></div></div>

<p>You want the deflater to be outside of the cache, I fear strange things may happen otherwise.</p>
<h4><strong>rack/rewriter</strong></h4>
<p>Task 1: Rails defaults to generating URLs like <strong>http://bindle.me/bindles/1</strong>. However, it&#8217;s possible the Googlebot will find a link like <strong>http://bindle.me/bindles/1/. </strong>Since <a href="http://support.google.com/webmasters/bin/answer.py?hl=en&amp;answer=66359">Google doesn&#8217;t like duplicate content</a>, we need to 301 redirect that trailing slash away.</p>
<p>Task 2: We also employ a URL shortener which redirects requests from <a href="http://bndl.es">bndl.es</a> and <a href="http://www.bindle.me">www.bindle.me</a> to <a href="http://bindle.me">bindle.me</a> to simplify the user experience and retain the google juice in a single domain.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">insert_before</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rack::Lock</span>, <span style="color:#6666ff; font-weight:bold;">Rack::Rewrite</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#008000; font-style:italic;"># anything ending in / to the same without the '/'</span>
  r301 <span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>$<span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'$1'</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># bndl.es/www.bindle.me -&gt; bindle.me (will then go to the shortener)</span>
  r301 <span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'http://bindle.me$&amp;'</span>, :<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>rack_env<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'bndl.es'</span>, <span style="color:#996600;">'www.bindle.me'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>rack_env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'SERVER_NAME'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h4><strong>rack/reverse_proxy</strong></h4>
<p><a href="http://searchengineland.com/how-changes-to-the-way-google-handles-subdomains-impact-seo-12899">PageRank doesn&#8217;t transfer between subdomains particularly well</a>. So instead of hosting your blog at <a href="http://blog.bindle.me">blog.bindle.me</a> it&#8217;s better to use a subdirectory format like <a href="http://bindle.me/blog">bindle.me/blog</a>. However, you might not want to use Rails for your blog; perhaps you prefer WordPress, or you&#8217;d like to use an external site like Tumblr. How do you redirect your blog from a subdomain to a subdirectory behind Heroku? Enter <code>rack/reverse_proxy</code>. Even though we don&#8217;t have access to the webserver, we don&#8217;t have to care thanks to rack.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">insert_after</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rack::Cache</span>, <span style="color:#6666ff; font-weight:bold;">Rack::ReverseProxy</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  reverse_proxy <span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span>^<span style="color:#006600; font-weight:bold;">/</span>blog<span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'http://blog.bindle.me$1'</span>, <span style="color:#ff3333; font-weight:bold;">:preserve_host</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h4><strong>Dragonfly</strong></h4>
<p><a href="http://markevans.github.com/dragonfly/file.Index.html">Dragonfly</a> adopts the philosophy of generating thumbnails on the fly. Bindle generates somewhere along the lines of ten different thumbnails for each image that is uploaded. Instead of worrying about image storage in the cloud, we generate the necessary images on the fly and store them in memory with memcached.</p>
<p>For example this is what we use to integrate Dragonfly into rack:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">insert_after</span> <span style="color:#6666ff; font-weight:bold;">Rack::Cache</span>, <span style="color:#6666ff; font-weight:bold;">Dragonfly::Middleware</span>, <span style="color:#ff3333; font-weight:bold;">:images</span></pre></div></div>

<h4><strong>split_assets</strong></h4>
<p>IE throws up some interesting problems. The split asset endpoint <a title="Splitting the Asset: destroying arcane IE bugs on the ruby rack." href="http://bindle.me/blog/index.php/200/splitting-the-asset-destroying-arcane-ie-bugs-on-the-rails-rack">solved a particularly thorny one for me</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">insert_after</span> <span style="color:#6666ff; font-weight:bold;">Rack::Cache</span>, <span style="color:#996600;">'SplitAssetEndpoint'</span>, <span style="color:#996600;">'/split_assets'</span></pre></div></div>

<p>Any other interesting middleware you&#8217;re using in your projects?</p>
<ol class="footnotes"><li id="footnote_0_304" class="footnote">I&#8217;m still trying to work out if us Australians, who pronounce it &#8216;kay-shh&#8217; rather than &#8216;cash&#8217; are wrong. I feel we probably are&#8212;it&#8217;s a French word after all.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/304/rack-middleware-for-seo-fun-and-profit/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Is SCSS killing your site&#8217;s performance?</title>
		<link>http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance</link>
		<comments>http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance#comments</comments>
		<pubDate>Wed, 14 Mar 2012 02:17:34 +0000</pubDate>
		<dc:creator>Tom Coleman</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=493</guid>
		<description><![CDATA[SCSS and LESS have made waves in the frontend development community. They’ve allowed us to write concise styles in a lean fashion, but is there a hidden performance tax? The fact is preprocessors are used widely at the production level. But what about performance? What comes out is simply CSS, but how does that CSS [...]]]></description>
			<content:encoded><![CDATA[<p><strong>SCSS and LESS have made waves in the frontend development community.</strong> They’ve allowed us to write concise styles in a lean fashion, but is there a hidden performance tax?</p>
<p>The fact is preprocessors are used widely at the production level. But what about performance? What comes out is simply CSS, but how does that CSS compare to what we used to write by hand? How do browsers cope with the reams of machine generated styles?</p>
<p>In this post I’ll investigate how common CSS preprocessing idioms can affect the browser’s <em>rendering</em> performance.</p>
<p><span id="more-493"></span></p>
<h3><strong>The anatomy of a web-request; load times beyond the first hit</strong></h3>
<p>The <a href="http://developer.yahoo.com/performance/rules.html">accepted wisdom</a> recommends combining your assets together into a single large file where possible. Browsers open a limited number of concurrent requests to the server, so given the speed of connections, and the diminutive file size thanks to GZIP, you may as well send as much data as you can on that initial connection [<a href="http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance#footnote_0_493" id="identifier_0_493" class="footnote-link footnote-identifier-link" title="with the benefit of all subsequent request being cached in the browser">1</a>]. Thus, if we&#8217;re to follow this thinking, there are two categories of requests and optimizations that can be made:</p>
<p><strong>Initial requests</strong>: This is well covered ground. Suffice to say you will make more substantial gains ensuring your compression, CDNs, sprites, minification and the like are functioning optimally than worrying about the structure of your CSS.</p>
<p><strong>Subsequent requests: </strong>When you&#8217;ve established a baseline of performance with <em>initial requests</em>, further optimizations will bring you here. Let&#8217;s break down the order of page rendering.</p>
<div id="attachment_555" class="wp-caption aligncenter" style="width: 610px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/dom-profile1.png"><img class="size-full wp-image-555" title="dom-profile" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/dom-profile1.png" alt="" width="600" height="126" /></a><p class="wp-caption-text">In this subsequent request to bindle.me, we see that although the HTML has been returned after 624ms, the DOM is not ready for around 1.5s. Although much of this time is taken parsing JS, it seems that there is scope for gains to made in rendering time.</p></div>
<ol>
<li><strong>Download time:</strong> The browser hits your server up for the URL. Hopefully you are fast enough that the HTML is returned to the browser within a few hundred ms.</li>
<li><strong>Cached? </strong>The browser looks at what assets it needs, apart from a few images that aren’t important to the layout of the page, everything is in cache (0ms).</li>
<li><strong>Parse time: </strong>The browser parses the CSS [<a href="http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance#footnote_1_493" id="identifier_1_493" class="footnote-link footnote-identifier-link" title="and JS if you don&rsquo;t put it at the bottom of the &amp;lt;body&amp;gt;. I&rsquo;ll leave this discussion for another time">2</a>] of the page and is ready to render.</li>
<li><strong>Render time: </strong>The browser renders the HTML, using the CSS.</li>
<li><strong>Ready to go: </strong>The page looks correct (apart from maybe a few images which will slot in later) and is ready to use.</li>
</ol>
<p>&nbsp;</p>
<p>In the <strong>first request</strong>, the amount of waiting is determined mostly by whether assets need to be downloaded. For <strong>subsequent requests</strong>, the <em>parse </em>and <em>render </em>time can take as long, if not longer than <em>download time</em>. So <strong>subsequent requests </strong>are definitely a concern if you want to keep your web app snappy.</p>
<h3><strong>The double edged sword: How SCSS can help &amp; hinder </strong></h3>
<p>At Bindle we have ~47 icons which have 4 states each. Each of these combinations requires it’s own style rule like so:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #3333ff;">:active</span><span style="color: #6666ff;">.close</span><span style="color: #6666ff;">.icon16</span><span style="color: #3333ff;">:before </span><span style="color: #00AA00;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-425px</span> <span style="color: #933;">-34px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>47*4 = 188 styles at the very least. Of course it’s not that simple.</p>
<p>We need <code>.close.icon16:before</code> to take a different state depending on whether it’s a link or a button, which state it’s in (<code>:hover, :active, :focus</code>), whether it has class <code>inverse</code> or <code>checked</code> or <code>current</code>, if it’s in the <code>#sidebar</code> etc etc&#8230;  We generate ~1000 icon rules, each of which is a single background-position rule [<a href="http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance#footnote_2_493" id="identifier_2_493" class="footnote-link footnote-identifier-link" title="Thanks Mozilla! We could cut this down to about 50 if you supported background-position-y.">3</a>]. This kind of bloat would not have been possible without CSS preprocessors.</p>
<p>Believe it or not, we had more like 3000 at one point. We’ve pared it down since, but should we add more icons or states we may be overwhelmed. This was not the goal of CSS at it&#8217;s inception as evidenced by IE&#8217;s <a title="Splitting the Asset: destroying arcane IE bugs on the ruby rack." href="http://bindle.me/blog/index.php/200/splitting-the-asset-destroying-arcane-ie-bugs-on-the-rails-rack">lingering problem with too many styles</a>, and obviously it adds to the stylesheet&#8217;s size, but how much does it affect CSS rendering time?</p>
<h3><strong>Aside 1: <em><strong> ‘Ideal’ HTML + CSS</strong></em></strong></h3>
<p>Purists say that we should separate our form from function. CSS was originally developed in part to help us say what we mean in the HTML and to specify what it looks like in the stylesheet. This step enabled us to go from:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a font-face=&quot;Helvetica&quot; color=&quot;red&gt;</pre></div></div>

<p>To</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a class=&quot;fancy red&quot;&gt;</pre></div></div>

<p>A massive improvement. However, it isn’t very semantic to say “This &lt;a&gt; is red and fancy”. It’s better to say “This &lt;a&gt; is a call to action”:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a class=&quot;call-to-action&quot;&gt;</pre></div></div>

<p>And to make the <code>.call-to-action</code> class red and fancy:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.call-to-action</span> <span style="color: #00AA00;">&#123;</span>
  // CSS preprocessors let us do this<span style="color: #00AA00;">;</span> now we are getting somewhere.
  <span style="color: #a1a100;">@include red;</span>
  <span style="color: #a1a100;">@include fancy;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Is this the correct solution though? Is the HTML the right place to be saying this particular &lt;a&gt; is the CTA on this page? Isn’t this a style issue that may change? (Perhaps we have different priority on different @media devices?). Isn’t it better to leave the &lt;a&gt; clean and say:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#sidebar</span> a<span style="color: #3333ff;">:first-child </span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #a1a100;">@include call-to-action;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>“All first links in the sidebar are call-to-action”. Then we can get specific, and override it on the <code>users#show</code> page [<a href="http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance#footnote_3_493" id="identifier_3_493" class="footnote-link footnote-identifier-link" title="Here and in future I use the syntax provided by edifice for namespacing page-level CSS">4</a>]:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.c_users</span><span style="color: #6666ff;">.v_show</span> <span style="color: #cc00cc;">#sidebar</span> a<span style="color: #3333ff;">:last-child </span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #a1a100;">@include call-to-action;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>In this post we will see that although this style of CSS may be &#8216;more progressive&#8217;; it isn’t a habit you want to form and will hurt performance if you&#8217;re careless.</p>
<h3><strong>Aside 2: <em>How browsers parse and render CSS</em></strong></h3>
<p><em>Disclaimer: I am not an expert on this; and perhaps it may change in the future.</em></p>
<blockquote><p><em>Browsers parse CSS from right to left</em></p></blockquote>
<p>The ‘parsing’ step of CSS rendering involves constructing a massive hash table of right-hand-side(RHS) selectors. So when a browser sees:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.c_users</span><span style="color: #6666ff;">.v_show</span> <span style="color: #cc00cc;">#sidebar</span> a <span style="color: #00AA00;">&#123;</span> ... <span style="color: #00AA00;">&#125;</span> //<span style="color: #00AA00;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#93;</span></pre></div></div>

<p>It knows that this rule will only apply to <code>&lt;a&gt;</code> elements, and no others.</p>
<p>When rendering, the browser runs through elements one at a time, testing all such rules that match. So every time it finds a link element, it must check to see if it is a descendant of a <code>#sidebar</code> inside a <code>.c_users.v_show</code>. This is a relatively expensive check as it requires running up the whole DOM tree; so we can see that the right-hand side selector is very important.</p>
<p>So every time you construct a rule that has a overly generic selector on the RHS (an &lt;<code>a&gt;</code> rather than a <code>.call-to-action</code> say), you add an extra check that must be made for <em>every</em> single element on the page that matches.</p>
<h3><strong>Quantifying Rendering time</strong></h3>
<p>In an effort to try and quantify this CSS rendering time I’ve constructed a set of experiments that measure the parsing + rendering time for a very large test page. These should give you an idea of what to expect, and how much credence you should put into worrying about parsing.</p>
<p>Read the source code to the <a title="CSS timings github project." href="https://github.com/tmeasday/css_timings">css-timings project</a> and <a title="CSS Timings project." href="http://css-timings.herokuapp.com/">give it a try</a>. I ran these experiments on my late 2011 Macbook Pro and my iPhone 4 [<a href="http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance#footnote_4_493" id="identifier_4_493" class="footnote-link footnote-identifier-link" title="Note that these experiments are very CPU bound. On Dom&rsquo;s early 2010 Macbook pro (18 months older), we saw pretty much 2x times for more-or-less everything. Moore&rsquo;s Law in action, baby!">5</a>] and recorded the results in a <a href="https://docs.google.com/spreadsheet/ccc?key=0AkKItN-QIq6WdFRlRU9QZlc4Y3phc0tycjdRbkh6NWc">public google doc</a>. I’ll explain the setup here and talk through the significance of the results in a moment.</p>
<p>The basic experiment is as follows; our page consists of 10,000 <code>&lt;div&gt;</code>s, each with a unique class name. Our base CSS file consists of one simple style targeted to each of the <code>&lt;div&gt;</code>s (so 10,000 base styles as well).</p>
<h3><strong>Experiment One: How long does it all take?</strong></h3>
<p>10,000 elements on one hand is a lot. <a href="http://www.stevesouders.com/blog/2009/03/10/performance-impact-of-css-selectors/">Steve Souders</a> measured the number of selectors on some popular sites and averaged around 1,000 back in 2009. I’d probably estimate that this has grown by now. However it helps us emphasize differences.</p>
<p>10,000 style rules on the other hand isn’t that many. We (Bindle) have around 6,000 selectors right now, and most have many more than 1 rule.</p>
<div id="attachment_548" class="wp-caption aligncenter" style="width: 610px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-03-12-at-9.00.06-PM.png"><img class="size-full wp-image-548" title="Rendering time vs number of HTML elements, iPhone 4" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-03-12-at-9.00.06-PM.png" alt="" width="600" height="363" /></a><p class="wp-caption-text">There is a fairly linear relationship between the number of elements of the page and the rendering time when there are many potentially applicable style rules per element.</p></div>
<p>Anyway, given those caveats, parsing &amp; rendering these elements takes a non-trivial but probably insignificant amount of time on my high-end laptop; however, the rendering time on Mobile Safari is much more noteworthy.</p>
<p><strong>Conclusion #1: <em>Mobile Devices are slow enough that this stuff actually matters.</em></strong></p>
<h3><strong>Experiment Two: What difference do inapplicable styles make?</strong></h3>
<div id="attachment_542" class="wp-caption aligncenter" style="width: 610px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-03-07-at-1.46.58-PM.png"><img class="size-full wp-image-542" title="Graph of Timings for CSS rendering" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/Screen-Shot-2012-03-07-at-1.46.58-PM.png" alt="" width="600" height="164" /></a><p class="wp-caption-text">All browsers seem to have a similar profile; they suffer greatly with overly generic RHS selectors.</p></div>
<p>You’ve combined all your CSS files into one big file, so when rendering each page of the site the browser must consider the styles which are targeted at all your other pages. Not to worry, you use a nice CSS namespacing library [<a href="http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance#footnote_5_493" id="identifier_5_493" class="footnote-link footnote-identifier-link" title="Say, edifice again?">6</a>], so there’s no danger of it getting confused. What does checking all those extra styles cost?</p>
<p>Here we experiment with adding an extra 10,000 irrelevant style rules. Firstly we can try to add styles that the browser knows won’t apply, either because they target the wrong element, or because they target a non-existent class. We see that they make a linear difference to CSS parsing time&#8212;parsing twice as many styles takes twice as long, surprisingly enough&#8212;and no difference to rendering time. Well done browser!</p>
<p>On the other hand, if the styles don’t apply, but do match on the RHS, for instance like:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#bla</span> <span style="color: #6666ff;">.foo</span> ul <span style="color: #6666ff;">.something-else</span> div</pre></div></div>

<p>The browser is forced to do many expensive ancestor checks, and the rendering time blows way out (even in the seconds for Firefox on my Pro).</p>
<p><strong>Conclusion #2: <em>Try to right-most target classes where possible.</em></strong></p>
<p>This, along with the more pressing issue of CSS file size, is a nail in the coffin for the idea of ‘replacing’ classes with <code>@mixins</code> and writing super-semantic html. </p>
<h3><strong>Experiment Three: can we hide irrelevant rules from the browser?</strong></h3>
<p>Seeing as your stylesheet is no doubt full of rules that you know are never going to apply to the page being rendered, you can’t help but wish there way some way to tell the browser not to worry about them. We understand the reason why browsers look at rules right-to-left, but is there something that we can put on the LHS to help the browser out?</p>
<p>Unfortunately, even if you explicitly tell the browser that the body needs a specific class for the rule to apply:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">body<span style="color: #6666ff;">.c_users</span><span style="color: #6666ff;">.v_show</span> <span style="color: #cc00cc;">#sidebar</span> <span style="color: #00AA00;">&gt;</span> <span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span> .. <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Browsers aren’t smart enough to work in such a left-to-right fashion [<a href="http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance#footnote_6_493" id="identifier_6_493" class="footnote-link footnote-identifier-link" title="It could work though. There can only be one body in the page, so if the body doesn&amp;#8217;t have the relevant classes, we can &amp;#8216;turn that rule off&amp;#8217;.">7</a>]. One trick that does work is to use a @media query:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@media-type print {</span>
  <span style="color: #cc00cc;">#sidebar</span> <span style="color: #00AA00;">&gt;</span> <span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span> .. <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Browsers do disregard those rules entirely. However, there’s no ‘custom’ media queries available, so we cannot do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@media-page v_users_c_show {</span>
  <span style="color: #cc00cc;">#sidebar</span> <span style="color: #00AA00;">&gt;</span> <span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span> .. <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><strong>Conclusion #3: <em>At this point, there is no good way to hide styles. So we need to be careful.</em></strong></p>
<h3><strong>Conclusion: CSS efficiency <em>can</em> have an impact on mobile; but only if you do things really wrong.</strong></h3>
<p>So, after all this, what can we conclude? Firstly, hopefully the css-timings project can provide a nice base for people to conduct their own experiments and try to measure the impact of various other CSS quirks they may be thinking of squashing. Please feel free to criticize my methodology and conclusions to your hearts content!</p>
<p>Secondly, poor CSS will hurt you on mobile. The amount any one mistake can hurt you, though small, will add up with systemic errors like global selector * rules:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.foo</span> <span style="color: #cc00cc;">#bar</span> <span style="color: #00AA00;">&gt;</span> <span style="color: #00AA00;">*</span></pre></div></div>

<p>Each will add 0.15ms to the rendering time of <em>every single</em> page on your entire website [<a href="http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance#footnote_7_493" id="identifier_7_493" class="footnote-link footnote-identifier-link" title="Where did I get 0.15ms from? We saw that 10k over-general selectors adding on 10k elements blew the time out by 15s. So, given that&amp;#8217;s 100 million &amp;#8216;checks&amp;#8217;, each check takes around 0.00015ms. If we have an average sized page (one thousand elements), then each * selector will cause 1000 checks, and thus take around 0.15ms. YMMV">8</a>]. So don&#8217;t worry about a single rule. However, systemic use will lead to a constant rendering tax that will adversely effect the user experience.</p>
<p>So, I would advise:</p>
<ol>
<li><strong></strong>Steer clear of replacing classes with <code>@mixins</code>. One day the browsers will support it or something like it, but until then, <code>
<div class=”call-to-action”></code> isn’t such a horrible mixture of presentation and content.</li>
<li><strong></strong>Keep your number of selectors down, but don’t go overboard about it. This post should hopefully give you an idea of you are dealing with.</li>
</ol>
<ol class="footnotes"><li id="footnote_0_493" class="footnote">with the benefit of all subsequent request being cached in the browser</li><li id="footnote_1_493" class="footnote">and JS if you don’t put it at the bottom of the &lt;body&gt;. I’ll leave this discussion for another time</li><li id="footnote_2_493" class="footnote">Thanks Mozilla! We could cut this down to about 50 if you supported background-position-y.</li><li id="footnote_3_493" class="footnote">Here and in future I use the syntax provided by <a title="Edifice for Rails" href="http://edifice-rails.com/">edifice</a> for namespacing page-level CSS</li><li id="footnote_4_493" class="footnote">Note that these experiments are very CPU bound. On Dom’s early 2010 Macbook pro (18 months older), we saw pretty much 2x times for more-or-less everything. Moore’s Law in action, baby!</li><li id="footnote_5_493" class="footnote">Say, <a href="http://edifice-rails.com">edifice</a> again?</li><li id="footnote_6_493" class="footnote">It <em>could</em> work though. There can only be one body in the page, so if the body doesn&#8217;t have the relevant classes, we can &#8216;turn that rule off&#8217;.</li><li id="footnote_7_493" class="footnote">Where did I get 0.15ms from? We saw that 10k over-general selectors adding on 10k elements blew the time out by 15s. So, given that&#8217;s 100 million &#8216;checks&#8217;, each check takes around 0.00015ms. If we have an average sized page (one thousand elements), then each <code>*</code> selector will cause 1000 checks, and thus take around 0.15ms. YMMV</li></ol>]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/493/is-scss-killing-your-sites-performance/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Running binaries on Heroku</title>
		<link>http://bindle.me/blog/index.php/405/running-binaries-on-heroku</link>
		<comments>http://bindle.me/blog/index.php/405/running-binaries-on-heroku#comments</comments>
		<pubDate>Mon, 12 Mar 2012 02:22:30 +0000</pubDate>
		<dc:creator>Tom Coleman</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[heroku]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=405</guid>
		<description><![CDATA[Bindle 2 is fresh out of the oven; one of the main areas of focus was making Bindles more intuitive to create &#38; navigate&#8212;to that end we&#8217;re making use of a binary called Autotrace which helps us calculate the boundaries of items [1].  Unfortunately there is no ruby interface for this library in rails just yet, so we [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_503" class="wp-caption aligncenter" style="width: 417px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/autotrace.png"><img class="size-full wp-image-503" title="autotrace" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/03/autotrace.png" alt="" width="407" height="280" /></a><p class="wp-caption-text">Blue highlight courtesy of Autotrace</p></div>
<p>Bindle 2 is fresh out of the oven; one of the main areas of focus was making Bindles more intuitive to create &amp; navigate&#8212;to that end we&#8217;re making use of a binary called <a href="http://autotrace.sourceforge.net/">Autotrace</a> which helps us calculate the boundaries of items [<a href="http://bindle.me/blog/index.php/405/running-binaries-on-heroku#footnote_0_405" id="identifier_0_405" class="footnote-link footnote-identifier-link" title="This makes dragging items more intuitive: click on the item to drag it, click on the negative space to pan. So no more rectangles to confine your images!">1</a>].  Unfortunately there is no ruby interface for this library in rails just yet, so we needed to get a binary running on our Heroku Cedar instance. Luckily it was pretty easy to do.</p>
<p>Here&#8217;s how to use binaries with Rails on Heroku:</p>
<p><span id="more-405"></span></p>
<p>Running binaries on heroku is possible if they are in the <code>bin/</code> directory of your app. This is not officially supported, so use at your own risk. Heroku runs on Ubuntu 10.04 LTS 64bit, so you&#8217;ll want to <a href="http://www.ubuntu.com/download/ubuntu/download">download it</a> to compile your program on a VM. Install Ubuntu as normal, then download the source code for the program you are interested in.</p>
<p>If your program doesn&#8217;t have any dependencies which aren&#8217;t already installed on ubuntu (or only depend on header files) you are probably good to go with the normal compilation process. If, like autotrace, it depends on libraries which don&#8217;t come installed on Ubuntu, then they won&#8217;t be on your Heroku instance, and you&#8217;ll need to compile a statically linked version.</p>
<p>Don&#8217;t worry, it&#8217;s not super difficult. Here&#8217;s what I did:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># download and unpack source into a directory and cd in there:</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> autotrace-0.31.1<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># install ubuntu packages which the program requires</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libmagick++-dev
&nbsp;
<span style="color: #666666; font-style: italic;"># configure to be statically linked</span>
.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--enable-shared</span>=no
&nbsp;
<span style="color: #666666; font-style: italic;"># compile</span>
<span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<p>The <code>make</code> process creates an executable, which you can then place into the <code>bin/</code> directory of your heroku app. I put it in the <code>bin/x86_64-linux/</code> directory just to be clear about what can run where. Statically linked <code>autotrace</code> comes in at around 400k, so in order to keep your slug size down you&#8217;ll probably want to use your discretion here.</p>
<p>Finally, to test, you can open up a heroku console (<code>heroku run console</code>) and check that it is running successfully:</p>
<pre>irb(main):006:0> `bin/x86_64-linux/autotrace`
Usage: bin/x86_64-linux/autotrace [options] .
(Missing .)
For more information, use ``-help''.</pre>
<p>Success!</p>
<ol class="footnotes"><li id="footnote_0_405" class="footnote">This makes dragging items more intuitive: click on the item to drag it, click on the negative space to pan. So no more rectangles to confine your images!</li></ol>]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/405/running-binaries-on-heroku/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Choosing in context: product recommendation the Bindle way</title>
		<link>http://bindle.me/blog/index.php/419/choosing-in-context-product-recommendation-the-bindle-way</link>
		<comments>http://bindle.me/blog/index.php/419/choosing-in-context-product-recommendation-the-bindle-way#comments</comments>
		<pubDate>Tue, 28 Feb 2012 17:18:01 +0000</pubDate>
		<dc:creator>Tom Coleman</dc:creator>
				<category><![CDATA[Bindle]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=419</guid>
		<description><![CDATA[The internet knows no horizon.  It is the most visible example of technology&#8217;s unstoppable progress and is so vast that we couldn&#8217;t cope if we had to use pre-Google search engines. Google&#8217;s Page Rank algorithm admirably filters signal from noise, but the imminent commoditization of production and the consequent explosion choice presents a new challenge: will an algorithm be able [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/02/collaborative_filtering.jpg"><img class="aligncenter size-full wp-image-480" title="collaborative_filtering" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/02/collaborative_filtering.jpg" alt="" width="600" height="400" /></a>The internet knows no horizon.  </strong>It is the most visible example of <a href="http://www.kurzweilai.net/the-law-of-accelerating-returns">technology&#8217;s unstoppable progress</a> and is so vast that we couldn&#8217;t cope if we had to use pre-Google search engines. Google&#8217;s Page Rank algorithm admirably filters signal from noise, but the imminent <a href="http://www.wired.com/magazine/2010/01/ff_newrevolution/all/1">commoditization of production</a> <em>and</em> the consequent explosion choice presents a new challenge: <em>will an algorithm be able to make our purchasing decisions for us?</em></p>
<p>Dom <a title="Chances of discovery: one in a billion" href="http://bindle.me/blog/index.php/170/chances-of-discovery-one-in-a-billion">explained the problem in an earlier post:</a></p>
<blockquote><p>&#8230;the major challenge for new companies will not be in sales or manufacturing, but bringing their goods to the attention of buyers. The challenge for the consumer will be focusing their attention on the <strong><em>right goods</em></strong><em>. </em>When everyone’s mom, sister, and brother is a producer, we’ll need to filter the deluge of brand messaging else expect to washed away in the mass of creativity.</p></blockquote>
<p>In this post, I&#8217;ll begin to explore the solution.<span id="more-419"></span></p>
<h4><strong>Collaborative Filtering</strong></h4>
<p>The classic computer algorithm for the problem of recommendation is a technique called Collaborative Filtering. It&#8217;s the formalisation of a simple idea; if we know people&#8217;s past purchasing behaviour (including yours), then we should look at what&#8217;s popular with people whose purchases mirror yours. The amount that you are likely to like an item X is the average of how much everyone else likes X, weighted by their similarity to you.</p>
<p>This technique might seem very crude, but like many data-mining algorithms, predicts your preferences quite well due to the massiveness of the datasets involved. <a href="http://http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=1167344">Amazon famously implemented</a> a &#8216;reverse&#8217; version of it (using users&#8217; purchasing patterns to link similar items) to produce the &#8216;Customers who bought X also bought Y&#8217; section of their website.</p>
<p>There&#8217;s no doubt this approach may lead us to discover interesting new products, or prompt us to buy something that we&#8217;ve forgotten we wanted. However, <a href="http://pinterest.com/">discovery</a> is <a href="https://svpply.com/">becoming</a> <a href="http://bindle.me/">easier</a> as the long tail of products comes to the fore. Does the algorithm really help us to decide which product is best for us? Does it give us confidence that we are looking at the perfect choice? Can a machine help us <a title="Why Bindle?" href="http://bindle.me/blog/index.php/9/why-bindle">own the right stuff</a>?</p>
<p><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/02/user_reviews.jpg"><img class="aligncenter size-full wp-image-481" title="user_reviews" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/02/user_reviews.jpg" alt="" width="350" height="260" /></a></p>
<h4><strong>The User Review</strong></h4>
<p>A human alternative to a machine recommendation is a user review. Pioneered by <a href="http://www.consumerreports.org/">Consumer Reports</a> and deftly wielded by the Amazons and Yelps of the world, the user reviews&#8217; achilles heel lies in it&#8217;s propensity to polarize opinion. People review products if they love or hate them [<a href="http://bindle.me/blog/index.php/419/choosing-in-context-product-recommendation-the-bindle-way#footnote_0_419" id="identifier_0_419" class="footnote-link footnote-identifier-link" title="they also tend to love or hate things more than they should">1</a>], but rarely if they are &#8216;just OK&#8217;. Contrary to what it appears online, it&#8217;s the silent majority between love and hate that actually BUY things.</p>
<p>Moreover, out of context, it can be very difficult to tell if someone&#8217;s opinion can be trusted. Try this experiment: think of your favourite product, find it on Amazon, and have a look at the negative reviews (<a href="http://www.imdb.com/title/tt0110912/reviews?filter=hate">there&#8217;ll</a> <a href="http://gdgt.com/apple/iphone/4s/reviews/gpi/">be</a> <a href="http://www.urbanspoon.com/u/reviews/1988271">some</a>!). Would you know, uninitiated, that those reviewers were dead wrong? Or at least that their opinions were baseless? Product reviews can range from nitpicky rambling loud noise to compelling argument.</p>
<h4><strong>Trust</strong></h4>
<p>The issue in both cases is trust. It&#8217;s hard to trust the computer, because we don&#8217;t <em>know what it&#8217;s thinking</em>. This is less of a problem when we are <a title="Netflix" href="http://netflix.com">deciding on a movie</a>, but when the outlay of money is higher, it becomes a more serious issue. Likewise, when we read a review, we generally just don&#8217;t have enough of a handle on the reviewer to know if we can trust them.</p>
<p>The solution to the trust issue online is simple: context. If we know a user&#8217;s pattern of behaviour, it&#8217;s easy to understand the significance of a single act. The idea of a bindle is to provide that context with a single glance. It&#8217;s the easiest way to see in a moment how the recommender&#8217;s choice of a product fits within the general pattern of their choices.</p>
<p>&nbsp;</p>
<div class="wp-caption aligncenter"><iframe src="http://bindle.me/bindles/329/embed?no_links=true&amp;size=szlarge" frameborder="0" marginwidth="0" marginheight="0" width="600" height="496"></iframe></p>
<p class="wp-caption-text">Seeing the kind of bikes those wheels are used on might make you realise they are pretty pro.</p>
</div>
<p>&nbsp;</p>
<div class="wp-caption aligncenter"><iframe src="http://bindle.me/bindles/275/embed?no_links=true&amp;size=szlarge" frameborder="0" marginwidth="0" marginheight="0" width="600" height="496"></iframe></p>
<p class="wp-caption-text">Seeing this Apple-device user with a Nexus One tells you something about that particular phone.</p>
</div>
<p>The advent of the internet has dragged us into a post information age; attention is the new scarcity. The latest trends on the web revolve around curating the firehose of information that is available.</p>
<p>Bindle is an effort to enable us to trust other gear lovers, driving that trust by allowing us to see someone&#8217;s choices in the context they have been made. Everyone has friends who they trust for their excellent sense of style, or technical knowledge, or experience in some area. I know there are people who&#8217;s recommendations I would blindly buy without further thought. Bindle will enable this behaviour <em>on an internet scale</em>.</p>
<p>We hope that in the future, with Bindle, we will all <em>own the right things</em>, thanks to the recommendations of those we trust.</p>
<ol class="footnotes"><li id="footnote_0_419" class="footnote">they also tend to love or hate things <a href="http://en.wikipedia.org/wiki/Confirmation_bias#Polarization_of_opinion">more than they should</a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/419/choosing-in-context-product-recommendation-the-bindle-way/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Splitting the Asset: destroying arcane IE bugs on the ruby rack.</title>
		<link>http://bindle.me/blog/index.php/200/splitting-the-asset-destroying-arcane-ie-bugs-on-the-rails-rack</link>
		<comments>http://bindle.me/blog/index.php/200/splitting-the-asset-destroying-arcane-ie-bugs-on-the-rails-rack#comments</comments>
		<pubDate>Wed, 08 Feb 2012 00:34:35 +0000</pubDate>
		<dc:creator>Tom Coleman</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://bindle.me/blog/?p=200</guid>
		<description><![CDATA[TESTING AGAINST INTERNET EXPLORER IS NEVER FUN. That’s an understatement. Even though at Bindle we limit our support to IE9, we still fear the obscure IE bug. It leads to an aversion to booting up a Windows 7 VM. jQuery + Compass have (greatly) reduced the worries over IE quirks, but from time to time I am still left [...]]]></description>
			<content:encoded><![CDATA[<p><strong>TESTING AGAINST INTERNET EXPLORER IS NEVER FUN.</strong> That’s an understatement.</p>
<p>Even though at <a href="http://bindle.me">Bindle</a> we limit our support to IE9, we still fear the obscure IE bug. It leads to an aversion to booting up a Windows 7 VM. <a href="http://jquery.com/">jQuery</a> + <a href="http://compass-style.org/">Compass</a> have (greatly) reduced the worries over IE quirks, but from time to time I am still left shaking my head.</p>
<p>These days I find I can get away with deferring IE testing until towards the end of a release process, as most of the time all we need to do is double check things look and behave right. So imagine my surprise when seeing this while loading our latest <a title="Introducing a better Bindle.me" href="http://bindle.me/blog/index.php/120/introducing-a-new-bindle-me">release</a>:<span id="more-200"></span></p>
<div class="mceTemp mceIEcenter">
<div id="attachment_216" class="wp-caption aligncenter" style="width: 624px"><a href="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/01/Screen-Shot-2012-01-07-at-12.43.25-PM.png"><img class=" wp-image-216 " title="IE-Split-Asset-Problem" src="http://bindle-blog-images.s3.amazonaws.com/wp-content/uploads/2012/01/Screen-Shot-2012-01-07-at-12.43.25-PM-1024x737.png" alt="" width="614" height="442" /></a><p class="wp-caption-text">Bindle doesn&#39;t look very good when IE isn&#39;t interpreting all of the stylesheets.</p></div>
</div>
<p>As you can see the majority of our stylesheets are not being interpreted. It <a href="http://blogs.msdn.com/b/ieinternals/archive/2011/05/14/internet-explorer-stylesheet-rule-selector-import-sheet-limit-maximum.aspx">turns out</a> the problem is due to some internal limitations in IE, versions up to 9. Such browsers allow:</p>
<ul>
<li>up to 4095 selectors per stylesheet [this is the one that was causing us the problem].</li>
<li>up to 31 <code>@import</code>ed sheets</li>
<li><code>@import</code> nesting up to 4 levels deep.</li>
</ul>
<p>Why in this day and age do we have such a limit, you ask? Eric explains:</p>
<blockquote><p>The root of the limitations is that Internet Explorer uses a 32bit integer to identify, sort, and apply the cascading rules. The integer’s 32bits are split into five fields: four sheetIDs of 5 bits each, and one 12bit ruleID. The 5 bit sheetIDs results in the 31 @import limit, and the 12 bit ruleID results in the 4095 rules-per-sheet limitation.</p></blockquote>
<p><a title="Chris Wilson's personal site" href="http://cwilso.com/">Chris Wilson</a> comments that the feature was implemented in IE3 (released 1996), when keeping the memory requirement per stylesheet rule was <em>a little bit</em> more important.</p>
<h4><strong>Dealing with the problem</strong></h4>
<p>The specific issue is that Bindle has more than just 4095 selectors. The solution is to split the stylesheet up (for IE only), and to serve up chunks of it selectively to IE. So you&#8217;ll see in our <code>&lt;head&gt;</code> tag, something like:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">    &lt;link href=&quot;/assets/screen.css?body=1&quot; media=&quot;screen&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
    &lt;!--[if IE]&gt;&lt;link href=&quot;/split_assets/screen-2.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot;&gt;&lt;![endif]--&gt;</pre></div></div>

<p>The standard sprocketed stylesheet and a second, IE only stylesheet (containing the remaining rules from our stylesheet that it ignores).</p>
<p>How to work producing this <code>screen-2.css</code> into our Rails 3.1 / git / heroku workflow? The nicest solution is <a title="Bless CSS splitter" href="http://blesscss.com/">Bless</a>, a node.js project that splits CSS files via a command line program, even adding @import statements to your first file to ensure you don&#8217;t need to change your html code at all. However, this solution isn&#8217;t practical when you are deploying to heroku, with it&#8217;s read-only file system, and git push style deployment. (If you are deploying to your own server via capistrano, I would advise following this route). </p>
<p>So what is a poor heroku based developer to do? I found a simple little <a href="https://gist.github.com/1131536">CSS splitter ruby class</a> which I tried to work into the sprockets assets workflow, so that the CSS files would get split on <code>rake assets:precompile</code>; however (not being a sprockets expert) it seemed that turning one file into multiple wasn&#8217;t part of the basic sprockets plan. Another possibility would be to hook somewhere else into the assets:precompile task, however I need my code to run after <code>assets:precompile</code> (and thus the css files generated), and I don&#8217;t know how to ensure that [<a href="http://bindle.me/blog/index.php/200/splitting-the-asset-destroying-arcane-ie-bugs-on-the-rails-rack#footnote_0_200" id="identifier_0_200" class="footnote-link footnote-identifier-link" title="rake.enhance should work, but doesn&amp;#8217;t seem to hook in on Heroku. I guess it&amp;#8217;s the special way they run assets:precompile. I would welcome suggestions about how to do this in the comments">1</a>].</p>
<p><b>UPDATE:</b> Zweilove have released this class as a <a href="https://github.com/zweilove/css_splitter">gem</a>, and have a workaround for the multiple-files problem, so it&#8217;s now possible to integrate it into the asset workflow.</p>
<h4><strong>A solution: rack middleware</strong></h4>
<p>Although working harder through one of the solutions outlined above would probably be the <em>ideal</em> way to do it, I secretly didn&#8217;t really want to, as I was itching for a chance to implement a <a href="http://amberbit.com/blog/introduction-to-rack-middleware">rack middleware</a>. I had previously had to dig around in middleware when solving an <a title="The Scariest Bug" href="http://bindle.me/blog/index.php/22/the-scariest-bug">extremely scary bug</a> that I encountered. I was keen to have a go at one of my own. Here&#8217;s the end result:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'css_splitter'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> SplitAssetEndpoint
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>app, path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@app</span> = app
    <span style="color:#0066ff; font-weight:bold;">@path</span> = path
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> call<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'PATH_INFO'</span><span style="color:#006600; font-weight:bold;">&#93;</span> =~ <span style="color:#CC00FF; font-weight:bold;">Regexp</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'^'</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#0066ff; font-weight:bold;">@path</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/([^/]*)<span style="color:#000099;">\-</span>(<span style="color:#000099;">\d</span>+).css'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      filename = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>::Rails.<span style="color:#9900CC;">public_path</span>, ::Rails.<span style="color:#9900CC;">application</span>.<span style="color:#9900CC;">config</span>.<span style="color:#9900CC;">assets</span>.<span style="color:#9900CC;">prefix</span>, $1 <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'.css'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      css = CssSplitter.<span style="color:#9900CC;">split_to_part</span><span style="color:#006600; font-weight:bold;">&#40;</span>filename, $2.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">200</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#996600;">'Content-Type'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'text/css'</span>,
        <span style="color:#996600;">'Content-Size'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> css.<span style="color:#9900CC;">bytesize</span>.<span style="color:#9900CC;">to_s</span>,
        <span style="color:#996600;">'Cache-Control'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'public, max-age=31536000'</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span>css<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#0066ff; font-weight:bold;">@app</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Here&#8217;s the <a href="https://gist.github.com/1573516">modifications I made to the css splitter library</a> to allow me to extract a single part of the file. The middleware itself is pretty simple. We simply look at the incoming request, and if it&#8217;s path matches (<tt>/split_assets/</tt> in our case), it parses out the part you want, pulls it out of the CSS asset, and serves it back to you. Otherwise it simply passes the request through to the remainder of the stack.</p>
<p>Doesn&#8217;t this use a lot of resources parse + split the CSS every time an IE user accesses the site? Well, no, thanks to the magic of HTTP caching. We simply make sure that we place the <tt>SplitAssetEndpoint</tt> behind the Cache, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">insert</span> insert_after <span style="color:#6666ff; font-weight:bold;">Rack::Cache</span>, <span style="color:#996600;">'SplitAssetEndpoint'</span>, <span style="color:#996600;">'/split_assets'</span></pre></div></div>

<p>Problem solved!</p>
<ol class="footnotes"><li id="footnote_0_200" class="footnote"><code>rake.enhance</code> should work, but doesn&#8217;t seem to hook in on Heroku. I guess it&#8217;s the special way they run <code>assets:precompile</code>. I would welcome suggestions about how to do this in the comments</li></ol>]]></content:encoded>
			<wfw:commentRss>http://bindle.me/blog/index.php/200/splitting-the-asset-destroying-arcane-ie-bugs-on-the-rails-rack/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
