SlideShare a Scribd company logo
1 of 71
Download to read offline
Building Isomorphic Apps
Spike Brehm
@spikebrehm
@spikebrehm
@AirbnbNerds
Spike Brehm
Isomorphic
JavaScript.
WTF is Isomorphic
JavaScript?
JavaScript code that can be
shared between environments.
JavaScript code that can be
shared between environments.
JavaScript code that can be
shared between environments.
JavaScript code that can be
shared between environments.
Backend
Ruby
Python
Java
PHP
Node.js
Persistence
Client
JavaScript
Shared
JavaScript
DOM manipulation UX
View layer
Application
logic
Routing
Etymology of
“Isomorphic
JavaScript”.
adjective
corresponding or similar in form and relations.
i·so·mor·phic
i·so·mor·phic
formsame
http://blog.nodejitsu.com/scaling-isomorphic-javascript-code/
“monomorphic”
“heteromorphic”
“homomorphic”
“polymorphic”
You’re using it wrong!
Isomorphic
JavaScript
in the wild.
Flickr
Flickr
! Yahoo’s Modown libraries
(successor to Mojito).
Flickr
Instagram*
Instagram*
! Facebook’s React library
in a Django app.
Instagram*
Airbnb Mobile
Airbnb Mobile
! Airbnb’s Rendr library, built on
Backbone and Express.
Airbnb Mobile
Asana
! Entire App runtime synced
between client & server.
Asana
Meteor
! Realtime app framework.
Meteor
Wy go to the
trouble?
Initial pageload speed.
Performance
Crawlable single-page apps.
SEO*
Reduce code duplication.
Maintainability
Run code anywhere.
Flexibility
Isomorphic use
cases.
• Templating
• I18n
• Date & currency formatting
• Application logic
• Routing
• Model validation
• API interaction
• ...?
Isomorphic use cases.
Isomorphic
JavaScript is a
spectrum.
Entire view
layer and app
logic shared
Small bits of
view layer or
logic shared
Many
abstractions
Few
abstractions
View layer
shared
Entire app
runtime synced
between client
& server
Isomorphic
JavaScript can be
or
shimmed per
environment .
environment-
agnostic
Does not depend on browser-specific
properties (window) or server-specific
properties (process.env,
req.cookies).
Environment-agnostic
Example: Handlebars.js
var template = !
'<ul>' !
'{{#each posts}}' !
' <li>{{title}}</li>' !
'{{/each}}' !
'</ul>'!
;!
 !
