ASP.NET Bundling - Bundle not updating after included file has changed (returns 304 not modified)

63,721

Solution 1

I just had the exact same problem. I have a folder with 2 CSS files:

  • ~/Content/main.css
  • ~/Content/main.min.css (pre-existing from my previous manual minification process)

My bundling code is this:

bundles.Add(new StyleBundle("~/css/main").Include("~/content/main.css"));

No matter how much I changed my main.css the output was the same url with the same contents:

<link href="/css/main?v=6Xf_QaUMSlzHbXralZP7Msq1EiLTd7g1vId6Vcy8NJM1" rel="stylesheet"/>

The only way to update the bundle was to rebuild my solution - obviously not the best approach.

However as soon as I deleted main.min.css, everything started to work just fine. Playing a little more I discovered that if there are both main.css and main.min.css, then updating main.min.css will actually update the bundle... Weirdness, but at least predictable.

Solution 2

After fighting to figure out what makes the bundle cache refresh I came to a few conclusions that will hopefully help others:

If .min files ARE included as part of the bundle:

  • release mode + change min js code = cache refresh
  • release mode + change non min js code = no cache refresh
  • debug mode + change min js code = no cache refresh
  • debug mode + change non min js code = no cache refresh

If .min files are NOT included as part of the bundle:

  • debug mode + change js code = no cache refresh
  • release mode + change js code = cache refresh

Notes

  • By debug mode i mean web.config compilation debug = true (and BundleTable.EnableOptimizations = false or is omitted)
  • By release mode i mean web.config compilation debug = false (and BundleTable.EnableOptimizations = true or is omitted
  • Be sure you are actually making code changes. Changes such as spaces and comments do not affect the resulting minified js so the server is correct in that there are no changes (so the bundle cache is not refreshed).

Solution 3

Please note that if you are using Google Chrome the caching is quite aggressive. To ensure nothing is cached, you can do Ctrl-Shift-I to bring up the developer pane. Go to Network and click Disable Cache. Ensure you keep this open. Now refresh the page. Your cache should be cleared and the file changes should be reflected now.

Disable cache in Google Chrome

Solution 4

Okay, here's my story. I disabled generation of min files for less files in Web Essentials. Old min files were not deleted, and bundle thingy saw those instead of updated CSS. Good luck!

EDIT

Sometime later I spent another good 2 hours on the same issue. This time it was my fault I guess - I forgot the leading tilde, i.e. I wrote

Scripts.Render("/js/script")

in lieu of

Scripts.Render("~/js/script")

For whatever reason it sometimes worked, and sometimes it did no such thing.

Solution 5

The bundling operation is case sensitive. Make sure the filename has the proper case.

I had to change a line in my BundleConfig.cs:

bundles.Add(new StyleBundle("~/Content/css").Include(
    "~/Content/bootstrap.css",
    "~/Content/Site.css"));  <-- Uppercased.
Share:
63,721
Tommi Gustafsson
Author by

Tommi Gustafsson

Updated on July 05, 2022

Comments

  • Tommi Gustafsson
    Tommi Gustafsson almost 2 years

    I am trying out ASP.NET Bundling with ASP.NET MVC 4 application. The situation is that I want to make a CDN style service, which has JS and CSS files to which you can address from other sites with this type address: http://www.mycdn.com/scripts/plugin/js, which bundles and minifies all included .js files.

    My bundle config for one file looks like this:

    bundles.Add(new ScriptBundle("~/Scripts/plugin/pluginjs").Include("~/Scripts/plugin/jquery.plugin.js"));
    

    However, when I do this, the bundles are not getting updated even after I change the original js files. I keep on getting 304 Not Modified, when I refresh my browser, and the content of the minified file is not updated. How can I make bundles update, because it is useless to have bundles with old content? I tried out every way, but could not figure out a solution.

    Thanks in advance!

  • Tommi Gustafsson
    Tommi Gustafsson over 11 years
    The problem is that this is a CDN type service, so it is hosting files for other web sites and it does not use the files themselves. So, the referencing sites can be anything, and not necessarily ASP.NET sites. I also came upon the fact that Bundling has something called CDN-support.
  • Tommi Gustafsson
    Tommi Gustafsson over 11 years
    asp.net/mvc/tutorials/mvc-4/bundling-and-minification (see CDN section) Could it be possible that I Just host the files normally without bundling and minification, and then possibly reference from other ASP.NET sites to these files using the CDN format: bundles.UseCdn = true; //enable CDN support string cdnPath = "mycdn.com/scripts/plugin/myplugin-x.y.z.min.js"; bundles.Add(new ScriptBundle("~/bundles/jquery", cdnPath).Include("~/Scripts/myplugin-{version}.js"));
  • Hao Kung
    Hao Kung over 11 years
    Yes, if that's a feasible option, then that is most likely the best option, just have the ASP.NET sites use your CDN which hosts the bundle.
  • felickz
    felickz over 10 years
    documentation The bundling framework follows several common conventions such as: Selecting “.min” file for release when “FileX.min.js” and “FileX.js” exist. Selecting the non “.min” version for debug. Ignoring “-vsdoc” files (such as jquery-1.7.1-vsdoc.js), which are used only by IntelliSense.
  • Hanno
    Hanno about 10 years
    Removing .min files solved the bundling problem for me as well. In my case it probably occurred after merging between branches.
  • khellang
    khellang almost 9 years
    See stackoverflow.com/a/14967754/682105 for an answer to how you can configure the ignore list.
  • Ricker Silva
    Ricker Silva over 7 years
    So you did not found any solution. I'm experiencing same problem, changed background image in css file, debuging in dev station works good, after publiching, old background appears. no minified file was updated. :(
  • Brandon
    Brandon about 7 years
    What if we release in debug mode? Does that mean that bundling will be broken in production--always delivering the first bundle it created after IIS was restarted?
  • Bolo
    Bolo about 7 years
    You can still use bundling with web.config debug="true" if you also set BundleTable.EnableOptimizations = true
  • Egli Becerra
    Egli Becerra about 7 years
    This fixed the problem... really good answer... thanks also in my case of weirdness i had a main.js and a main.min.remove.js (it picked up the main.min.remove.js) technical debt of some other person I cleaned after that all was sweeeet
  • zerohero
    zerohero over 6 years
    I find it ridiculous that Microsoft decided to ignore my carefully coded bundles and just goes with whatever "min" file is included in the same directory. If I wanted to use the "min" file, I would SPECIFY the "min" file. FFS.
  • Hardik Shah
    Hardik Shah almost 3 years
    This did worked for me too, but any known result why "~" is not working.
  • agrath
    agrath over 2 years
    @felickz thanks, that was a weird problem and while the behaviour makes sense, I didn't expect it. I had to patch a library and didn't update the .min.js files manually because the .js file was included in the bundle.