<?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>Why Can&#039;t You Code? &#187; yslow</title>
	<atom:link href="http://www.whycantyoucode.com/tag/yslow/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.whycantyoucode.com</link>
	<description>The highest form of ignorance is when you reject something you don&#039;t know anything about.</description>
	<lastBuildDate>Thu, 25 Feb 2010 14:39:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to improve your YSlow &#8220;Add Expires Header&#8221; score in ASP.NET using T4 templates</title>
		<link>http://www.whycantyoucode.com/2010/02/how-to-improve-your-yslow-add-expires-header-score-in-asp-net-using-t4-templates/</link>
		<comments>http://www.whycantyoucode.com/2010/02/how-to-improve-your-yslow-add-expires-header-score-in-asp-net-using-t4-templates/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 14:28:51 +0000</pubDate>
		<dc:creator>Andrei Alecu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Performance Optimization]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[t4]]></category>
		<category><![CDATA[yslow]]></category>

		<guid isPermaLink="false">http://www.whycantyoucode.com/?p=143</guid>
		<description><![CDATA[If you care about your website performance, I’m sure you must know about the YSlow addin for Firebug. One of its recommendations is to add an Expires header with a far future expiration date for your static content. You can read more about it here. If you use CSS for your layout (which you should) [...]]]></description>
			<content:encoded><![CDATA[<p>If you care about your website performance, I’m sure you must know about the <a href="http://developer.yahoo.com/yslow/" target="_blank">YSlow addin for Firebug</a>.</p>
<p>One of its recommendations is to add an Expires header with a far future expiration date for your static content. You can read more about it <a href="http://developer.yahoo.com/performance/rules.html#expires" target="_blank">here</a>.</p>
<p><a href="http://www.whycantyoucode.com/content/uploads/2010/02/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.whycantyoucode.com/content/uploads/2010/02/image_thumb.png" width="744" height="203" /></a></p>
<p>If you use CSS for your layout (which you should) you are probably using CSS background images as well. These images should very rarely change, so you could just make sure your entire <strong>/images/</strong> directory is served with an appropriate cache expiration header.</p>
<p>If you’re using IIS7, all you need to do is create a <strong>web.config</strong> in your<strong> /images/</strong> folder which contains something like the following:</p>
<pre class="brush: xml; ruler: true; smart-tabs: false;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
    &lt;system.webServer&gt;
        &lt;staticContent&gt;
            &lt;clientCache cacheControlMode=&quot;UseExpires&quot; httpExpires=&quot;Sun, 01 Dec 2019 00:00:00 GMT&quot; cacheControlCustom=&quot;&quot; /&gt;
        &lt;/staticContent&gt;
    &lt;/system.webServer&gt;
&lt;/configuration&gt;</pre>
<p>Now the client’s browser can cache your images until 2019, and your <strong>YSlow</strong> score should be dramatically improved.</p>
<p>But what happens when you <strong>need</strong> to change an image? All your returning visitors would have to clear their cache to see the updated version, so that’s something we’re trying to avoid.</p>
<h3>Enter T4 Templates</h3>
<p>To work around this, you can use a feature of IIS and request the static images and including a querystring. For example: <strong>/images/bg.png?something</strong> works the same as<strong> /images/bg.png</strong>, but it comes with the added benefit of the user’s browser thinking they’re entirely different files, so it will cache them <strong>separately</strong>.</p>
<p>Armed with this knowledge, it becomes apparent that we want our <strong>.css</strong> stylesheet to link to images with a versioned querystring, which we can easily just increment when we update our images so it’s all seamless for the visitor.</p>
<p>Using the <strong>Text Template Transformation Toolkit</strong> (T4) readily available in<strong> Visual Studio </strong>we can create a <strong>.tt</strong> file which autogenerates our <strong>.css </strong>and takes care of your versioning needs.</p>
<p>Simply add a new file with a<strong> .tt</strong> extension to your Website Project, and paste the following into it:</p>
<pre class="brush: css; ruler: true; smart-tabs: false;">&lt;#@ template inherits=&quot;Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation&quot; language=&quot;C#v3.5&quot; debug=&quot;true&quot; hostSpecific=&quot;true&quot; #&gt;
&lt;#@ output extension=&quot;.css&quot; #&gt;

/* example css */
body { background:url(&lt;#= GetImage(&quot;main_bg.jpg&quot;) #&gt;) }
/* other css */
.someclass { background:url(&lt;#= GetImage(&quot;main_box_top_bg.jpg&quot;) #&gt;) repeat-x top #FFF; width:960px; }

&lt;#+
// you can simply change the value of the 'version' variable below
// to something else when you update your images

static string version = &quot;1&quot;;

string GetImage(string image)
{

    // replace /images/ with your images directory name
    return &quot;/images/&quot; + image + &quot;?&quot; + version;
}#&gt;</pre>
<p>Notice the <strong>&lt;#= GetImage(“filename.png”) #&gt;</strong> embedded code blocks.</p>
<p>Now, when you <strong>Save </strong>the file in Visual Studio, if your t4 syntax is valid, you should see a generated <strong>.css</strong> file under the <strong>.tt</strong> file in Solution Explorer.</p>
<p><a href="http://www.whycantyoucode.com/content/uploads/2010/02/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.whycantyoucode.com/content/uploads/2010/02/image_thumb1.png" width="167" height="43" /></a></p>
<p>Here’s how the generated <strong>.css</strong> file looks like:</p>
<pre class="brush: css; ruler: true; smart-tabs: false;">/* example css */
body { background:url(/images/main_bg.jpg?1) }
/* other css */
.someclass { background:url(/images/main_box_top_bg.jpg?1) repeat-x top #FFF; width:960px; }</pre>
<p>
  <br />All your layout images should be now cached, and they won’t needlessly be downloaded on every page view. Your visitors will thank you!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.whycantyoucode.com/2010/02/how-to-improve-your-yslow-add-expires-header-score-in-asp-net-using-t4-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