var templateFn = Handlebars.compile(template)!
, html = templateFn({posts: posts});
Provide shims for accessing
environment-specific properties so
module can expose a single API.
window.location.pathname	
vs.
req.path
Shimmed per environment
Example: Superagent
superagent!
.get('/api/posts.json')!
.end(function(res) {!
if (res.status === 200) {!
console.log("Posts:", res.body);!
} else {!
console.error("Error");!
}!
});
Abstractions.
Abstraction: User Agent
Client navigator.userAgent
Server req.get('user-agent')
Abstraction: Cookies
Client
document.cookie =!
'myCookie=1; Domain=.example.org';
Server
res.setHeader(!
'Set-Cookie: myCookie=1; ' +!
'Domain=.example.org'!
);
Abstraction: Redirects
Client
document.location.href = '/login';!
!
window.pushState({}, '', '/login');
Server res.redirect('/login');
How to isomorph.
Let’s write a module that abstracts the
setting of cookies, providing the same
API for client & server.
setCookie('myCookie', 'the value');
setCookie('myCookie', 'the value');
document.cookie = 'myCookie=the%20value';
or
res.setHeader('Set-Cookie: myCookie=the%20value;');
setCookie('myCookie', 'the value', {!
path: '/',!
domain: '.example.org',!
expires: new Date(2014, 12, 31)!
});
document.cookie =!
'myCookie=the%20value; Domain=.example.org; ' +!
'Path=/; Expires=Sat, 31 Jan 2015 05:00:00 GMT';
Eww, that looks hard.
NPM & Browserify
to the rescue.
Browserify
Use CommonJS to require()
modules in the browser.
Browserify
Package dependencies from
node_modules into our bundle.
How do we make a
shimmed-per-
environment module?
Utilize package.json “browser” field.
{!
"name": "set-cookie",!
"dependencies": {...}!
}!
!
!
!
{!
"name": "set-cookie",!
"dependencies": {...},!
"browser": "./lib/client.js"!
}!
!
!
Swap out the entire
implementation.
{!
"name": "set-cookie",!
"dependencies": {...},!
"browser": {!
"./lib/node.js": "./lib/client.js"!
}!
}!
Swap out specific files.
{!
"name": "set-cookie",!
"dependencies": {...},!
"browser": {!
"./lib/node.js": "./lib/client.js",!
"cookie": "cookie-browser"!
}!
}
Swap out dependencies.
Let’s build `set-cookie`.
https://github.com/spikebrehm/set-cookie
Module structure
.!
"## index.js!
"## lib!
$   %## setter!
$   "## index.js!
$   %## client.js!
"## node_modules!
$   %## cookie
// ./index.js!
!
var cookie = require('cookie');!
var setter = require('./lib/setter');!
!
module.exports = function(name, value, options) {!
  var cookieStr = cookie.serialize(name, value, options);!
  setter(cookieStr, options);!
};
// ./lib/setter/index.js!
!
module.exports = function setter(cookieStr, options) {!
  var res = options && options.res;!
!
  if (!res)!
throw new Error('Must specify `res` ' +!
'when setting cookie.’);!
!
  res.setHeader('Set-Cookie', cookieStr);!
};
// ./lib/setter/client.js!
!
module.exports = function setter(cookieStr) {!
  document.cookie = cookieStr;!
};
// ./package.json!
!
{!
"name": "set-cookie",!
"dependencies": {!
"cookie": "^0.1.2"!
},!
"browser": {!
"./lib/setter/index.js": "./lib/setter/client.js"!
}!
}
// ./index.js!
!
var cookie = require('cookie');!
var setter = require('./lib/setter');!
!
module.exports = function(name, value, options) {!
  var cookieStr = cookie.serialize(name, value, options);!
  setter(cookieStr, options);!
};
How to isomorph
in a nutshell.
@spikebrehm

@AirbnbNerds
Thanks!
More resources available at

http://spike.technology
@spikebrehm

@AirbnbNerds
Thanks!
More resources available at

http://spike.technology
We’re hiring!!!We’re hiring!!!We’re hiring!!!We’re hiring!!!We’re hiring!!!We’re hiring!!!

More Related Content

What's hot

Service Worker 201 (en)
Service Worker 201 (en)Service Worker 201 (en)
Service Worker 201 (en)
Chang W. Doh
 

What's hot (20)

The Evolution of Airbnb's Frontend
The Evolution of Airbnb's FrontendThe Evolution of Airbnb's Frontend
The Evolution of Airbnb's Frontend
 
Browserify
BrowserifyBrowserify
Browserify
 
Getting Started with HTML 5 Web workers
Getting Started with HTML 5 Web workersGetting Started with HTML 5 Web workers
Getting Started with HTML 5 Web workers
 
Node, express & sails
Node, express & sailsNode, express & sails
Node, express & sails
 
SxSW 2015
SxSW 2015SxSW 2015
SxSW 2015
 
Workshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewWorkshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General Overview
 
JavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & BrowserifyJavaScript Dependencies, Modules & Browserify
JavaScript Dependencies, Modules & Browserify
 
Webpack DevTalk
Webpack DevTalkWebpack DevTalk
Webpack DevTalk
 
Isomorphic web application
Isomorphic web applicationIsomorphic web application
Isomorphic web application
 
Web workers
Web workersWeb workers
Web workers
 
Web Workers
Web WorkersWeb Workers
Web Workers
 
Service Worker 201 (en)
Service Worker 201 (en)Service Worker 201 (en)
Service Worker 201 (en)
 
Single Page Application (SPA) using AngularJS
Single Page Application (SPA) using AngularJSSingle Page Application (SPA) using AngularJS
Single Page Application (SPA) using AngularJS
 
JavaScript Web Workers
JavaScript Web WorkersJavaScript Web Workers
JavaScript Web Workers
 
Packing it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to nowPacking it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to now
 
Javascript Frameworks Comparison - Angular, Knockout, Ember and Backbone
Javascript Frameworks Comparison - Angular, Knockout, Ember and BackboneJavascript Frameworks Comparison - Angular, Knockout, Ember and Backbone
Javascript Frameworks Comparison - Angular, Knockout, Ember and Backbone
 
An Intro into webpack
An Intro into webpackAn Intro into webpack
An Intro into webpack
 
Webpack Tutorial, Uppsala JS
Webpack Tutorial, Uppsala JSWebpack Tutorial, Uppsala JS
Webpack Tutorial, Uppsala JS
 
Once upon a time, there were css, js and server-side rendering
Once upon a time, there were css, js and server-side renderingOnce upon a time, there were css, js and server-side rendering
Once upon a time, there were css, js and server-side rendering
 
WordPress as a Platform - talk to Bristol Open Source Meetup, 2014-12-08
WordPress as a Platform - talk to Bristol Open Source Meetup, 2014-12-08WordPress as a Platform - talk to Bristol Open Source Meetup, 2014-12-08
WordPress as a Platform - talk to Bristol Open Source Meetup, 2014-12-08
 

Similar to JSConf US 2014: Building Isomorphic Apps

Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
Ran Mizrahi
 
JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big Picture
Simon Willison
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
Rob Davarnia
 
Creating Custom Dojo Widgets Using WTP
Creating Custom Dojo Widgets Using WTPCreating Custom Dojo Widgets Using WTP
Creating Custom Dojo Widgets Using WTP
nsandonato
 

Similar to JSConf US 2014: Building Isomorphic Apps (20)

Be ef presentation-securitybyte2011-michele_orru
Be ef presentation-securitybyte2011-michele_orruBe ef presentation-securitybyte2011-michele_orru
Be ef presentation-securitybyte2011-michele_orru
 
Isomorphic JS - new silver bullet
Isomorphic JS - new silver bulletIsomorphic JS - new silver bullet
Isomorphic JS - new silver bullet
 
VisualWeb - Building a NodeJS Server Meshwork and Full-Javascript Stack Frame...
VisualWeb - Building a NodeJS Server Meshwork and Full-Javascript Stack Frame...VisualWeb - Building a NodeJS Server Meshwork and Full-Javascript Stack Frame...
VisualWeb - Building a NodeJS Server Meshwork and Full-Javascript Stack Frame...
 
(In)Security Implication in the JS Universe
(In)Security Implication in the JS Universe(In)Security Implication in the JS Universe
(In)Security Implication in the JS Universe
 
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
 
Bringing The Sexy Back To WebWorkers
Bringing The Sexy Back To WebWorkersBringing The Sexy Back To WebWorkers
Bringing The Sexy Back To WebWorkers
 
JavaScript Libraries: The Big Picture
JavaScript Libraries: The Big PictureJavaScript Libraries: The Big Picture
JavaScript Libraries: The Big Picture
 
Refactoring JavaScript Applications
Refactoring JavaScript ApplicationsRefactoring JavaScript Applications
Refactoring JavaScript Applications
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
 
Developing Java Web Applications
Developing Java Web ApplicationsDeveloping Java Web Applications
Developing Java Web Applications
 
Firefox OS, HTML5 pour le mobile - Code(love) Hackathon - 2014-05-28
Firefox OS, HTML5 pour le mobile - Code(love) Hackathon - 2014-05-28Firefox OS, HTML5 pour le mobile - Code(love) Hackathon - 2014-05-28
Firefox OS, HTML5 pour le mobile - Code(love) Hackathon - 2014-05-28
 
From Backbone to Ember and Back(bone) Again
From Backbone to Ember and Back(bone) AgainFrom Backbone to Ember and Back(bone) Again
From Backbone to Ember and Back(bone) Again
 
Creating Custom Dojo Widgets Using WTP
Creating Custom Dojo Widgets Using WTPCreating Custom Dojo Widgets Using WTP
Creating Custom Dojo Widgets Using WTP
 
Service worker API
Service worker APIService worker API
Service worker API
 
从小书签到浏览器扩展的应用
从小书签到浏览器扩展的应用从小书签到浏览器扩展的应用
从小书签到浏览器扩展的应用
 
Micro frontends
Micro frontendsMicro frontends
Micro frontends
 
Installing Games Sucks, Learn WebGL
Installing Games Sucks, Learn WebGLInstalling Games Sucks, Learn WebGL
Installing Games Sucks, Learn WebGL
 
Flash And Dom
Flash And DomFlash And Dom
Flash And Dom
 
From Idea to App (or “How we roll at Small Town Heroes”)
From Idea to App (or “How we roll at Small Town Heroes”)From Idea to App (or “How we roll at Small Town Heroes”)
From Idea to App (or “How we roll at Small Town Heroes”)
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

JSConf US 2014: Building Isomorphic Apps