Share
Embed
[Dreamforce 24] Dynamic LWC: With great power comes great responsibility
Dynamic LWC is a long awaited feature. No need to hardcode LWC names, create them dynamically on the fly. It's great but can have a severe impact on performance if used incorrectly. Let’s deep dive!
Fabien Taillon
September 18, 2024
Transcript
30 slides
-
Fabien Taillon, Partner & CTO at Texeï [email protected] Dynamic Lightning
Web Components With great power comes great responsibility -
Fabien Taillon Partner & CTO at Texeï Salesforce MVP -
Hall of Fame Paris Developer Group leader French Touch Dreamin team https://www.texei.com/blog -
What is a Dynamic Component A way to dynamically instantiate
a component that may not be known before runtime @FabienTaillon - https://www.texei.com/blog -
We have purposely made the decision of not allowing dynamic
component creation yet (the equivalent of $A.createComponent) on the Salesforce platform to make sure we had a clean, statically analyzable and predictable behavior on which to build upon in LWC. … In LWC in the platform we don't allow you to shoot yourself in the foot creating small tiny components, and once we do, we will give you a water pistol first :) Diego Ferreiro Val - Ex Principal Architect LWC https://salesforce.stackexchange.com/questions/244847/not-able-to-render-dynamic-lightning-web-component -
Valid use cases Most use cases can be achieved with
existing lwc:if/lwc:elseif, but: • N°1 use case would be for AppExchanges allowing dynamic components defined through Metadata for instance (think App Builder like) • Can be used to defer big components load if not on the main displayed page • Displayed after a button click • Displayed in another tab • … @FabienTaillon - https://www.texei.com/blog -
How it works now Just add the capability to the
parent component to instantiate dynamic components: <?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>59.0</apiVersion> <capabilities> <capability>lightning__dynamicComponent</capability> </capabilities> </LightningComponentBundle> @FabienTaillon - https://www.texei.com/blog -
How It Works Now And then it’s very easy: •
Import component in JS • import MyComponent from 'c/myComponent'; • await import('c/myComponent'); • Define a Dynamic component in template with lwc:component and lwc:is • <lwc:component lwc:is={componentConstructor}></lwc:component> • Assign in JS the component you want to render • this.componentConstructor = MyComponent; @FabienTaillon - https://www.texei.com/blog -
Pass Attributes Down Easily pass attributes via markup <template> <lwc:component
lwc:is={componentConstructor} text="I love dynamic components!"></lwc:component> </template> Depending on dynamic components, you may not know which attributes are need. Properties can be passed down via JavaScript object and lwc:spread directive: JS childProps = { city: "San Francisco", state: "CA" }; Markup <template> <lwc:component lwc:is={componentConstructor} lwc:spread={childProps}></lwc:component> </template> @FabienTaillon - https://www.texei.com/blog -
Before Dynamic Components Declarative use of components in the template
markup All components are known at save time and can be statically analyzed @FabienTaillon - https://www.texei.com/blog -
Before Dynamic Components Only one round trip to get the
whole component and sub-components @FabienTaillon - https://www.texei.com/blog c-my-component c-my-component + c-small-component + c-heavy-component -
With Dynamic Components Dynamic components will be created by JS
and may not be known at that time Depends on how dynamic components are created - more on this later @FabienTaillon - https://www.texei.com/blog -
With Dynamic Components If not done correctly, one additional round
trip per dynamic component @FabienTaillon - https://www.texei.com/blog c-my-component c-my-component + c-small-component c-heavy-component c-heavy-component -
Dynamic Components To do it correctly, you need to understand
how it works and different options available @FabienTaillon - https://www.texei.com/blog -
Static Import - Statically Analyzable • Import needed components •
Switch the rendered component by changing the lwc:is variable • Almost the same as using components in markup and lwc:if • Everything is downloaded with a single server round trip @FabienTaillon - https://www.texei.com/blog -
Dynamic Import - Statically Analyzable • Same as previous, but
loading is done on demand ◦ connectedCallback ◦ Function linked to any action (user click…) • Not part of the initial load so additional server round trip @FabienTaillon - https://www.texei.com/blog -
Dynamic Import - Not Statically Analyzable • Same as previous
BUT component is not statically analyzable • No way for Salesforce to improve loading, caching etc • As much as possible, don’t use this one @FabienTaillon - https://www.texei.com/blog -
Loading Heavy Component Async • One heavy component • Not
visible during first page load • Would slow down the whole page if part of the markup @FabienTaillon - https://www.texei.com/blog -
Recap • Use it when you understand how it works
• Not for very small components, server round trip will be slower than initial load • Use it for big components that may slow down initial load and that are not immediately visible (like unclicked tab) @FabienTaillon - https://www.texei.com/blog -
Resources Documentation https://developer.salesforce.com/docs/platform/lwc/guide/js- dynamic-components.html Blog post https://developer.salesforce.com/blogs/2023/10/dynamic-co mponents-for-lightning-web-components-is-now-ga RFC https://github.com/jmsjtu/lwc-rfcs/blob/jtu/dynamic-compon
ent-creation/text/0119-dynamic-component-creation.md StackExchange answer about performance https://salesforce.stackexchange.com/questions/244847/not- able-to-render-dynamic-lightning-web-component @FabienTaillon - https://www.texei.com/blog -
Attend the Developer Keynote Build the future with the Salesforce
Platform Thursday, Sept. 19 | 2:30 PM PT InterContinental, L3, Grand Ballroom ABC sforce.co/devkeynote -
Coffee on us. The first 4,000 attendees to provide feedback
on this event will receive a $5 Starbucks gi card. *Restrictions apply. See rules at sforce.co/survey-terms Open the Salesforce Events Mobile App. Navigate to My Event. Select My Surveys. Complete four Session Surveys and present the completed Event Survey page at Badge Pickup to redeem on Thursday, September 19.*