Sahi
Sahi

Reputation: 1484

MVC Bundling - one file in more than one bundle

I have 5 js files:

  1. a1.js
  2. a2.js
  3. a3.js
  4. a4.js
  5. a5.js

bundled together in bundle1.js, and I'm using this bundle in View1.

Now I need to use a3.js , a5.js and a6.js in in View2.

Can I create a new bundle with a3, a5 and a6 or use bundle1 for a3 & a5 and create new bundle for a6?

Which is the good approach?

Upvotes: 2

Views: 965

Answers (2)

Mukesh Kumar
Mukesh Kumar

Reputation: 2376

You can use the bundle1 for all view. But problem is that when some browser is going to open your page which required only a3.js and a5.js then it will download all. and it makes your site very slow.

So, according to me, you should always create multiple bundling as per required js/css as per page.

bundles.Add(new ScriptBundle("~/bundles/JqueryInterviewHomePage").Include("~/Content/js/jquery-1.8.3.min.js", "~/Content/Jquery/bootstrap.min.js", "~/Content/Jquery/InterviewHomeJquery.js"));

bundles.Add(new ScriptBundle("~/bundles/JqueryInterviewViewPage").Include("~/Content/js/jquery-1.8.3.min.js", "~/Content/Jquery/bootstrap.min.js", "~/Content/Jquery/SingleInterviewPostJquery.js"));

bundles.Add(new ScriptBundle("~/bundles/JqueryArticlesHomePage").Include("~/Content/js/jquery-1.8.3.min.js","~/Content/Jquery/bootstrap.min.js", "~/Content/Jquery/ArticlesHomeJquery.js"));

bundles.Add(new ScriptBundle("~/bundles/JqueryArticlesViewPage").Include("~/Content/js/jquery-1.8.3.min.js","~/Content/Jquery/bootstrap.min.js", "~/Content/Jquery/SinglePostJquery.js"));

Hope this will help you. thanks

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

I think that there's no correct answer to this. You could also consider including a6.js in bundle1 and just using bundle1.js in View2. It will already be cached by the browser, so no additional HTTP requests even if some of the individual js are not needed. So basically have all your scripts in a single bundle that is placed in your Layout even if not strictly required by all views.

But if you have to choose between creating a new bundle with a3, a5 and a6 or bundle1 for a3 & a5 and create new bundle for a6 then the first is preferred.

Upvotes: 5

Related Questions