Created
July 20, 2019 16:21
-
-
Save fredgdaley2/341587d3bcffc7fca9a123d3cdac5384 to your computer and use it in GitHub Desktop.
Bundle Extension for versioning
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
using System.Web.Optimization; | |
using MyProject.Extensions; | |
namespace MyProject | |
{ | |
public class BundleConfig | |
{ | |
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862 | |
public static void RegisterBundles(BundleCollection bundles) | |
{ | |
bundles.Add(new ScriptBundle("~/bundles/jquery").Include( | |
@"~/Scripts/jquery-{version}.js")); | |
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include( | |
@"~/Scripts/jquery.validate*")); | |
bundles.Add(new ScriptBundle("~/bundles/jqueryvalnative") | |
.Include("~/Scripts/jquery.validate.js")); | |
// Use the development version of Modernizr to develop with and learn from. Then, when you're | |
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need. | |
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( | |
@"~/Scripts/modernizr-*")); | |
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( | |
@"~/Scripts/bootstrap.js", | |
@"~/Scripts/respond.js", | |
@"~/Scripts/sweet-alert.js", | |
@"~/Scripts/bootstrap-slider.js", | |
@"~/Scripts/moment.js", | |
@"~/Scripts/daterangepicker.js", | |
@"~/Scripts/masterlayout.js").WithLastModifiedToken()); | |
bundles.Add(new StyleBundle("~/Content/css").Include( | |
@"~/Content/bootstrap.css", | |
@"~/Content/bootstrap-flat.css", | |
@"~/Content/bootstrap-flat-extras.css", | |
@"~/Content/bootstrap-tab-extras.css", | |
@"~/Content/bootstrap-slider.css", | |
@"~/Content/daterangepicker.css", | |
@"~/Content/site.css", | |
@"~/Content/sweet-alert.css").WithLastModifiedToken()); | |
bundles.Add(new ScriptBundle("~/bundles/jqueryextras").Include( | |
@"~/Scripts/jquery-ui-1.9.2.js", | |
@"~/Scripts/jquery.hint.js", | |
@"~/Scripts/jquery.format.js", | |
@"~/Scripts/jquery.fileupload.js").WithLastModifiedToken()); | |
bundles.Add(new ScriptBundle("~/bundles/knockout").Include( | |
@"~/Scripts/knockout-3.4.1.js", | |
@"~/Scripts/knockout.mapping-latest.js").WithLastModifiedToken()); | |
} | |
} | |
} |
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
using System; | |
using System.Web.Optimization; | |
using System.IO; | |
using System.Web.Hosting; | |
namespace MyProject.Extensions | |
{ | |
internal static class BundleExtensions | |
{ | |
/// <summary> | |
/// Versioning for Bundles | |
/// Source: H-Dog's answer https://stackoverflow.com/questions/15005481/mvc4-stylebundle-can-you-add-a-cache-busting-query-string-in-debug-mode | |
/// </summary> | |
/// <param name="sb"></param> | |
/// <returns></returns> | |
public static Bundle WithLastModifiedToken(this Bundle sb) | |
{ | |
sb.Transforms.Add(new LastModifiedBundleTransform()); | |
return sb; | |
} | |
public class LastModifiedBundleTransform : IBundleTransform | |
{ | |
public void Process(BundleContext context, BundleResponse response) | |
{ | |
foreach (var file in response.Files) | |
{ | |
var lastWrite = File.GetLastWriteTime(HostingEnvironment.MapPath(file.IncludedVirtualPath)).Ticks.ToString(); | |
file.IncludedVirtualPath = string.Concat(file.IncludedVirtualPath, "?v=", lastWrite); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment