back to the future: isomorphic javascript applications

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

Upload: luciano-colosio

Post on 03-Aug-2015

708 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Back to the future:  Isomorphic javascript applications

unlucioSenior Software Engineer - Namshi

Dive Master - PADI Remote Nerd - WEBdeBS

Page 2: Back to the future:  Isomorphic javascript applications

Back to the future:

Isomorphic javascript applications

Page 3: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

and a websiteis a bunch of nicely presented html

with an interactive layer on top

Page 7: Back to the future:  Isomorphic javascript applications

to be isomorphicwe need to stop at the html

take a note for future self!

Page 8: Back to the future:  Isomorphic javascript applications

Why do we need one?

Page 9: Back to the future:  Isomorphic javascript applications

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

Page 10: Back to the future:  Isomorphic javascript applications

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

Loading speed

Page 11: Back to the future:  Isomorphic javascript applications

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

Loading speedJavascript doesn’t “fail safe”

Page 12: Back to the future:  Isomorphic javascript applications

@ Namshi…

Page 13: Back to the future:  Isomorphic javascript applications

We had to turn this…

Page 14: Back to the future:  Isomorphic javascript applications
Page 15: Back to the future:  Isomorphic javascript applications

in to this

Page 16: Back to the future:  Isomorphic javascript applications

See the difference?

Page 17: Back to the future:  Isomorphic javascript applications
Page 18: Back to the future:  Isomorphic javascript applications

SPA Page Isomorphic Page

Page 19: Back to the future:  Isomorphic javascript applications

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

Page 20: Back to the future:  Isomorphic javascript applications

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

~ 40p page speed at least 90p page speed

Page 21: Back to the future:  Isomorphic javascript applications

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

~ 40p page speed at least 90p page speed

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

Page 22: Back to the future:  Isomorphic javascript applications

?????????

Page 23: Back to the future:  Isomorphic javascript applications

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

Page 24: Back to the future:  Isomorphic javascript applications
Page 25: Back to the future:  Isomorphic javascript applications
Page 26: Back to the future:  Isomorphic javascript applications

We’re already using it on our SPAs

Page 27: Back to the future:  Isomorphic javascript applications

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

Page 28: Back to the future:  Isomorphic javascript applications

We’re already using it on our SPAs

Well known dependency injection

Data bindings

Page 29: Back to the future:  Isomorphic javascript applications

We’re already using it on our SPAs

Well known dependency injection

Data bindings

Jade (and we like it)

Page 30: Back to the future:  Isomorphic javascript applications

It’s ok but…

Making it isomorphic needs

quite a bit of tooling around

Page 31: Back to the future:  Isomorphic javascript applications
Page 32: Back to the future:  Isomorphic javascript applications
Page 33: Back to the future:  Isomorphic javascript applications

New and fashionable…

Page 34: Back to the future:  Isomorphic javascript applications

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

Page 35: Back to the future:  Isomorphic javascript applications

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

The virtual DOM looks amazing

Page 36: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications
Page 41: Back to the future:  Isomorphic javascript applications

Let’s try it! :D

Page 42: Back to the future:  Isomorphic javascript applications

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

opinionated

Page 43: Back to the future:  Isomorphic javascript applications

expectations slightly not met :’(

Page 44: Back to the future:  Isomorphic javascript applications

Yup, we’re back to square one…

Page 45: Back to the future:  Isomorphic javascript applications

Yup, we’re back to square one…

Page 46: Back to the future:  Isomorphic javascript applications
Page 47: Back to the future:  Isomorphic javascript applications

ok… let’s try getting what we like…

Page 48: Back to the future:  Isomorphic javascript applications

ok… let’s try getting what we like…

data bindings jade

flux components

Page 49: Back to the future:  Isomorphic javascript applications

flux capacitor

Page 50: Back to the future:  Isomorphic javascript applications

So, how does it work?

Page 51: Back to the future:  Isomorphic javascript applications

So, how does it work?Step 1

Understand the view

Data Content

TemplateInteraction

Page 52: Back to the future:  Isomorphic javascript applications

So, how does it work?Step 2

Strip the interaction layer

Data Content

TemplateInteraction

Remember our note: stop at the html.

Page 53: Back to the future:  Isomorphic javascript applications

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

Data Content

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

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

Page 54: Back to the future:  Isomorphic javascript applications

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

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

This guy is html!

Page 55: Back to the future:  Isomorphic javascript applications

So, how does it work?Choose a template engine

TemplateJade:

our current angular ones

Nunjucks: renders on client and server

Page 56: Back to the future:  Isomorphic javascript applications

let’s bring backwhat we liked about React

Page 57: Back to the future:  Isomorphic javascript applications

Flux

Page 58: Back to the future:  Isomorphic javascript applications

FluxOur data source: API

Page 59: Back to the future:  Isomorphic javascript applications

FluxOur data source: API

State: the URL

Page 60: Back to the future:  Isomorphic javascript applications

FluxOur data source: API

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

Page 61: Back to the future:  Isomorphic javascript applications

Show me the code!

Page 62: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

Components

Page 65: Back to the future:  Isomorphic javascript applications

ComponentsActivate on state update

Page 66: Back to the future:  Isomorphic javascript applications

ComponentsActivate on state update

Receive data

Page 67: Back to the future:  Isomorphic javascript applications

ComponentsActivate on state update

Receive dataTransform them if needed

Page 68: Back to the future:  Isomorphic javascript applications

ComponentsActivate on state update

Receive dataTransform them if needed

Delegate to templates for final html generation

Page 69: Back to the future:  Isomorphic javascript applications

Show me the code!

Page 70: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

Show me the code!Title Template

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

Page 72: Back to the future:  Isomorphic javascript applications

Hey but what about my interaction?

Page 73: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

Show me the code!

Page 76: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

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: Back to the future:  Isomorphic javascript applications

did we meet our target?

Page 79: Back to the future:  Isomorphic javascript applications

did we meet our target?Isomorphism: yes :)

Page 80: Back to the future:  Isomorphic javascript applications

did we meet our target?Isomorphism: yes :)

Size: 162kb (including templates)

Page 81: Back to the future:  Isomorphic javascript applications

did we meet our target?Isomorphism: yes :)

Size: 162kb (including templates)

Page speed…

Page 82: Back to the future:  Isomorphic javascript applications

Page speed…

Page 83: Back to the future:  Isomorphic javascript applications

node.js Browserify

Gulp Nunjucks Vanilla JS express.js

VUE.js Docker

Page 84: Back to the future:  Isomorphic javascript applications

Thank you for listening

Page 85: Back to the future:  Isomorphic javascript applications

Thank you for listening