The Problem
I like concise, so when I realized I could change the following:
@Scripts.Render("~/bundles/bundle1")
@Scripts.Render("~/bundles/bundle2")
@Scripts.Render("~/bundles/bundle3")
@Scripts.Render("~/bundles/bundle4")
... to ...
@Scripts.Render("~/bundles/bundle1", "~/bundles/bundle2", "~/bundles/bundle3", "~/bundles/bundle4")
... naturally I got excited. And it worked really well, that is, until I needed bundle3 to come before bundle1.
For a more realistic example, I have my own bundle that is dependent on KnockoutJS and requires Knockout to be loaded before my bundle is. It would look like this:
@Scripts.Render("~/bundles/something", "~/bundles/knockout", "~/bundles/adminView")
I assumed that the bundles would be rendered in the same order I specified in that call, but it doesn't seem to play nice that way. My "~/bundles/adminView" was rendered before Knockout which broke my script.
To fix it, I removed my bundle from the rest of the list and moved it out into a separate Scripts.Render call. Now it looks like:
@Scripts.Render("~/bundles/something", "~/bundles/knockout")
@Scripts.Render("~/bundles/adminView")
Doing this forced my "adminView" bundle to be rendered after the other two because it was a separate call.
Moral of the Story
You can only "bundle" multiple bundle paths in one Scripts.Render call when none of them depend on each other, because you can't count on what order they will be rendered in. I don't know how Microsoft decides the order to render them in, but it's not necessarily the order you provide.If order matters, use separate calls and order them the way you need to.
No comments:
Post a Comment