spa, isomorphic and back to the server our journey with js @ frontend con poland 2017

152
Alex Nadalin - CTO @ namshi.com FRONTEND CON 2017 (WARSAW - poland)

Upload: alessandro-nadalin

Post on 23-Jan-2018

123 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Alex Nadalin - CTO @ namshi.comFRONTEND CON 2017 (WARSAW - poland)

Page 2: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

WARNING

Controversyahead

Page 3: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

WARNING

Insult@_odino_

Page 4: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Zalando of the middle east

Page 5: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

http://tech.namshi.io/blog/2017/05/02/rebuilding-our-mobile-website/

Shidhin CR

https://developers.google.com/experts/people/shidhin-cr

Mohamed amin

https://medium.com/@MohamedAmin88

Gabriel izebhigie

http://tech.namshi.io/team/#Gabriel Izebhigie

Page 6: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Take #1

Page 7: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Started with a SPA

Page 8: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

...which pissed bots off

Page 9: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

...which pissed clients off

Page 10: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

...which pissed clients off

Page 11: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

...which pissed clients off

Page 12: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Take #2

Page 13: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Isomorphic js

Page 14: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Both on the client & the server

Page 15: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

universal js

Page 16: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Works only in movies

Page 17: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

https://2015.jsday.it/talk/back-to-the-future-isomorphic-javascript-applications/

Page 18: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Clients are still angry

Page 19: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Sneak peak of a custom-madeuniversal js framework

Page 20: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Sneak peak of a custom-madeuniversal js framework

Page 21: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Sneak peak of a custom-madeuniversal js framework

Page 22: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Fonzie does not approve.

Page 23: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

The do-over

Page 24: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Simplify our codebase

Page 25: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Better client-side performance

Page 26: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

roi

Page 27: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Best website?

Page 28: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

It’s an app.

Page 29: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

It’s an app.

Smooth transitions

Page 30: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

It’s an app.

Smooth transitions

Great (perceived) performance

Page 31: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Can a websiteMatch that?

Page 32: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Spa closely bridges the gap

Page 33: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Spa closely bridges the gap

Additional layer of complexity

Page 34: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Generally no.

Spa closely bridges the gap

Additional layer of complexity

Page 35: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Do we need it?

Page 36: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017
Page 37: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017
Page 38: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017
Page 39: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

service is king

Page 40: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

The b* stack

Page 41: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

The b* stack

Page 42: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

The b* stack

Page 43: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

The b* stack

Page 44: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

The b* stack

Page 45: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

server

Page 46: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017
Page 47: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

client

Page 48: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Webpack 2

Page 49: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Webpack 2

Tree-shaking saved us15/20% of the gzipped

bundle size

Page 50: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Say “no” to jquery(as much as possible)

Page 51: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017
Page 52: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Abstraction === cost

Page 53: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Abstraction === cost let obj = { name: 'alex', age: 28, hair: 'enough', status: 'married', job: 'who really knows', }

_.pick(obj, ['name', 'age'])

{ name: obj.name, age: obj.age,}

Page 54: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Abstraction === cost let obj = { name: 'alex', age: 28, hair: 'enough', status: 'married', job: 'who really knows', }

_.pick(obj, ['name', 'age'])

{ name: obj.name, age: obj.age,}

Page 55: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Abstraction === cost let obj = { name: 'alex', age: 28, hair: 'enough', status: 'married', job: 'who really knows', }

_.pick(obj, ['name', 'age'])

{ name: obj.name, age: obj.age,}

Page 56: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Abstraction === cost let obj = { name: 'alex', age: 28, hair: 'enough', status: 'married', job: 'who really knows', }

_.pick(obj, ['name', 'age'])

{ name: obj.name, age: obj.age,}700k ops/s 75m ops/s

Page 57: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Abstraction === cost let obj = { name: 'alex', work: { name: 'Namshi' } }

_.get(obj, 'work.name', null)

Page 58: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Abstraction === cost let obj = { name: 'alex', work: { name: 'Namshi' } }

_.get(obj, 'work.name', null)

1.3m ops/s

Page 59: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Abstraction === cost let obj = { name: 'alex', work: { name: 'Namshi' } }

let work = null;

if (obj && obj.work && obj.work.name) { work = obj.work.name }

