Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 470
Tommi Gustafsson Avatar asked Aug 28 '12 12:08

Tommi Gustafsson


2 Answers

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.

like image 98
niaher Avatar answered Oct 16 '22 20:10

niaher


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).
like image 36
Bolo Avatar answered Oct 16 '22 22:10

Bolo