2

I would like to know the implications of modifying bundles collection dynamically (say during load of a page). I tried adding a new script file to the bundles collection (originally created in app_start). Its working fine in my initial test, - one difference I noticed is that browser is not caching the bundled script and style(sending new requests on every refresh). I would like to know if there is a way to force caching of the bundle script/style after the initial fetch.

I have my static scripts and styles loaded to the bundles collection in app_start itself. But I have piece of code in master page load, to check the existence of page specific script or styles (for ex. lets say a page ABC.aspx is being loaded, this code would look for existence of ABC.js in Scripts folder and ABC.css in Styles folder). if it exists it'll be loaded to the page header. This is where I tried adding it to the bundles. What would be the best approach to make these conditional scripts/styles part of default bundle collection?

My production environment is a web farm. So is there something I should specifically do to have the url V hash remain same across servers?

I'd read a comment by "Hao Kung" here, explaning a bundle caching issue for webfarms (results in 404), what would be the best approch to handle this?

Community
  • 1
  • 1

1 Answers1

1

AFAIK, when building the bundle, the content is hashed in order to create a unique key for it (the extra parameter in the url).

So if you are modifying its contents the hash will change since it actually represents a different bundle. The whole purpose of that is for cache busting when the content changes.

The behavior your are seeing is because bundles are not intended to be used in that dynamic way, since it goes against the idea that the bundle's content is static (and therefore cacheable).

Why not just create a separate bundle for each page? That way, the "common" bundle, with all the shared code will be cached and reused, and when loading your ABC.aspx page, you always load your "abc" bundle, that will do its own version control on the contents and not affect the common libraries.

Plus, there is an additional downside to modifying the bundle on each page:

If each page delivers a different bundle, then the client will receive all the code for your shared libraries over and over again, once for each page. Even if cached. For example: there would be no point in sending JQuery along with each page.

Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58
  • Thanks Pablo for the suggestions. – code_explorer Feb 04 '14 at 18:07
  • Thanks Pablo for the suggestions. Yes, its better to keep the bundles static as much as possible. I agree on that. My other question is that, is there any specific settings I should do to make the bundles static hash same across all servers in a web farm? – code_explorer Feb 04 '14 at 18:16
  • Sure thing. Regarding the servers, there are no settings that I know of. It is computed based on the content of each file, so if the content is the same there shouldn't be much of an issue. However, some have reported issues when the servers aren't running the same .NET version, such as: http://stackoverflow.com/a/15071452/1373170 – Pablo Romeo Feb 05 '14 at 04:11
  • Thank you. In my case, its assured that all boxes on the farm will have same configuration. So I think, I can have peace of mind with this issue then :) – code_explorer Feb 05 '14 at 04:19
  • Yeah, I think so but it's still always a good idea to test it out just in case. I haven't personally had any issues with that. Now, if you're still going to be dynamically adding bundles, beware with the issues mentioned in that link (the 404 one). I've always kept them static so I can't say exactly how it will behave in that scenario. – Pablo Romeo Feb 05 '14 at 04:23