Created
February 25, 2013 10:47
MVC Scripts + Styles With Cache Buster
This file contains hidden or 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 static class ViewComponentHelper | |
{ | |
private static string _cacheComponentVersion; | |
public static IHtmlString RenderScripts(string[] paths) | |
{ | |
//Bust The Cache. | |
var bustedPaths = paths.Select(BustPath).ToArray(); | |
return Scripts.Render(bustedPaths); | |
} | |
public static IHtmlString RenderStyles(string[] paths) | |
{ | |
//Bust The Cache. | |
var bustedPaths = paths.Select(BustPath).ToArray(); | |
return Styles.Render(bustedPaths); | |
} | |
private static string BustPath(string path) | |
{ | |
var cachedComponentVersion = GetCachedComponentVersion(); | |
return path + "?" + cachedComponentVersion; | |
} | |
private static string GetCachedComponentVersion() | |
{ | |
const string cacheKey = "CacheComponentVersion"; | |
return _cacheComponentVersion ?? | |
(_cacheComponentVersion = ConfigurationManager.AppSettings[cacheKey]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment