向BundleCollection添加一个脚本文件有什么价值? [英] Is it any value of adding one script file to BundleCollection?

查看:50
本文介绍了向BundleCollection添加一个脚本文件有什么价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在设计一个非常大的应用程序,我想为将来提供良好的基础架构.

I am designing a very big application now and i want to make good infrastructure for the future.

在Razor(MVC)中,通过指定特定文件,可以使用功能 @ Scripts.Render(〜/Scripts/myscript.js")在视图中包含javascript文件.或BundleConfig.cs中定义的BundleCollection中的捆绑商品.

In Razor (MVC) there is an option to include javascript files in a view by using the function @Scripts.Render("~/Scripts/myscript.js") by specify a specific file or bundle item from BundleCollection which defined in BundleConfig.cs.

我想知道,在Scripts.Render函数中仅定义单个文件,而不指定脚本文件本身,是否有任何附加值?

I am wondering, is there any added value to define a bundle item in the Scripts.Render function of only single file instead of specify the script file itself?

我发现一个附加值是,从长远来看,我将能够根据需要向捆绑包添加更多文件,而无需更改使用捆绑包的看法,我将享受优化.但是当我仍在捆绑包"中使用一个js文件时,我找不到相应的附加值.

one of added value i found is that in the long run i will be able to add more files to the bundle as needed without changing the views that using it and i will enjoy the optimization. but i couldn't find added value for that while i am still using one js file in the "bundle".

推荐答案

除了您提到的好处外,还有很多好处:

There is a couple of benefits actually apart from the one you have mentioned:

  1. 在发布模式下捆绑包会缩小.当您将文件添加到捆绑包(甚至是单个文件)并在视图中引用捆绑包时,代码也会被缩小.直接引用文件不是这种情况.这很可能是您要在生产中使用的.
  2. 该框架会使用捆绑软件的每个新版本生成一个随机哈希并将其附加到脚本中,以使先前捆绑软件的缓存无效.看起来像这样的< script src ="/bundles/myscript.js?v = CQJxkNd9QnrvutTyUG9mM-vD0FrbCc1"</script> .每个新版本发布后,您的客户都会感谢您.再说一次-如果您直接引用文件,除非您手动生成并附加版本,否则不会发生.
  1. Bundles are minified in release mode. When you add a file to the bundle (even a single file) and reference the bundle in the view the code will be minified. It's not the case when you reference the file directly. That's most probably what you're going for in production.
  2. The framework generates and appends a random hash to the script with every new version of the bundle to invalidate the cache of the previous bundles. It looks something like this <script src="/bundles/myscript.js?v=CQJxkNd9QnrvutTyUG9mM-vD0FrbCc1"></script>. Your clients will thank you for this after every new release. And again - it doesn't happen if you reference the file directly unless you generate and append the versions manually.

所以我通常会做的,并且我认为这是一个好习惯,直到被证明是错误的为止,我会为每个页面的特定需求创建一个包,即使它包含一个带有小模块的js文件也是如此.例如首页:

So what I usually do, and I consider it a good practice until I'm proven wrong, I create a bundle for every page specific needs even if it consists of a single js file with a tiny module. For example Home page:

bundles.Add(new ScriptBundle(〜/bundles/home").Include(〜/Scripts/home.js"))); @ Scripts.Render(〜/bundles/home")

这篇关于向BundleCollection添加一个脚本文件有什么价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