when building a website, should I refer to javascript libraries in specific user controls or in the master page?

StackOverflow https://stackoverflow.com/questions/19631853

Frage

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

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

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.

  • 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

  • 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.

  • 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.

War es hilfreich?

Lösung

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.

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top