Skip to content

Instantly share code, notes, and snippets.

@jchandra74 jchandra74/fingerprint.cs

Last active Aug 29, 2015
Embed
What would you like to do?
asp.net good config and practices
//Stolen from @mkristensen
//Use to bust static content like .js and .css when they changed.
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.Hosting;
namespace CHANGE_YOUR_NAMESPACE_HERE
{
public class Fingerprint
{
public static string Tag(string rootRelativePath)
{
if (HttpRuntime.Cache[rootRelativePath] == null)
{
var absolute = HostingEnvironment.MapPath("~" + rootRelativePath);
var date = File.GetLastWriteTime(absolute);
var index = rootRelativePath.LastIndexOf('/');
var result = rootRelativePath.Insert(index, "/v-" + date.Ticks);
HttpRuntime.Cache.Insert(rootRelativePath, result, new CacheDependency(absolute));
}
return HttpRuntime.Cache[rootRelativePath] as string;
}
}
}
<system.webserver>
<!-- ensure gzip on both static and javascript, etc. -->
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
<!-- long expiry cache for static contents -->
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
<!-- url rewriting to strip of the versioning to break the long expiry cache on static contents -->
<rewrite>
<rules>
<rule name="fingerprint">
<match url="([\S]+)(/v-[0-9]+/)([\S]+)" />
<action type="Rewrite" url="{R:1}/{R:3}" />
</rule>
</rules>
</rewrite>
</system.webserver>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.