SlideShare a Scribd company logo
SOLVING COMMON
WEB COMPONENT
PROBLEMS
with Server Side Rendering
SOLVING COMMON
WEB COMPONENT
PROBLEMS
with Server Side Rendering
using Enhance!
SIMON MACDONALD
@MACDONST@MASTODON.ONLINE
INDEX
What are Web Components?
Why are they useful?
What are their problems?
How do we solve them?
WHAT ARE WEB COMPONENTS?
WHAT ARE WEB COMPONENTS?
Web Components is a suite of different
technologies allowing you to create
reusable custom elements — with their
functionality encapsulated away from
the rest of your code — and utilize them
in your web apps.
– MDN
〞
“ OH, YOU MEAN REACT
Solving Common Web Component Problems - Simon MacDonald
Let's address the
elephant in the
room right away
REACT WC'S
Reusable Components
REACT WC'S
Reusable Components
DOM Syncing
REACT WC'S
Reusable Components
DOM Syncing
Stateful
REACT WC'S
Reusable Components
DOM Syncing
Stateful
Not the platform
REACT WC'S
Reusable Components
DOM Syncing
Stateful
Not the platform
REACT
Reusable Components
WC'S
Reusable Components
DOM Syncing
Stateful
Not the platform
REACT
Reusable Components
Interoperable
WC'S
Reusable Components
DOM Syncing
Stateful
Not the platform
REACT
Reusable Components
Interoperable
Accessible
WC'S
Reusable Components
DOM Syncing
Stateful
Not the platform
REACT
Reusable Components
Interoperable
Accessible
Part of the platform
WC'S
WHAT ARE WEB COMPONENTS (PART DEUX)?
WHAT ARE WEB COMPONENTS (PART DEUX)?
Custom Elements
WHAT ARE WEB COMPONENTS (PART DEUX)?
Custom Elements
Shadow DOM
WHAT ARE WEB COMPONENTS (PART DEUX)?
Custom Elements
Shadow DOM
HTML Templates
CUSTOM ELEMENTS
https://codepen.io/macdonst/embed/ZErMzrr?default-
tab=html%2Cresult&editable=true
CUSTOM ELEMENTS
<hello-world name="Simon">
<h1>Hello Simon</h1>
</hello-world>
1
2
3
SHADOW DOM
https://codepen.io/macdonst/embed/oNdpNKQ?default-
tab=html%2Cresult&editable=true
SHADOW DOM
<hello-world name="Simon">
#shadow-root
<h1>Hello <span>Simon</span></h1>
</hello-world>
1
2
3
4
HTML TEMPLATES
https://codepen.io/macdonst/embed/vYjpYpV?default-
tab=html%2Cresult&editable=true
HTML TEMPLATES
<hello-world>
#shadow-root
<h1>
Hello <slot name="name"></slot>
</h1>
<span slot="name">Simon</span>
</hello-world>
1
2
3
4
5
6
7
INDEX
What are Web Components?
Why are they useful?
What are their problems?
How do we solve them?
WHY ARE WEB COMPONENTS USEFUL?
WHY ARE WEB COMPONENTS USEFUL?
Component reuse and interoperability
WHY ARE WEB COMPONENTS USEFUL?
Uses less JavaScript
Component reuse and interoperability
WHY ARE WEB COMPONENTS USEFUL?
Uses less JavaScript
Accessible
Component reuse and interoperability
WHY ARE WEB COMPONENTS USEFUL?
Uses less JavaScript
Accessible
Shorter learning path
Component reuse and interoperability
WHY ARE WEB COMPONENTS USEFUL?
Uses less JavaScript
Accessible
Shorter learning path
Encapsulation
Component reuse and interoperability
ASIDE IS ANYONE
ACTUALLY USING WEB
COMPONENTS?
SURE SEEMS LIKE IT
https://chromestatus.com/metrics/feature/timeline/popularity/1689
APPS MADE WITH WEB COMPONENTS
INDEX
What are Web Components?
Why are they useful?
What are their problems?
How do we solve them?
PROBLEM: FLASH OF UNSTYLED CONTENT (FOUC)
PROBLEM: FLASH OF UNSTYLED CONTENT (FOUC)
A flash of unstyled content is an
instance where a web page appears
briefly with the browser's default styles
prior to loading an external CSS
stylesheet, due to the web browser
engine rendering the page before all
information is retrieved
– Wikipedia
〞
PROBLEM: FLASH OF UNDEFINED CUSTOM
ELEMENTS (FOUCE)
PROBLEM: FLASH OF UNDEFINED CUSTOM
ELEMENTS (FOUCE)
You may see a brief flash of unstyled
HTML where your custom elements
should be when the page loads. This is
not dissimilar to FOUC, which occurs
when HTML is displayed before the
stylesheet has loaded.
– Cory LaViska
〞
FOUCE EXAMPLE
https://codepen.io/macdonst/embed/poVaVeN?default-
tab=html%2Cresult&editable=true
WHY DOES FOUCE HAPPEN?
WHY DOES FOUCE HAPPEN?
Browser requests HTML
WHY DOES FOUCE HAPPEN?
Browser requests HTML
It encounters an unknown element, "hello-world"
WHY DOES FOUCE HAPPEN?
Browser requests HTML
It encounters an unknown element, "hello-world"
The browser treats the unknown element as an inline element
No styles are applied due to the Shadow DOM boundary
WHY DOES FOUCE HAPPEN?
Browser requests HTML
It encounters an unknown element, "hello-world"
The browser treats the unknown element as an inline element
No styles are applied due to the Shadow DOM boundary
Browser loads the web components JavaScript
WHY DOES FOUCE HAPPEN?
Browser requests HTML
It encounters an unknown element, "hello-world"
The browser treats the unknown element as an inline element
No styles are applied due to the Shadow DOM boundary
Browser loads the web components JavaScript
The web component is instantiated, and the encapsulated
CSS & JavaScript is applied
FOUCE HAPPENS!!!
PROBLEM: SHADOW DOM DOESN'T PLAY WELL WITH
NATIVE FORMS
PROBLEM: SHADOW DOM DOESN'T PLAY WELL WITH
NATIVE FORMS
Custom elements cannot extend elements other than
HTMLElement*
PROBLEM: SHADOW DOM DOESN'T PLAY WELL WITH
NATIVE FORMS
Custom elements cannot extend elements other than
HTMLElement*
Form elements inside the Shadow DOM are not
considered as such by the component's parent form.
SHADOW DOM VS FORMS EXAMPLE
https://codepen.io/macdonst/embed/bGMLKBR?default-
tab=html%2Cresult&editable=true
INDEX
What are Web Components?
Why are they useful?
What are their problems?
How do we solve them?
SOLUTIONS
FOUCE SOLUTION
https://codepen.io/macdonst/embed/bGMLKmL?default-
tab=html%2Cresult&editable=true
DECLARATIVE SHADOW DOM
DECLARATIVE SHADOW DOM
Proposal to allow rendering elements
with shadow DOM (aka web
components) using server-side
rendering.
〞
DECLARATIVE SHADOW DOM
https://codepen.io/macdonst/embed/gOQwyJO?default-
tab=html%2Cresult&editable=true
DECLARATIVE SHADOW DOM
<hello-world>
#shadow-root
<style>
h1 { color: red }
</style>
<h1>
Hello <slot name="name"></slot>
</h1>
<span slot="name">Simon
</span></hello-world>
1
2
3
4
5
6
7
8
9
10
Solving Common Web Component Problems - Simon MacDonald
Solving Common Web Component Problems - Simon MacDonald
SHADOW DOM DOESN'T PLAY WELL WITH NATIVE
FORMS SOLUTION
SHADOW DOM DOESN'T PLAY WELL WITH NATIVE
FORMS SOLUTION
SHADOW DOM DOESN'T PLAY WELL WITH NATIVE
FORMS SOLUTION
SHADOW DOM DOESN'T PLAY WELL WITH NATIVE
FORMS SOLUTION
SHADOW DOM DOESN'T PLAY WELL WITH NATIVE
FORMS SOLUTION
FACE* AND ELEMENT INTERNALS
https://codepen.io/rbethel/embed/zYMeZEd
*Form Associated Custom Elements
IS THERE A SOLUTION WE CAN USE TODAY?
〞
We can't solve problems by using the
same kind of thinking we used when we
created them.
– Albert Einstein
Solving Common Web Component Problems - Simon MacDonald
WHAT IS ENHANCE?
AUTHOR
Enhance allows developers to
write components as pure
functions that return HTML.
Then render them on the server
to deliver complete HTML
immediately available to the
end user.
Enhance takes care of the
tedious parts, allowing you to
use today’s Web Platform
standards more efficiently. As
new standards and platform
features become generally
available, Enhance will make
way for them.
STANDARDS
Enhance allows for easy
progressive enhancement so
that working HTML can be
further developed to add
additional functionality with
JavaScript.
PROGRESSIVE
Enhance is a web standards-based HTML framework. It’s
designed to provide a dependable foundation for building
lightweight, flexible, and future-proof web applications.
ENHANCE FOUCE SOLUTION
export default function HelloWorld({ html }) {
return html`
<style>
h1 { color: red }
</style>
<h1>
Hello <slot name="name"></slot>
</h1>`
}
1
2
3
4
5
6
7
8
9
<hello-world>
<span slot="name">Simon</slot>
</hello-world>
1
2
3
HTML
Javascript
https://bird-3st.begin.app/fouce
https://bird-3st.begin.app/fouce
ENHANCE FOUCE SOLUTION
https://codepen.io/macdonst/embed/wvNPLep?default-
tab=html%2Cresult&editable=true
STOPPING FOUCE
STOPPING FOUCE
Browser requests HTML
STOPPING FOUCE
Browser requests HTML
Enhance expands your custom elements on the server as well
as hoisting your encapsulated styles to the page's head tag
STOPPING FOUCE
Browser requests HTML
Enhance expands your custom elements on the server as well
as hoisting your encapsulated styles to the page's head tag
It encounters an unknown element, "hello-world"
STOPPING FOUCE
Browser requests HTML
Enhance expands your custom elements on the server as well
as hoisting your encapsulated styles to the page's head tag
It encounters an unknown element, "hello-world"
The browser treats the unknown element as an inline
element, but your CSS is already loaded, so it is styled
properly
STOPPING FOUCE
Browser requests HTML
Enhance expands your custom elements on the server as well
as hoisting your encapsulated styles to the page's head tag
It encounters an unknown element, "hello-world"
The browser treats the unknown element as an inline
element, but your CSS is already loaded, so it is styled
properly
Browser loads the web components JavaScript
STOPPING FOUCE
Browser requests HTML
Enhance expands your custom elements on the server as well
as hoisting your encapsulated styles to the page's head tag
It encounters an unknown element, "hello-world"
The browser treats the unknown element as an inline
element, but your CSS is already loaded, so it is styled
properly
Browser loads the web components JavaScript
The web component is instantiated, and your custom element
is "enhanced"
ENHANCE SHADOW DOM VS FORMS SOLUTION
export default function MyButton({ html }) {
return html`
<style>
button {
color: white;
background-color: black;
border-radius: 3px;
}
</style>
<button>
<slot></slot>
</button>`
}
1
2
3
4
5
6
7
8
9
10
11
12
13
<form action="javascript:alert('hello')">
<input type="text" name="name">
<input type="password" name="password">
<my-button>
Login
</my-button>
</form>
1
2
3
4
5
6
7
HTML
Javascript https://bird-3st.begin.app/forms
https://bird-
3st.begin.app/forms
ENHANCE SHADOW DOM VS FORMS SOLUTION
https://codepen.io/macdonst/embed/RwvjzQV?default-
tab=html%2Cresult&editable=true
BENEFITS OF ENHANCE
BENEFITS OF ENHANCE
All components are server side renderable
with no changes.
BENEFITS OF ENHANCE
All components are server side renderable
with no changes.
Leverage your CSS skills and style
encapsulation in the light DOM.
BENEFITS OF ENHANCE
All components are server side renderable
with no changes.
Leverage your CSS skills and style
encapsulation in the light DOM.
Use slots without the shadow DOM
BENEFITS OF ENHANCE
All components are server side renderable
with no changes.
Leverage your CSS skills and style
encapsulation in the light DOM.
Use slots without the shadow DOM
Avoid excessive JavaScript code by
leveraging the platform and web standards
Solving Common Web Component Problems - Simon MacDonald
THANKS!

More Related Content

Solving Common Web Component Problems - Simon MacDonald