KEMBAR78
Web components: A simpler and faster react | PDF
Web Components:
A simpler and faster React
#UseThePlatform to build a better web
Chris Lorenzo / @chiefcll
Senior Principal Engineer
Web Components:
A simpler and faster ...
#UseThePlatform to build a better web
Chris Lorenzo / @chiefcll
Senior Principal Engineer
Questions...
Workshop:
Introduction to Web Components & Polymer
Little Rock Tech Fest - October 5, 2017
John Riviello
@JohnRiv
Chris Lorenzo
@Chiefcll
http://tinyurl.com/lrtf-polymer
Google PolymerWeb Components
● Largest Cable TV & Internet
Service Provider in US
● Fortune 50 company
● 30+ million customers
● Owns NBCUniversal
● 500+ Components
Comcast
© Comcast
ABOUT ME
● Married 6 years
● 10 Years @ Comcast
● FE dev 16 years
● Son and daughter
Theo & Josie
New York City
Philadelphia
© Comcast
© Comcast
© Comcast
© Comcast
© Comcast
© Comcast
My
Coding
Journey
Programming Fu (Mastery)
Time
Fulevel
2012
2012
© Comcast
© Comcast
2017
Programming Fu Level
Time
Fulevel
20172013
How do I build an app?
How do I maintain an app?
Which
Way?
My
Journey
It’s
Hard
That’s why we love
Frameworks (+Libraries)
Follow the Path
No one knows how to structure a Front End
application. I've worked in this industry for years,
and "best practices" are never taught. Instead,
"libraries" are taught. jQuery? Angular? Backbone?
Source: http://blog.andrewray.me/flux-for-stupid-people/
-@andrewray
"Paris - September | October 2012" by Nan Palmero is licensed under CC BY 2.0
What makes an app simple?
Simplicity Matters
-Rich Hickey
source: https://upload.wikimedia.org/wikipedia/commons/a/ac/IndianElephant.jpg
Components
Important Concepts to Understand
• Encapsulation
• Composition
• Separation of
Concerns
• Functional
Decomposition "separated" by hansol is licensed under CC BY 2.0
React
A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES
React is all about building reusable components.
In fact, with React the only thing you do is build
components. Since they're so encapsulated,
components make code reuse, testing, and
separation of concerns easy.
Source: https://facebook.github.io/react/docs/why-react.html
State => View (DOM)
import React, { Component } from 'react';
export default class HiMessage extends Component {
render() {
return <h1>Hi {this.props.name}!</h1>;
}
}
<HiMessage name="Chris">
import React, { Component } from 'react';
export default class HiMessage extends Component {
render() {
return <h1>Hi {this.props.name}!</h1>;
}
}
<HiMessage name="Chris">
We can be creating the exact same
programs out of significantly simpler
components
Source: https://www.youtube.com/watch?v=rI8tNMsozo0&t=805s
-Rich Hickey
import { Element } from 'polymer/polymer-element.js';
class HiMessage extends Element {
static get template() {
return `<h1>Hi [[name]]!</h1>`;
}
}
customElements.define('hi-message', HiMessage);
<hi-message name="Chris" />
import { Element } from 'polymer/polymer-element.js';
class HiMessage extends Element {
static get template() {
return `<h1>Hi [[name]]!</h1>`;
}
}
customElements.define('hi-message', HiMessage);
<hi-message name="Chris" />
import React, { Component } from 'react';
export default class HiMessage extends Component
{
render() {
return <h1>Hi {this.props.name}!</h1>;
}
}
<HiMessage name="Chris">
import { Element } from 'polymer/polymer-element.js';
class HiMessage extends Element {
static get template() {
return `<h1>Hi [[name]]!</h1>`;
}
}
customElements.define('hi-message', HiMessage);
<hi-message name="Chris" />
with
Existing Frameworks
Applications
Web Platform
Web Components built with Polymer (or not)
Vanilla Component
Custom Elements V1
source: https://upload.wikimedia.org/wikipedia/commons/a/ac/IndianElephant.jpg
<script src="https://polaris.xfinity.com/polaris.js" async></script>
<xc-header
is-authed="[[isAuthed]]"
login-url="/login"
tab="myxfinity"
sign-out-url="/logout">
</xc-header>
<script src="https://polaris.xfinity.com/polaris.js" async></script>
<xc-footer></xc-footer>
Rob Dodson:
Custom Elements
Everywhere
https://custom-elements-everywhere.com
import React, { Component } from 'react';
export default class HiMessage extends Component
{
render() {
return <h1>Hi {this.props.name}!</h1>;
}
}
<HiMessage name="Chris">
import { Element } from 'polymer/polymer-element.js';
class HiMessage extends Element {
static get template() {
return `<h1>Hi [[name]]!</h1>`;
}
}
customElements.define('hi-message', HiMessage);
<hi-message name="Chris" />
12kb50kb
VS
Performance
Matters
Performance
Matters
"stopwatch" by Julian Lim is licensed under CC BY 2.0 / Color adjusted from original
"Samsung Galaxy S7" by Kārlis Dambrāns is licensed under CC BY 2.0
Mobile Safari 10.1Chrome for
Android 54
Alex Russell:
Adapting to the
Mobile Web F̶u̶t̶u̶r̶e̶
Present
53% of users abandon a site that takes
longer than 3 seconds to load.
Source: https://www.soasta.com/blog/google-mobile-web-performance-study/
-Google
Mobile pages that are 1 second faster
experience up to 27% increase in
conversion rate.
Source: https://www.soasta.com/blog/mobile-web-performance-monitoring-conversion-rate/
Over 15s!
"Paris - September | October 2012" by Nan Palmero is licensed under CC BY 2.0
Using Web Components
Framework/Library Sizes (with GZIP compression)
Source: http://minime.stephan-brumme.com
React’s size is react.min.js + react-dom.min.js from https://cdnjs.com/libraries/react
jQuery’s size is from https://code.jquery.com/jquery-3.2.1.min.js
Polymer’s size computed locally based on v2.0.0-rc.4 with just polymer-element.html, and also with the non-legacy imports from polymer.html
123.7kb 57.4kb 34.6kb 26.2kb 11.3kb -
17.7kb
46.0kb
v2.12.1 v1.6.4 v3.2.1 v2.2.6 v2.0.0-rc4v15.5.4
Google Polymer Project
#UseThePlatform
Google Polymer Project
Your JS
Code +
Framework
Your JS
Code
Browser
Alex Russell:
Web Components:
Just in the Nick of
Time
Web Components
#UseThePlatform
CUSTOM ELEMENTS HTML IMPORTS
SHADOW DOMTEMPLATES
Source: http://webcomponents.org/
customElements.define('hi-message', HiMessage);
<template id="t">
<li>{item}</li>
</template>
<link rel="import" href="myc.html">
let shadowRoot = elm.attachShadow({mode: 'open'});
shadowRoot.innerHTML = 'Hi There!';
Source: http://webcomponents.org/
Edge Browser
https://developer.microsoft.com/en-us/microsoft-edge/platform/status/?q=sort%3AVotes
https://developer.microsoft.com/en-us/microsoft-edge/platform/status/?q=sort%3AVotes
Closer to the metal
"Samsung Galaxy S7" by Kārlis Dambrāns is licensed under CC BY 2.0
Mobile Safari 10.1Chrome for
Android 54
= nearly
100%
of mobile
What about Virtual Dom?
Programmers know the benefits of
everything and the tradeoffs of nothing.
Source: https://www.youtube.com/watch?v=rI8tNMsozo0
-Rich Hickey
Source: https://medium.com/@gethylgeorge/how-virtual-dom-and-diffing-works-in-react-6fc805f9f84e
Are you Facebook?
source: https://commons.wikimedia.org/wiki/File:Internet_Explorer_10_logo.svg
Is IE #1?
Justin Fagnani:
Efficient,
Expressive, and
Extensible HTML
Templates
`
<h1>${name}</h1>
<p>${content}</p>
`
html`
<h1>${name}</h1>
<p>${content}</p>
`
html`
<h1>${name}</h1>
<p>${content}</p>
`
var name = 'Chris';
var content = 'Loves Web Components!';
function html(strings, name, content) {
// strings[0] === <h1>
// strings[1] === </h1>
// name === Chris
return 'Whatever string you want';
}
Source: https://twitter.com/mathias/status/912223187005509632
Source: https://arewefastyet.com/#machine=29&view=single&suite=six-speed&subtest=templatestringtag-es6
What about building an App?
Unidirectional
Data Flow
<xc-app>
index.html
LogicStore {}
component-name-event-name
<xc-app>
index.html
Logic
xc-people-unpause-all-devices
Native Event
Store {}
What’s
That?<xc-app>
index.html
LogicStore {}
component-name-event-name
<xc-app>
index.html
Logic
xc-people-unpause-all-devices
Native Event
Store {}
component-name-event-name
<xc-app>
index.html
Store {} Logic
xc-people-unpause-all-devices
Native Event
component-name-event-name
<xc-app>
index.html
Store {} Logic
xc-people-unpause-all-devices
Native Event
Smart Parent
88 MPH + 1.21 gigawatts
= Time travel
+
<xc-app>
index.html
LogicStore {}
component-name-event-name
xc-people-unpause-all-devices
Native Event
<xc-app>
index.html
LogicStore {}
component-name-event-name
xc-people-unpause-all-devices
Native Event
Decouple
State UI
I’ve been married and divorced to so many
frontend frameworks that I want to stay a
bachelor for the rest of my life.
-Paul Palladino
Can you move it?
-Rich Hickey
Source: https://www.youtube.com/watch?v=rI8tNMsozo0
Store {}
"Software, testing, service" by testbytes is licensed under CC BY 2.0
Kevin Schaaf:
End to End Apps
with Polymer
In Conclusion
We can be creating the exact same
programs out of significantly simpler
components
Source: https://www.youtube.com/watch?v=rI8tNMsozo0&t=805s
-Rich Hickey
class HelloMessage extends HyperHTMLElement {
created() {
this.state.name = 'Chris';
this.render();
}
render() {
this.html`<h1>${this.state.name}</h1>`;
}
}
HelloMessage.define('hello-message');
12kb50kb
VS VS
6kb
React v16
49.8kb -> 34.8kb
Source: https://facebook.github.io/react/blog/2017/09/26/react-v16.0.html
React v16
MIT licensed
Source: https://facebook.github.io/react/blog/2017/09/26/react-v16.0.html
Hacker News PWA
Source: https://hnpwa.com/
Simplicity Matters
-Rich Hickey
Simple !== Easy
-Rich Hickey
My
Journey
It’s
Hard
"Rocky" by Stefan Ogrisek is licensed under CC BY 2.0 / Color adjusted from original
Simplicity
Creates
Opportunity
Polymer Overview - Check out my other talks!
Polymer in Practice @ Comcast Polymer Changes Everything!
See all my talks - http://bit.ly/2kYFOyj
Thanks! / Questions?
Chris Lorenzo / @chiefcll
Senior Principal Engineer
Talks: http://bit.ly/2kYFOyj

Web components: A simpler and faster react