Add cache batter to a non-optimized package?

I use Bundling in MVC and have the following:

@Scripts.Render("~/bundles/scripts.js"); 

When BundleTable.EnableOptimizations = true it displays as:

 <script src="/bundles/scripts.js?v=RF3ov56782q9Tc_sMO4vwTzfffl16c6bRblXuygjwWE1"></script> 

When BundleTable.EnableOptimizations = false it displays as:

 <script src="/js/header.js"></script> <script src="/js/content.js"></script> <script src="/js/footer.js"></script> 

Is it possible to intercept a non-optimized version to include my own custom cache?

For example:

 <script src="/js/header.js?v=12345"></script> <script src="/js/content.js?v=12345"></script> <script src="/js/footer.js?v=12345"></script> 
+9
caching asp.net-mvc bundling-and-minification
source share
2 answers

Why do you need this? In development, where BundleTable.EnableOptimizations = false nothing is cached anyway, and in production you should have BundleTable.EnableOptimizations = true , also denying the need for something like that.

The short answer is, it’s not , there is nothing created so that you can do what you ask, primarily because of the reasons that I have already mentioned: there simply is no need for such a function.

+5
source share

Here is a detailed solution for what you are looking for:

https://volaresystems.com/blog/post/2013/03/18/Combining-JavaScript-bundling-minification-cache-busting-and-easier-debugging

To summarize the cache crash, create a utility method that combines the timestamp for creating the file of non-optimized files that you want to cache.

for example

<script src="@StaticFile.Version("/js/header.js")"></script>

This will produce what you are looking for:

 <script src="/js/header.js?v=12345"></script> 

Cache Cleanup Utility:

 using System.IO; using System.Web.Caching; using System.Web.Hosting; namespace System.Web.Optimization { public static class StaticFile { public static string Version(string rootRelativePath) { if (HttpRuntime.Cache[rootRelativePath] == null) { var absolutePath = HostingEnvironment.MapPath(rootRelativePath); var lastChangedDateTime = File.GetLastWriteTime(absolutePath); if (rootRelativePath.StartsWith("~")) { rootRelativePath = rootRelativePath.Substring(1); } var versionedUrl = rootRelativePath + "?v=" + lastChangedDateTime.Ticks; HttpRuntime.Cache.Insert(rootRelativePath, versionedUrl, new CacheDependency(absolutePath)); } return HttpRuntime.Cache[rootRelativePath] as string; } } } 
0
source share

All Articles