11

我对捆绑脚本和样式文件的正确方法有点困惑。目前,我的 BundleConfig.cs 看起来像这样:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/jquery-{version}.js"));

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-{version}.js"));

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.unobtrusive*",
            "~/Scripts/jquery.validate*"));

// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
            "~/Scripts/modernizr-*"));

bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
            "~/Scripts/knockout-{version}.js",
            "~/Scripts/knockout-{version}.debug.js",
            "~/Scripts/knockout-sortable.js"));

bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
            "~/Content/themes/base/jquery.ui.core.css",
            "~/Content/themes/base/jquery.ui.resizable.css",
            "~/Content/themes/base/jquery.ui.selectable.css",
            "~/Content/themes/base/jquery.ui.accordion.css",
            "~/Content/themes/base/jquery.ui.autocomplete.css",
            "~/Content/themes/base/jquery.ui.button.css",
            "~/Content/themes/base/jquery.ui.dialog.css",
            "~/Content/themes/base/jquery.ui.slider.css",
            "~/Content/themes/base/jquery.ui.tabs.css",
            "~/Content/themes/base/jquery.ui.datepicker.css",
            "~/Content/themes/base/jquery.ui.progressbar.css",
            "~/Content/themes/base/jquery.ui.theme.css"));

bundles.Add(new StyleBundle("~/bundles/BootStrapcss").Include(
            "~/BootStrap/css/bootstrap.css",
            "~/BootStrap/css/bootstrap-fileupload.css"));

bundles.Add(new StyleBundle("~/bundles/BootStrap").Include(
            "~/BootStrap/tpg-main.css",
            "~/BootStrap/tpg-internal.css"));

bundles.Add(new ScriptBundle("~/bundles/BootStrapjs").Include(
            "~/BootStrap/js/bootstrap-fileupload.js",
            "~/BootStrap/js/bootstrap.js"));

BundleTable.EnableOptimizations = true;

应该保留我所拥有的,还是将我所有的脚本文件打包成一个ScriptBundle,将我所有的样式打包成一个StyleBundle?我想达到最好的性能。

4

1 回答 1

15

如果您总是使用所有文件,请继续将它们分成两个捆绑包;一种用于 javascript,一种用于样式。更少的 bundle 意味着更少的请求到服务器来获取资源,这可能会导致第一次命中时的性能稍微好一些;随后文件将被浏览器缓存。

如果您并不总是使用所有文件,那么将它们分成更多包更有意义。

于 2013-09-25T14:56:50.043 回答