Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ---
  2. title: Optimizing The Widget Set
  3. order: 29
  4. layout: page
  5. ---
  6. [[optimizing-the-widget-set]]
  7. = Optimizing the widget set
  8. Vaadin contains a lot of components and most of those components
  9. contains a client side part which is executed in the browser. Together
  10. all the client side implementations sum up to a big amount of data the
  11. enduser needs to download to the browser even if he might never use all
  12. the components.
  13. For that reason Vaadin uses three strategies for downloading the
  14. components:
  15. [[eager]]
  16. Eager
  17. +++++
  18. What eager means is that the client implementation for the component is
  19. included in the payload that is initially downloaded when the
  20. application starts. The more components that is made eager the more will
  21. need to be downloaded before the initial view of the application is
  22. shown. By default Vaadin puts most components here since Vaadin does not
  23. know which components will be used in the first view and cannot thus
  24. optimize any further. You would have noticed this if you ever made a
  25. Hello World type of application and wondered why Vaadin needed to
  26. download so much for such a simple application.
  27. [[deferred]]
  28. Deferred
  29. ++++++++
  30. When marking a component as deferred it means that its client side
  31. implementation will be downloaded right after the initial rendering of
  32. the application is done. This can be useful if you know for instance
  33. that a component will soon be used in that application but is not
  34. displayed in the first view.
  35. [[lazy]]
  36. Lazy
  37. ++++
  38. Lazy components client side implementation doesn't get downloaded until
  39. the component is shown. This will sometimes mean that a view might take
  40. a bit longer to render if several components client side implementation
  41. needs to first be downloaded. This strategy is useful for components
  42. that are rarely used in the application which everybody might not see.
  43. `RichTextArea` and `ColorPicker` are examples of components in Vaadin that by
  44. default are Lazy.
  45. [[optimizing-the-loading-of-the-widgets]]
  46. Optimizing the loading of the widgets
  47. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  48. Now that we know what Vaadin provides, lets see how we can modify how a
  49. component is loaded to provide the best experience for our application.
  50. Lets say we want to build a HelloWorld application which only needs a
  51. few components. Specifically these components will be shown on the
  52. screen:
  53. * UI - The UI of the application.
  54. * VerticalLayout - The Vertical layout inside the UI where the message
  55. is shown
  56. * Label - A label with the text "Hello World"
  57. All other Vaadin components provided by Vaadin we don't want to load. To
  58. do this we are going to mark those three components as Eager (initially
  59. loaded) and all the rest as Lazy.
  60. To do that we need to implement our own `ConnectorBundleLoaderFactory`.
  61. Here is my example one:
  62. [source,java]
  63. ....
  64. public class MyConnectorBundleLoaderFactory extends
  65. ConnectorBundleLoaderFactory {
  66. private static final List<Class> eagerComponents = new
  67. LinkedList<Class>();
  68. static {
  69. eagerComponents.add(UI.class);
  70. eagerComponents.add(VerticalLayout.class);
  71. eagerComponents.add(Label.class);
  72. }
  73. @Override
  74. protected LoadStyle getLoadStyle(JClassType connectorType){
  75. Connect annotation = connectorType.getAnnotation(Connect.class);
  76. Class componentClass = annotation.value();
  77. // Load eagerly marked connectors eagerly
  78. if(eagerComponents.contains(componentClass)) {
  79. return LoadStyle.EAGER;
  80. }
  81. //All other components should be lazy
  82. return LoadStyle.LAZY;
  83. }
  84. }
  85. ....
  86. We also need to add our factory to the widgetset by adding the following
  87. to our <widgetset>.gwt.xml:
  88. [source,xml]
  89. ....
  90. <generate-with class="com.example.widgetsetoptimization.MyConnectorBundleLoaderFactory">
  91. <when-type-assignable class="com.vaadin.client.metadata.ConnectorBundleLoader" />
  92. </generate-with>
  93. ....
  94. If you are using the Eclipse Plugin to compile the widgetset you will
  95. also want to add the following meta data for the compiler so it does not
  96. overwrite our generator setting:
  97. [source,xml]
  98. ....
  99. <!-- WS Compiler: manually edited -->
  100. ....
  101. If you have used the Maven archetype for setting up your project, you
  102. might need to add vaadin-client-compiler as a dependency in your project
  103. as it is by default only used when actually starting the widgetset
  104. compiler. See http://dev.vaadin.com/ticket/11533 for more details.
  105. Finally, here is my simple test application UI for which I have
  106. optimized the widgetset:
  107. [source,java]
  108. ....
  109. public class HelloWorldUI extends UI {
  110. @Override
  111. protected void init(VaadinRequest request) {
  112. VerticalLayout layout = new VerticalLayout();
  113. layout.addComponent(new Label("Hello world"));
  114. setContent(layout);
  115. }
  116. }
  117. ....
  118. Now, all I have to do is recompile the widgetset for the new load
  119. strategy to take effect.
  120. If you now check the network traffic when you load the application you
  121. will notice a *huge difference*. Using the default widgetset with the
  122. default loading strategy our Hello World application will load over *1
  123. Mb* of widgetset data. If you then switch to using our own widgetset
  124. with our own custom loader factory the widgetset will only be about *375
  125. kb*. That is over *60% less!*
  126. Using your own custom widgetset loader factory is highly recommended in
  127. all projects.
  128. [[finding-out-which-components-are-loaded-by-a-view]]
  129. Finding out which components are loaded by a View
  130. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  131. So you want to start optimizing your widgetset but how do you find out
  132. which components are needed for the initial view so you can make them
  133. eager while keeping everything else deferred or lazy? Fortunately there
  134. is an addon
  135. https://vaadin.com/directory#addon/widget-set-optimizer[WidgetSetOptimizer]
  136. for doing just this.
  137. To use it you download this addon and add it to your project.
  138. Add the following to the <widgetset>.gwt.xml:
  139. [source,xml]
  140. ....
  141. <inherits name="org.vaadin.artur.widgetsetoptimizer.WidgetSetOptimizerWidgetSet" />
  142. ....
  143. You will also need to add the following to your UI classes init method
  144. [source,java]
  145. ....
  146. new WidgetSetOptimizer().extend(this);
  147. ....
  148. Finally compile the widgetset and run the application with the &debug
  149. parameter. In the debug window there will be a new button "OWS" which by
  150. pressing you will get the Generator class automatically generated for
  151. you. The generated generator class will mark the currently displayed
  152. components as Eager while loading everything else as Deferred. More
  153. information about the addon and its usage can be found on the Addon page
  154. in the directory.