0

I have a weird issue with the mvc4 bundler not including files with extension .min.js

In my BundleConfig class, I declare

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/Scripts/jquery")
        .Include("~/Scripts/jquery-1.8.0.js"));

    bundles.Add(new ScriptBundle("~/Scripts/mybundle")
        .Include("~/Scripts/myscript.js?v=" + DateTime.Today.ToShortDateString().Replace("/", "").Replace(" ", ""));
}

In my view, I declare

<html>
    <head>
    @Scripts.Render("~/Scripts/jquery")
    @Scripts.Render("~/Scripts/mybundle")
    </head><body>test</body>
</html>

And when it renders, it only renders

<html>
    <head>
         <script src="/Scripts/jquery-1.8.0.js"></script>
    </head>
    <body>test</body>
</html>

If I remove versioning from script ~/Scripts/myscript.js (and update the path in the bundle accordingly), both scripts are rendered correctly.

Is there some config setting that is causing it to ignore 'versioning' files?

user3328402
  • 47
  • 2
  • 3
  • 12

2 Answers2

1

The sysnax we use for bundling (as you are already doing in your code) is

bundles.Add(new ScriptBundle("~/Scripts/urlToYourBundledJs")
    .Include("~/Scripts/yourIndividualScript.js"));

If you want to force update your scripts from client, you can add a version to the url, not the file name. For example, you can do the following in your view file

@Scripts.Render("~/Scripts//urlToYourBundledJs?v=" + DateTime.Today.ToShortDateString().Replace("/", "").Replace(" ", ""))

So, every time your html requests for the script, it will ask for a updated one (once per day, as per your script). But, note this will stop the automatic versioning of the bundles! Which is NOT something recommendable.

If your intention is to get the updated scripts always, just remove your versioning part and the framework will take care of everything. The version id generated by the optimization framework is actually a hash of all the files included. So, each time any file content is updated, the version is regenerated so that the client automatically gets the latest scripts :)

On the other hand, if you want to maintain versions of each file manually, you can use the {version} wildcard in the bundle

bundles.Add(new ScriptBundle("~/Scripts/urlToYourBundledJs")
    .Include("~/Scripts/yourIndividualScript-{version}.js"));

And the framework will automatically pick the latest version. Like, if you have two files in the scripts folder

yourIndividualScript-1.6.js
yourIndividualScript-2.3.js

only yourIndividualScript-2.3.js will be used. So, you can simply add a new version 3.0 and that will be picked up next time.

Arghya C
  • 9,805
  • 2
  • 47
  • 66
0

MVC Bundling already includes versioning in it's implementation. it appends a unique token as a query string. For example:

http://example.com/Scripts/mybundle?v=igXMB2ssiFEYs6OHiNIHZ080TnQY5Z997l4s1pyJMPs1

Each time on of the scripts in a bundle changes, new token is generated. So this insures that your clients receive an up to date content.

By the way in order to activate bundling add to your BundleConfig the following line:

BundleTable.EnableOptimizations = true;
Alex Art.
  • 8,711
  • 3
  • 29
  • 47