75

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Tommi Gustafsson
  • 1,860
  • 4
  • 17
  • 20

15 Answers15

88

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.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
niaher
  • 9,460
  • 7
  • 67
  • 86
  • 28
    [documentation](http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification) 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. – felickz Jan 17 '14 at 15:31
  • 1
    See http://stackoverflow.com/a/14967754/682105 for an answer to how you can configure the ignore list. – khellang Jun 22 '15 at 08:48
  • 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 – Egli Becerra Mar 28 '17 at 23:45
  • 1
    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. – zerohero Jan 04 '18 at 09:57
  • 1
    @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. – agrath Nov 08 '21 at 23:10
36

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).
Bolo
  • 1,494
  • 1
  • 19
  • 19
  • 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? – Brandon Mar 10 '17 at 14:37
  • You can still use bundling with web.config debug="true" if you also set BundleTable.EnableOptimizations = true – Bolo Mar 14 '17 at 16:48
13

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

coolboyjules
  • 2,300
  • 4
  • 22
  • 42
  • Asp.net bundling issues are usually a bundler issue, not a cache in browser issue. No amount of cache updates will change the bundler not including the appropriate updated files. – Sean Haddy Jul 19 '22 at 18:12
10

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.

SharpC
  • 6,974
  • 4
  • 45
  • 40
Gleno
  • 16,621
  • 12
  • 64
  • 85
  • Removing .min files solved the bundling problem for me as well. In my case it probably occurred after merging between branches. – Hanno Apr 11 '14 at 07:35
  • This did worked for me too, but any known result why "~" is not working. – Hardik Shah Jun 29 '21 at 12:51
3

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.
Doug Mair
  • 68
  • 6
2

I actually decided to not to use System.Web.Optimization for this task, but I found Microsoft Ajax Minifier, which is also included in WebGrease.dll, which comes with MVC4 System.Web.Optimization library. I wrote the following function, which I then called in Application_Start for each minified file:

    public static void MinifyFile(string virtualPath)
    {
        string fullPath = HttpContext.Current.Server.MapPath(virtualPath);
        string extension = Path.GetExtension(fullPath).ToLower();
        string targetPath = fullPath.Substring(0, fullPath.Length - extension.Length) + ".min" + extension;
        if(File.Exists(fullPath) == false) 
        {
            throw new FileNotFoundException("File not found: " + fullPath);
        }
        string input = File.ReadAllText(fullPath);
        string output;
        if (extension == ".js")
        {
            Microsoft.Ajax.Utilities.Minifier jsmin = new Microsoft.Ajax.Utilities.Minifier();
            output = jsmin.MinifyJavaScript(input);
        }
        else if (extension == ".css")
        {
            Microsoft.Ajax.Utilities.Minifier jsmin = new Microsoft.Ajax.Utilities.Minifier();
            output = jsmin.MinifyStyleSheet(input);                
        }
        else
        {
            throw new NotSupportedException(extension + " is not supported for minification.");
        }
        File.WriteAllText(targetPath, output);
    }

Now, my application is minifying all files on Application_Start.

Tommi Gustafsson
  • 1,860
  • 4
  • 17
  • 20
  • 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. :( – Ricker Silva Dec 15 '16 at 14:58
1

I'm not sure the feature as it currently stands will really support being a CDN, as it relies implicitly on the url to contain a hashcode to prevent browser caching.

But I can try to help you try to get there, and maybe its possible today... One issue that might potentially be a roadblock is that the BundleHandler will return 304 on any bundle requests that contain the IfLastModified header, since the assumption is that the browser cache is always valid due to the fingerprint in the url.

Can you add some details about how you are rendering references to the bundles? Are you using something like Scripts.Render("~/Scripts/plugin/pluginjs")?

Your bundle script tag should look something like this:

Good: <script src="/fbt/bundles/js?v=wvLq7H7qEZB2giyIRn7aEZAxhHOb2RfTYYh2HMd9EqM1"></script>

If your script tags are referencing the raw bundle with no version string, that would likely explain the caching issues you are seeing:

Not good: <script src="/fbt/bundles/js></script>
Hao Kung
  • 28,040
  • 6
  • 84
  • 93
  • 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 Aug 29 '12 at 07:11
  • http://www.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 = "http://www.mycdn.com/scripts/plugin/myplugin-x.y.z.min.js"; bundles.Add(new ScriptBundle("~/bundles/jquery", cdnPath).Include("~/Scripts/myplugin-{version}.js")); – Tommi Gustafsson Aug 29 '12 at 07:17
  • 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. – Hao Kung Aug 29 '12 at 18:17
0

I know its been a while since this was updated but I have found I just need to wait a couple seconds to let the bundle catch up with my css changes. I have the bootstrap less files being compiled into a css and min.css and its definitely not instant to see my changes. For me it was about 10 seconds on a fast pc with an ssd. Your miles may vary based on your system specs.

LasstLos
  • 254
  • 2
  • 4
0

I saw this answer but none of these were the case for me. There were certain CSS rules that were making the styles bundler fail and i was getting the same hash, even if i made changes to the CSS file. It was all working correctly before for me.

In my case the violating css selector rule was -

#globalSearch.searching { ... }

If i made this just

.searching { ... }

It all starts working again and any changes i make to my css file the bundler hash changes correctly. Just adding this answer as it might help someone.

MoXplod
  • 3,813
  • 6
  • 36
  • 46
0

For what it's worth, I had the same problem just now with one js file inexplicably refusing to update no matter what (rebuild, forced cache clear etc). After a while, I switched the client debug tools in IE on (F12) to start watching the network traffic, and this act alone forced the JS file to refresh. Go figure, but it worked.

Sentinel
  • 3,582
  • 1
  • 30
  • 44
0

The Issue for me was I had Fiddler running. After I close dit and rebuilt my solution it was loading the changes in the js file for me.

TruthOf42
  • 2,017
  • 4
  • 23
  • 38
0

I had a similar problem. In my situation, I had a CSS file referenced in a style bundle and had that bundle referenced in my MVC view. I also had the "EnableOptimizations" flag set to false in the bundle code.

Despite all this, the view refused to update to include the new CSS file.

My solution was to create a minified version of the CSS file and include it in the project and it started working. I have no idea why this would be the case since that minified file is not referenced anywhere (even after the view updated) and shouldn't even be considered since the code is set to not be optimized. This is most likely a bug (or a feature) of the bundling functionality. I hope this helps someone else running into this problem.

mfranchi
  • 601
  • 5
  • 7
0

Make sure your app is really being deployed in Release mode and that your host is fiddling with settings. I was having this issue, but after investigating, I realized my files were not actually being bundled. I was deploying in Release mode, but for some reason (I suspect host), I think my app was really deployed in debug.

I had to set the following at the end of the BundleConfig.cs file to force bundling, which in turn forced the updated file to finally show in the browser.

BundleTable.EnableOptimizations = true;
lucky.expert
  • 743
  • 1
  • 15
  • 24
0

I had this issue today and I went through all the answers but my problem wasn't solved by any of the solutions here. Later I found that this was happening because there was an error in my CSS. One of the urls was not closed (the last single quote was missing).

This caused the css file to have a syntax error and it didn't compile for the Bundleconfig. I suppose there would have been a message in the Output log, but I hadn't checked.

If this is happening to you in 2020, try making sure your CSS does not have a syntax error.

Cyril Gupta
  • 13,505
  • 11
  • 64
  • 87
-1

Just update your System.Web.Optimization by NuGet enter image description here

enter image description here