在构建网站时,我应该在特定用户控件或母版页中引用javascript库吗?

时间:2021-12-02 19:43:53

which of following cases is strongest with regard to client side (JavaScript) library referencing and loading.

以下哪个案例在客户端(JavaScript)库引用和加载方面最强。

Please note that I assume the web solution is well designed so that the components are well encapsulated

请注意,我认为Web解决方案设计合理,因此组件封装良好

Also, just to be clear, the master page I am talking about could be a user control or anything that holds all library references and loaded at the site-load regardless of the content of the site at the moment

另外,为了清楚起见,我正在谈论的母版页可以是用户控件或任何包含所有库引用并在站点加载时加载的内容,无论此时网站的内容如何

1 - include references (link href) for all custom (e.g. 3rd party) libraries in the master page.

1 - 包括母版页中所有自定义(例如第三方)库的引用(链接href)。

  • it's arguable this is easy to maintain because all references are in one place.

    这是有争议的,这很容易维护,因为所有参考都在一个地方。

  • it's also arguable that it's easy to perform library upgrades so you don't need to hunt within the solution where the library is referred to update it's reference.

    它也是有争议的,它很容易执行库升级,所以你不需要在引用库的解决方案中寻找更新它的参考。

2 - include reference to libraries in their specific user controls

2 - 在其特定用户控件中包含对库的引用

  • shouldn't this improve performance as the website only loads libraries when required. e.g. one can refer to the JavaScript libraries that works for maps within the user control that deals with maps with contrast to loading maps libraries even when there's no map on the current view.

    这不应该提高性能,因为网站只在需要时加载库。例如一个人可以参考用于控制地图的用户控件中的地图的JavaScript库,即使在当前视图上没有地图时也能加载地图库。

  • This encapsulates components well. for example, a developer who tries to fix a bug in the map component will see all the libraries it refers to when he opens the map user control. This stops the need to go through the master page and see which libraries are used.

    这很好地封装了组件。例如,尝试修复地图组件中的错误的开发人员将在打开地图用户控件时看到它引用的所有库。这样就无需通过母版页查看使用的库。

1 个解决方案

#1


1  

I don't think this is the right way to think about it.

我不认为这是思考它的正确方法。

The only reason not to load all libraries is performance. The debug reason you give is too minor to consider.

不加载所有库的唯一原因是性能。您提供的调试原因太小,无法考虑。

You can increase performance in many different ways. By far the easiest, most straightforward and attainable is to minify all your JavaScript into one file. You can additionally use a compiler like Google's Closure Compiler or Yahoo YUI Compressor to get a smaller filesize and faster execution.

您可以通过多种方式提高性能。到目前为止,最简单,最直接和最容易实现的是将所有JavaScript缩小为一个文件。您还可以使用像Google的Closure Compiler或Yahoo YUI Compressor这样的编译器来获得更小的文件大小和更快的执行速度。

If at this point you still have performance issues (that can be attributed to network latency) then maybe look into lazy loading your libraries.

如果此时您仍然遇到性能问题(可能归因于网络延迟),那么可能会考虑延迟加载库。

This doesn't mean you should forgo the guidelines of dependency injection. Any good framework will offer you a mechanism for dependency injection. This should make switching to solution 2 at a later point fairly trivial.

这并不意味着你应该放弃依赖注入的指导方针。任何好的框架都会为您提供依赖注入的机制。这应该使得稍后切换到解决方案2相当微不足道。

#1


1  

I don't think this is the right way to think about it.

我不认为这是思考它的正确方法。

The only reason not to load all libraries is performance. The debug reason you give is too minor to consider.

不加载所有库的唯一原因是性能。您提供的调试原因太小,无法考虑。

You can increase performance in many different ways. By far the easiest, most straightforward and attainable is to minify all your JavaScript into one file. You can additionally use a compiler like Google's Closure Compiler or Yahoo YUI Compressor to get a smaller filesize and faster execution.

您可以通过多种方式提高性能。到目前为止,最简单,最直接和最容易实现的是将所有JavaScript缩小为一个文件。您还可以使用像Google的Closure Compiler或Yahoo YUI Compressor这样的编译器来获得更小的文件大小和更快的执行速度。

If at this point you still have performance issues (that can be attributed to network latency) then maybe look into lazy loading your libraries.

如果此时您仍然遇到性能问题(可能归因于网络延迟),那么可能会考虑延迟加载库。

This doesn't mean you should forgo the guidelines of dependency injection. Any good framework will offer you a mechanism for dependency injection. This should make switching to solution 2 at a later point fairly trivial.

这并不意味着你应该放弃依赖注入的指导方针。任何好的框架都会为您提供依赖注入的机制。这应该使得稍后切换到解决方案2相当微不足道。