Created
December 16, 2014 04:25
-
-
Save andrewabest/68ff8edac4017d54f195 to your computer and use it in GitHub Desktop.
Cache Busting in MVC local
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppendFileHash : IBundleTransform | |
{ | |
public void Process(BundleContext context, BundleResponse response) | |
{ | |
foreach (var file in response.Files) | |
{ | |
using (var fs = File.OpenRead(HostingEnvironment.MapPath(file.IncludedVirtualPath))) | |
{ | |
var hash = new SHA256Managed().ComputeHash(fs); | |
var version = HttpServerUtility.UrlTokenEncode(hash); | |
file.IncludedVirtualPath = string.Concat(file.IncludedVirtualPath, "?hbuster=", version); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductNameScriptBundle : ScriptBundle | |
{ | |
public ProductNameScriptBundle(string virtualPath) : base(virtualPath) | |
{ | |
if (AppEnvironment.IsLocal()) | |
Transforms.Add(new AppendFileHash()); | |
} | |
public ProductNameScriptBundle(string virtualPath, | |
string cdnPath) : base(virtualPath, cdnPath) | |
{ | |
if (AppEnvironment.IsLocal()) | |
Transforms.Add(new AppendFileHash()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductNameStyleBundle : StyleBundle | |
{ | |
public ProductNameStyleBundle(string virtualPath) : base(virtualPath) | |
{ | |
if (AppEnvironment.IsLocal()) | |
Transforms.Add(new AppendFileHash()); | |
} | |
public ProductNameStyleBundle(string virtualPath, | |
string cdnPath) : base(virtualPath, cdnPath) | |
{ | |
if (AppEnvironment.IsLocal()) | |
Transforms.Add(new AppendFileHash()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment