Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top