Since bundling and minification is all about optimization and making pages load faster, it appears to me that it would be logical to create one bundle for scripts and one bundle for styles per view basis, so that to load all scripts and styles a browser will need to make 2 requests at most. So let's say I have a _Layout.cshtml
where I need 3 js files bootstrap.js
,jquery.js
and some custom1.js
I can create one bundle, something like this:
bundles.Add(new ScriptBundle("~/bundles/layout").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/jquery.js",
"~/Scripts/custom1.js"));
Then let's say I have another view User.cshtml
where I need bootstrap.js
,jquery.js
and custom2.js
, again I create one bundle:
bundles.Add(new ScriptBundle("~/bundles/user").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/jquery.js",
"~/Scripts/custom2.js"));
Same approach can be used for style bundles. But from what I see it's not a common practice to do it this way usually bundles are created on the type basis, e.g., one for bootstrap, one for jquery, one for custom js/css files etc. But wouldn't doing so increase the loading time of the page, because having more bundles means more request to the server. Also what is the problem of doing things the way I described at the beginning?