Page 60: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Abstraction === cost let obj = { name: 'alex', work: { name: 'Namshi' } }

let work = null;

if (obj && obj.work && obj.work.name) { work = obj.work.name }

70m ops/s

Page 61: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

!Results may vary

Page 62: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

let attributes = [‘price’,‘name’,‘description’,‘url’

]

………

function sanitize(products) {return products.map(p => {

return _.pick(p, attributes) })}

Page 63: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

let attributes = [‘price’,‘name’,‘description’,‘url’

]

………

function sanitize(products) {return products.map(p => {

return _.pick(p, attributes) })}

Page 64: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

let attributes = [‘price’,‘name’,‘description’,‘url’

]

………

function sanitize(products) {return products.map(p => {

return _.pick(p, attributes) })}

Page 65: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

let attributes = [‘price’,‘name’,‘description’,‘url’

]

………

function sanitize(products) {return products.map(p => {

return _.pick(p, attributes) })} 5ms / req

Page 66: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

let attributes = [‘price’,‘name’,‘description’,‘url’

]

………

function sanitize(products) {return products.map(p => {

return _.pick(p, attributes) })} 10% / req

Page 67: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017
Page 68: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

const _ = require(‘lodash’)

Page 69: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

const _ = require(‘lodash’)

const pick = require(‘lodash/pick’)

Page 70: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

articles.filter(a => a.active).map(a => {a.title = titleCase(a.title)

return a})

Page 71: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

articles.filter(a => a.active).map(a => {a.title = titleCase(a.title)

return a})

Page 72: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

articles.filter(a => a.active).map(a => {a.title = titleCase(a.title)

return a})

Page 73: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

articles.filter(a => a.active).map(a => {a.title = titleCase(a.title)

return a})

Page 74: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

articles.filter(a => a.active).map(a => {a.title = titleCase(a.title)

return a})

700k ops/s

Page 75: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

articles.reduce((acc, a) => {if (a.active) {a.title = titlecase(a.title)acc.push(a)

}

return acc})

Page 76: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

articles.reduce((acc, a) => {if (a.active) {a.title = titlecase(a.title)acc.push(a)

}

return acc}) 4M ops/s

Page 77: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

sprites?

Page 78: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

No thanks,I do http/2

Page 79: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

300kb 500kb 200kb

Page 80: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

The old www

Page 81: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

The old www

Page 82: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

300kb 500kb

The old www

Page 83: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

300kb 500kb 800kb / tot2 conns

The old www

Page 84: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

sprites

Page 85: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

sprites

Page 86: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

1mb / tot1 conn

sprites

Page 87: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

http/2

Page 88: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

http/2

Page 89: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

800kb / tot1 conn

http/2

Page 90: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

react?

Page 91: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

“No sauce please”

Page 92: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

“No sauce please”

react-lite 25kb

preact 3kb

Page 93: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

css animations

Page 94: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

css animations

Page 96: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preconnect

<html class="en"><head>

<link preconnect="https://a.namshicdn.com" crossorigin />

Page 97: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preconnect

<html class="en"><head>

<link preconnect="https://a.namshicdn.com" crossorigin />

Page 98: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preconnect

<html class="en"><head>

<link preconnect="https://a.namshicdn.com" crossorigin />Initiate

connections

ASAP

Page 99: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preload<link

rel="preload" href="https://mycdn.com/fonts.css" as="style" onload="

this.rel='stylesheet';this.className='font-loaded'

"/>

Page 100: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preload<link

rel="preload" href="https://mycdn.com/fonts.css" as="style" onload="

this.rel='stylesheet';this.className='font-loaded'

"/>

Page 101: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preload<link

rel="preload" href="https://mycdn.com/fonts.css" as="style" onload="

this.rel='stylesheet';this.className='font-loaded'

"/>

Page 102: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preload<link

rel="preload" href="https://mycdn.com/fonts.css" as="style" onload="

this.rel='stylesheet';this.className='font-loaded'

"/>

Page 103: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preload<link

rel="preload" href="https://mycdn.com/fonts.css" as="style" onload="

this.rel='stylesheet';this.className='font-loaded'

"/>

Page 104: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preload<link

rel="preload" href="https://mycdn.com/fonts.css" as="style" onload="

this.rel='stylesheet';this.className='font-loaded'

"/>

Async

Non-blocking

