javascript leverage: isomorphic applications by luciano colosio at codemotion dubai

85
unlucio Senior Software Engineer - Namshi Dive Master - PADI Remote Nerd - WEBdeBS

Upload: codemotion-dubai

Post on 11-Apr-2017

174 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

unlucioSenior Software Engineer - Namshi

Dive Master - PADI Remote Nerd - WEBdeBS

Page 2: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Back to the future:

Isomorphic javascript applications

Page 3: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

An isomorphic javascript application:

What is an “isomorphic application”

Why did we need one

How did we build it @ Namshi:

Requirements

Choices

What did we actually do

Page 4: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Isomorphism

Isomorphism is a very general concept that appears in several areas of mathematics. The word derives from the Greek iso, meaning

"equal," and morphosis, meaning "to form" or "to shape.”

wolfram.com

http://mathworld.wolfram.com/Isomorphism.html

Page 5: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Isomorphic application

It’s a dynamic website capable of generating its html on both server and client side, virtually using the same

exact code in both environments

Page 6: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

and a websiteis a bunch of nicely presented html

with an interactive layer on top

Page 7: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

to be isomorphicwe need to stop at the html

take a note for future self!

Page 8: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Why do we need one?

Page 9: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Why do we need one?Robots: search engines don’t cope with SPAs

Page 10: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Why do we need one?Robots: search engines don’t cope with SPAs

Loading speed

Page 11: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Why do we need one?Robots: search engines don’t cope with SPAs

Loading speedJavascript doesn’t “fail safe”

Page 12: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

@ Namshi…

Page 13: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

We had to turn this…

Page 14: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai
Page 15: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

in to this

Page 16: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

See the difference?

Page 17: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai
Page 18: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

SPA Page Isomorphic Page

Page 19: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

SPA Page Isomorphic Page~190Kb js Target 100Kb js

Page 20: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

SPA Page Isomorphic Page~190Kb js Target 100Kb js

~ 40p page speed at least 90p page speed

Page 21: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

SPA Page Isomorphic Page~190Kb js Target 100Kb js

~ 40p page speed at least 90p page speed

Angular.js ?????????

Page 22: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

?????????

Page 23: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

?????????yup, we had to make a choice

Page 24: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai
Page 25: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai
Page 26: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

We’re already using it on our SPAs

Page 27: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

We’re already using it on our SPAsWell known dependency injection

Page 28: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

We’re already using it on our SPAs

Well known dependency injection

Data bindings

Page 29: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

We’re already using it on our SPAs

Well known dependency injection

Data bindings

Jade (and we like it)

Page 30: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

It’s ok but…

Making it isomorphic needs

quite a bit of tooling around

Page 31: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai
Page 32: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai
Page 33: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

New and fashionable…

Page 34: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

New and fashionable… …we’re eager to try it :)

Page 35: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

New and fashionable… …we’re eager to try it :)

The virtual DOM looks amazing

Page 36: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

New and fashionable… …we’re eager to try it :)

The virtual DOM looks amazingFlux (once you get it) seems quite a good idea

Page 37: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

New and fashionable… …we’re eager to try it :)

The virtual DOM looks amazingFlux (once you get it) seems quite a good idea

JSX…

Page 38: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

New and fashionable… …we’re eager to try it :)

The virtual DOM looks amazingFlux (once you get it) seems quite a good idea

JSX…

… but components are a nice idea too!

Page 39: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

New and fashionable… …we’re eager to try it :)

The virtual DOM looks amazingFlux (once you get it) seems quite a good idea

JSX…

… but components are a nice idea too!and The Internet says it can be isomorphic too!

Page 40: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai
Page 41: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Let’s try it! :D

Page 42: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Examples and suggested techniques seems been quite complex, with lots of structure and quite

opinionated

Page 43: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

expectations slightly not met :’(

Page 44: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Yup, we’re back to square one…

Page 45: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Yup, we’re back to square one…

Page 46: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai
Page 47: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

ok… let’s try getting what we like…

Page 48: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

ok… let’s try getting what we like…

data bindings jade

flux components

Page 49: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

flux capacitor

Page 50: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

So, how does it work?

Page 51: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

So, how does it work?Step 1

Understand the view

Data Content

TemplateInteraction

Page 52: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

So, how does it work?Step 2

Strip the interaction layer

Data Content

TemplateInteraction

Remember our note: stop at the html.

Page 53: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

So, how does it work?What do we get form the api:

Data Content

{ data: [], meta: {}, search: {} }

{ data: “”, meta: {}, search: {} }

Page 54: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

So, how does it work?What do we get form the api:

{ data: “”, meta: {}, search: {} }

This guy is html!

Page 55: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

So, how does it work?Choose a template engine

TemplateJade:

our current angular ones

Nunjucks: renders on client and server

Page 56: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

let’s bring backwhat we liked about React

Page 57: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Flux

Page 58: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

FluxOur data source: API

Page 59: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

FluxOur data source: API

State: the URL

Page 60: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

FluxOur data source: API

State: the URLAdd a little dispatcher to notify for data update

Page 61: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Show me the code!

Page 62: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Show me the code!Server entry point

var app = require('express')(); var tpl = require(‘../shared/tpl'); var url = require('url');

app.get('*', function(req, res) { var path = url.parse(req.url).pathname; api.getData(path).then(function(data) { var html = tpl.render('main.html', data); res.end(html); }); });

Page 63: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Show me the code!And it’s client counterpart

var page = require('page');

page('*', function (ctx) { var path = url.parse(ctx.path).pathname;

dispatcher.broadcast('URL_CHANGE', {url: path}); api.getData(path).then(function (data) { dispatcher.broadcast('DATA_UPDATE', data); }); });

page.start();

Page 64: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Components

Page 65: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

ComponentsActivate on state update

Page 66: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

ComponentsActivate on state update

Receive data

Page 67: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

ComponentsActivate on state update

Receive dataTransform them if needed

Page 68: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

ComponentsActivate on state update

Receive dataTransform them if needed

Delegate to templates for final html generation

Page 69: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Show me the code!

Page 70: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Show me the code!Title Component

var tpl = require('../tpl'); var vpo = require('vpo');

module.exports = function(data) { return tpl.render('title.html', { text: vpo.get(data, 'meta.seo.pageTitle') || vpo.get(data, 'title') || 'Namshi.com' }); };

Page 71: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Show me the code!Title Template

<title data-iso-component="title">{{ text }}</title>

Page 72: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Hey but what about my interaction?

Page 73: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Hey but what about my interaction?

well, according to legend: turns out that back in the 90s humans were able to use websites simply clicking on links and

without fancy UI wow-effects

Page 74: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Hey but what about my interaction?

Interaction We call them: widgets

they react to UI changes

they attach to iso-components

interact with data-nm-*

and use VUE.js

Page 75: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Show me the code!

Page 76: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Show me the code!var dispatcher = require(‘../../shared/dispatcher'); var menu = require('./menu'); var cart = require('../services/cart');

module.exports = function($dom) { $dom.on('click', '#site_menu .search_trigger', function(e) { dispatcher.broadcast(‘TOGGLE_SEARCH’); e.preventDefault(); });

this.bind($dom, {data: { menu: menu, cart: cart }}); };

Page 77: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Show me the code!<div id="site_header" data-iso-component="header" data-nm-class="navigation_open: menu.shiftContent"> <ul id="site_menu"> <li><a href="" class="search_trigger"> <i>{{ 'search' | translate }}</i></a> </li> <li><a href="/cart/" class="cart_overview"> <span data-nm-text="cart.itemsCount">0</span></a> </li> </ul> </div>

Page 78: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

did we meet our target?

Page 79: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

did we meet our target?Isomorphism: yes :)

Page 80: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

did we meet our target?Isomorphism: yes :)

Size: 162kb (including templates)

Page 81: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

did we meet our target?Isomorphism: yes :)

Size: 162kb (including templates)

Page speed…

Page 82: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Page speed…

Page 83: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

node.js Browserify

Gulp Nunjucks Vanilla JS express.js

VUE.js Docker

Page 84: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Thank you for listening

Page 85: Javascript leverage: Isomorphic Applications by Luciano Colosio at Codemotion Dubai

Thank you for listening