Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to version javascript files in a Bundle?

I want to include my JS scripts in a Bundle AND at the same time version them to avoid browser caching if those scripts have been changed.

Is there a "native" way with ASP.NET MVC 5 to accomplish this?

I've found a library called Cassette that does exactly this (automatically versioning the JS, which is a good thing) but I wanted something (even) simpler, without changing a lot my script configuration...

The following does not work:

 var version = ConfigurationManager.AppSettings["cache"].ToString();
 bundles.Add(new ScriptBundle("~/bundles/scripts").Include(
     "~/Scripts/framework.js" + version,
     "~/Scripts/main.js"));

The result is that the "framework.js" file is not downloaded by the browser (probably is not found).

like image 922
Luis Avatar asked Mar 09 '16 19:03

Luis


1 Answers

MVC will automatically add a version parameter for release builds (i.e. if debug="false" in web.config); see "Bundle Caching" here: http://www.asp.net/mvc/overview/performance/bundling-and-minification

You can explicitly enable this in debug builds (described in the same article), but your scripts will be minified as part of the same action; there's no separate method to do so.

like image 196
technophile Avatar answered Oct 16 '22 23:10

technophile