Skip to content

Instantly share code, notes, and snippets.

@cscott530
Last active March 31, 2016 14:15
Show Gist options
  • Save cscott530/06dbe48bb274fd8026e9219ce6d9f7c2 to your computer and use it in GitHub Desktop.
Save cscott530/06dbe48bb274fd8026e9219ce6d9f7c2 to your computer and use it in GitHub Desktop.
Non-caching when EnableOptimizations won't work (Requires using Razor)
public static void RegisterBundles(BundleCollection bundles) {
//use bundles as usual
bundles.Add(new ScriptBundle("~/bundles/jqPlot/plugins")
.IncludeDirectory("~/Scripts/jqPlot/plugins", "*.js"));
}
public class MvcApplication : System.Web.HttpApplication
{
//Add this to the top of your Global.asax.cs file, it will be used for the timestamp.
public static DateTime StartTime = DateTime.UtcNow;
}
//This is an extension class on @Html
public static class HtmlRazorExtensions {
//given a path, will use the standard Scripts.render to resolve it to a raw <script src=""></script> then replace on it
public static IHtmlString UncachedScript(this HtmlHelper helper, string path)
{
var renderedPaths = Scripts.Render(path);
var fixedPaths = ReplaceListOfPaths("<script src=\"{0}\"></script>", renderedPaths, "src=\\\"(.*?)\\\"");
return helper.Raw(fixedPaths);
}
public static IHtmlString UncachedStyle(this HtmlHelper helper, string path)
{
var renderedPaths = Styles.Render(path);
var fixedPaths = ReplaceListOfPaths("<link href=\"{0}\" rel=\"stylesheet\">", renderedPaths, "href=\\\"(.*?)\\\"");
return helper.Raw(fixedPaths);
}
private static string ReplaceListOfPaths(string outputFormat, IHtmlString paths, string regexToMatch)
{
//lazily initialize the _nocache param. everytime the server restarts, this value will be unique, so
//when new scripts are deployed, they will never be cached
if (string.IsNullOrEmpty(_nonCachingExtension))
{
_nonCachingExtension = string.Format("_nocache={0}", MvcApplication.StartTime.ToString("yyyyMMddHHmmssffff"));
}
var rawPaths = paths.ToHtmlString();
//find any of the src="" or href="" entries in the given path(s)
var matches = Regex.Matches(rawPaths, regexToMatch);
var content = new StringBuilder();
foreach (Match match in matches)
{
//we have a (.*?) in our regex, so we want the value of the attribute
var group = match.Groups[1];
var actualPath = group.Value;
//some scripts (google, it seems) already have a ?, so we need to append with '&'
var appender = actualPath.Contains("?") ? "&" : "?";
var replaced = string.Format("{0}{1}{2}", actualPath, appender, _nonCachingExtension);
content.AppendFormat(outputFormat, replaced);
}
return content.ToString();
}
}
<!-- How you actually include the files
include our custom HtmlHelper extension -->
@using LEM.UIHelpers
@Html.UncachedScript("~/CustomScripts/AddToLearningPath.js")
@* instead of
@Scripts.Render("~/CustomScripts/AddToLearningPath.js")
*@
@* works with multi-script bundles, as well *@
@Html.UncachedScript("~/bundles/jqPlot/plugins")
<html>
...
<script src="/CustomScripts/AddToLearningPath.js?_nocache=201603311340174381"></script>
...
<script src="/Scripts/jqPlot/plugins/jqplot.barRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.BezierCurveRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.blockRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.bubbleRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.canvasAxisLabelRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.canvasAxisTickRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.canvasOverlay.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.canvasTextRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.categoryAxisRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.ciParser.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.cursor.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.dateAxisRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.donutRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.dragable.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.enhancedLegendRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.funnelRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.highlighter.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.json2.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.logAxisRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.mekkoAxisRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.mekkoRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.meterGaugeRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.mobile.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.ohlcRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.pieRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.pointLabels.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.pyramidAxisRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.pyramidGridRenderer.js?_nocache=201603311405494971"></script>
<script src="/Scripts/jqPlot/plugins/jqplot.pyramidRenderer.js?_nocache=201603311405494971"></script>
...
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment