in pursuit of the holy grail: building isomorphic javascript apps

46
In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps Spike Brehm @spikebrehm

Upload: spike-brehm

Post on 08-Sep-2014

6.786 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Spike Brehm @spikebrehm

Page 2: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Spike Brehm

Software Engineer @AirbnbNerds

Page 3: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Agenda

Page 4: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

...TF is Isomorphic JavaScript?Wat

...is it relevant to web developers?Wy

...can I build isomorphic apps?How

Page 5: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

WTF is Isomorphic JavaScript?

Page 6: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

JavaScript code that can run on both the client and server.

Page 7: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

A brief note on “isomorphic”.

Page 8: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

“monomorphic” “heteromorphic” “homomorphic” “multi-platform”

You’re using it wrong!

Page 9: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Isomorphic JavaScript can be

or shimmed per environment .

environment-agnostic

Page 10: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Does not depend on browser-specific properties (window) or server-specific properties (process.env, req.cookies).

Environment-agnostic

Page 11: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Example: Handlebars.js

var template = ! '<ul>' \! '{{#each posts}}' \! ' <li>{{title}}</li>' \! '{{/each}}' \! '</ul>'!;! !var templateFn = Handlebars.compile(template)! , html = templateFn({posts: posts});! !// <ul>!// <li>JavaScript is cool</li>!// <li>The Web is the platform</li>!// </ul>

Page 12: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Provides shims for accessing environment-specific properties so module can expose a single API.

window.location.pathnamevs. req.path

Shimmed per environment

Page 13: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Example: Superagent

superagent! .post('/api/posts.json')! .send({ title: 'Moar JavaScript', body: '...' })! .end(function(response) {! if (response.status === 200) {! console.log("Success!");! } else {! console.error("Error", response.body.error);! }! });

Page 14: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Isomorphic use cases.

Page 15: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

• Templating • Routing • I18n • Date & currency formatting • Model validation • API interaction • ...?

Isomorphic use cases.

Page 16: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Most of your favorite libraries can be used isomorphically.

Page 17: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

• Underscore.js • Backbone.js • Handlebars.js • jQuery • Moment • React.js • Polyglot.js (I18n)

Page 18: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Wy go to the trouble?

Page 19: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Initial pageload speed.Performance

Crawlable single-page apps.SEO

Reduce code duplication.Maintainability

Run code where you want.Flexibility

Page 20: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Ye olde days: fat-server, thin-client.

Page 21: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Client JavaScript

Server Ruby

Python Java PHP

Persistence

View layer

Application logic

Routing

DOM manipulation Animations

Page 22: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Circa 2011: thin-server, fat-client.

Page 23: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Routing

View layer

Server Ruby

Python Java PHP

Persistence

Client JavaScript

Application logic

DOM manipulation Animations

Page 24: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Teh footure: shared, fat-server, fat-client.

Page 25: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Server Ruby

Python Java PHP

Persistence

Client JavaScript

Routing

View layer

Application logic

Shared JavaScript

DOM manipulation Animations

Page 26: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Isomorphic JavaScript is a spectrum.

Page 27: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Entire view layer and app

logic shared

Small bits of view layer or logic shared

Page 28: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Many abstractions

Few abstractions

Page 29: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

ComplexSimple

Page 30: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

View layer shared

Entire app runtime synced between client

& server

Page 31: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

How to Isomorphic.

Page 32: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

The magic happens in the build process.Browserify & Grunt

Page 33: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

BrowserifyUse require() in the browser, the same way you would on the server.

Package up CommonJS modules for the browser.

Page 34: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Browserifydemo !

github.com/ spikebrehm/isomorphic-examples

Page 35: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Task runner for automating build and development workflow.

Grunt

Page 36: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Gruntdemo !

github.com/ spikebrehm/isomorphic-examples

Page 37: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Wat about shimmed per environment?

Page 38: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

How does Superagent do it?

Use package.json’s “browser” field

"browser": { "./lib/node/index.js": “./lib/client.js", "emitter": "emitter-component", "reduce": "reduce-component" },

“browser”: STRING || OBJECT,

“browser": "./lib/client.js",

Page 39: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Isomorphic frameworks.

Page 40: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

MojitoYahoo!; Full-stack; Never caught on.

MeteorRealtime; Full-stack; Awesome. No server-side rendering (yet).

RendrAirbnb; Library; Backbone & Handlebars. Came from m.airbnb.com.

Page 41: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Towards an isomorphic future.

Page 42: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

JavaScript is the lingua franca of the Web.

The Web is the platform; JavaScript provides the runtime.

Page 43: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

More reusable solutions; more small modules.

It will be rare to see a web app using no server-side JavaScript.

Page 44: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

The revolution will come in the build systems.

Static analysis, conditional builds, source transforms.

Page 45: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

Questions?

Page 46: In Pursuit of the Holy Grail: Building Isomorphic JavaScript Apps

@AirbnbNerds@spikebrehm

Thanks!