Skip to content

I run my site on AppHarbor or Azure. Are there any special issues to consider here?

mwrock edited this page Mar 12, 2012 · 6 revisions

RequestReduce is fully functional on both AppHarbor and Azure.

Persisting RequestReduce processed content when your instance is reclaimed

By default, RequestReduce saves all processed content to local disk. When your compute instance is reclaimed by AppHarbor or Azure, these files are lost and newly deployed instances will need to reprocess content. If this does not happen often and you do not have alot of content to process, this may not be much of a concern. However, if you have a large number of CSS, Scripts and background images, it may take a while for RequestReduce to reprocess all of your content. To remedy this, use RequestReduce.SqlServer to persist the processed files to Sql Server. This does not mean that RequestReduce will make a sql call on every request. It just means that if RequestReduce cannot find the reduced file on disk, it will fall back to sql server to pull the file and then save that file to disk for subsequent calls.

Note to AppHarbor users: To allow RequestReduce to write the optimized files to disk, make sure to check the box next to "Enable file system write access" on your application's settings page. If this is not checked, RequestReduce will not work and you will receive this error on application startup:

StructureMap Exception Code: 207 Internal exception while creating Instance '056ad88f-623b-4989-a02c-8c7cf7663e0e' of PluginType RequestReduce.Store.IStore. Check the inner exception for more details.

Using Azure CDN Endpoints to cache RequestReduce generated content**

The azure CDN works a little differently from traditional CDNs like Akamai. The Azure CDN, expects all of your CDN content to reside inside of a folder called "CDN". When the Azure CDN pulls content from your site when its cache is cold, it pulls all content relative to this folder but does not include the /CDN/ folder name in the path. So if you have a source url of http://mysite.com/cdn/logo.png, the azure cdn url may be http://azurecdnhost.com/logo.png.

RequestReduce provides a contentHost configuration setting commonly used for working with CDNs. Normally, you would specify http://azurecdnhost.com for this setting and RequestReduce will use this host name instead of your local site's name when constructing its URLs. Unfortunately, http://azurecdnhost.com/CDN/logo.png which RequestReduce would generate for a logo.png in the CDN folder would not be a valid Url. To work around this problem, in addition to the above contentHost configuration setting, add this API call to your application startup code:

RequestReduce.Api.Registry.UrlTransformer = (x, y, z) => z.Replace("/cdn/", "/");

Now RequestReduce will remove the excess /CDN/ from its generated URL which generates http://azurecdnhost.com/logo.png which is a valid Azure CDN Url.

AppHarbor Build change for Optipng PNG optimizations

However, there is one small yet significant detail to be aware of with AppHarbor. AppHarbor does not play nice with the post-build script that the RequestReduce Nuget package adds to your project. This script is supposed to copy optipng.exe to your application's bin directory so that RequestReduce can shell out to that exe to perform lossless compression on the sprited png image. Here is the script:

start /MIN xcopy /s /y "$(SolutionDir)packages\RequestReduce.0.9.233\pngoptimization\*.exe" "$(TargetDir)"

The AppHarbor build will succeed but optipng.exe will be missing from your bin directory. RequestReduce will perform without error and will be able to partially optimize the sprites but it will not be able to perform the final compression step. There is a way to tweak your AppHarbor application to get optipng.exe into your bin.

Copy optipng.exe from the RequestReduce nuget package directory to the root of your application Visual Studio project. optipng.exe can be found in /packages/RequestReduce[version]/pngoptimization. Once you have copied optipng.exe to your project root, in Visual Studio do the following:

  1. Right click optipng.exe and select Include in Project
  2. In the properties of that file, select None as the build action
  3. Also in properties, select Copy Always as the Copy to Output Directory action

This will make sure that optipng.exe surfaces in your bin but is not accessible via http from the root of your deployed web site.

Clone this wiki locally