Most performant client-side clientlib implementation
Hi all,
I'm wondering what the most performant way to include client-side clientlibs is.
Example: Assume you have to develop a couple of components each including their own client-side JavaScript, like a Google Map component and a carousel component (e.g. Slick) .
As far as I can see, there are a couple of options:
- Create a postloaded clientlib category which includes all the client-side logic and call it in HTL it in every page footer.
+: There is only one instance of the client-side script
-: Pages not who don't need Google Maps or Slick also load the libraries, which are not needed and often take time initializing. - Embed the HTL clientlib call in the component to include the clientlibs
+: Clear separation of client-side code per component
-: The clientlib will be included for every component, so if you have 3 carousels, the clientlib will be added and executed 3 times, not utilizing clientlibs to their maximum potential.
-: Dependencies on frameworks (like jQuery) are not fulfilled since these are loaded in the page footer (as per best practice) and are not initialized yet during the render phase of the component. - Embed the HTL clientlib call in specific templates
+: Clientlibs are not loaded on all pages, only on pages that allow specific components on the parsys.
-: When you change the component mapping (allowing Google Maps on other templates), you need to keep the clientlib categories in line with the component mapping and dynamic changes to the component mapping (using design mode) are not possible. - Resolve all components in a SlingModel when rendering the page template to figure out which components are added to the page and map these to clientlib categories.
+: Only the required clientlibs are added to the page.
+: Dynamic adding of components to templates will still work
-: Performance impact on every page load since you will have to loop through all the nodes on the parsys.
Am I missing another (better) option that only includes client-side JavaScripts that are required and allows code separation per component?
If not, which of these options above is considered best practice?
Thanks!