CSS

Page 105: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

preload

Page 106: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

prerender window.addEventListener('load', function(){ var preRenderLink = doc.createElement('link'); preRenderLink.rel='prerender'; preRenderLink.href= '{{ nextPage }}'; document.head.appendChild(preRenderLink); });

Page 107: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

prerender window.addEventListener('load', function(){ var preRenderLink = doc.createElement('link'); preRenderLink.rel='prerender'; preRenderLink.href= '{{ nextPage }}'; document.head.appendChild(preRenderLink); });

Page 108: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

prerender window.addEventListener('load', function(){ var preRenderLink = doc.createElement('link'); preRenderLink.rel='prerender'; preRenderLink.href= '{{ nextPage }}'; document.head.appendChild(preRenderLink); });

Page 109: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

prerender window.addEventListener('load', function(){ var preRenderLink = doc.createElement('link'); preRenderLink.rel='prerender'; preRenderLink.href= '{{ nextPage }}'; document.head.appendChild(preRenderLink); });

Page 110: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

prerender window.addEventListener('load', function(){ var preRenderLink = doc.createElement('link'); preRenderLink.rel='prerender'; preRenderLink.href= '{{ nextPage }}'; document.head.appendChild(preRenderLink); });

Page 111: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

prerender

Page 112: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

SPLIT ASSETS

Page 113: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

SPLIT ASSETS

Page 114: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

SPLIT ASSETS

Page 115: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

index.html

Page 116: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

detail.html

Page 117: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Less bandwidth, good cache rate

Page 118: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Less bandwidth, good cache rate

7 req~100kb

Page 119: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Less bandwidth, good cache rate

9 req~150kb

Page 120: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

RESULTS ?

Page 121: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

-60%AVG document content loaded time

1.93 vs 4.84

Page 122: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

-14%Bounce rate

Page 123: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

+40%AVG session duration

Page 124: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

+30%Conversion rate

Page 125: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

LookingForward

to...

Page 126: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017
Page 127: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Not available in the browserhttps://github.com/grpc/grpc/issues/8682

Page 128: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017
Page 129: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Under developmenthttps://firebase.google.com/docs/cloud-messaging/

Page 130: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017
Page 131: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Under developmenthttps://githubengineering.com/githubs-post-csp-journey/

Page 132: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

NERD ADVICE

Page 133: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

NERD ADVICEAct upon what moves the needlehttps://en.wikipedia.org/wiki/Pareto_principle

Page 134: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

“Hot” is overrated

NERD ADVICE

Page 135: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

“Hot” is overratedhttps://jakearchibald.com/2016/caching-best-practices/

“As you can see, you can hack around poor caching in your service worker, but you're way better off fixing the root of the problem. Getting your caching right makes things easier in service worker land, but also benefits browsers that don't support service worker (Safari, IE/Edge), and lets you get the most out of your CDN.”

https://jakearchibald.com/2016/caching-best-practices/

NERD ADVICE

Page 136: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

“Solve problems on the right layer”https://www.ampproject.org/learn/amp-design-principles/

NERD ADVICE

Page 137: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

“Solve problems on the right layer”https://www.ampproject.org/learn/amp-design-principles/

NERD ADVICE

Page 138: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Servers can still be pretty darn fast.

NERD ADVICE

Page 139: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

NERD ADVICE

Page 140: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Servers can still be pretty fast

NERD ADVICE

Page 141: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Servers can still be pretty fast

NERD ADVICE

Page 142: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Servers can still be pretty fast

NERD ADVICE

Page 143: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

50% within <20ms

Page 144: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

95% within <120ms

Page 145: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Take this any day

Page 146: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Alessandro Nadalin

Page 147: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Alessandro Nadalin

@_odino_

Page 148: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Alessandro Nadalin

@_odino_

Namshi

Page 149: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Alessandro Nadalin

@_odino_

Namshi

CTO

Page 150: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Alessandro Nadalin

@_odino_

Namshi

CTO

odino.org

Page 151: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

Thanks!Alessandro Nadalin

@_odino_

Namshi

CTO

odino.org

Page 152: Spa, isomorphic and back to the server  our journey with js @ frontend con poland 2017

we are hiring!tech.namshi.com/join-us

github.com/namshi

twitter.com/TechNamshi

tech.namshi.io