isomorphic web application

45
Isomorphic Web Application Demo using React

Upload: nguyen-vu

Post on 12-Jul-2015

793 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Isomorphic web application

Isomorphic Web ApplicationDemo using React

Page 2: Isomorphic web application

Isomorphic Web Application

Vu [email protected]

Liti Solutions

Page 3: Isomorphic web application

Agenda

1. A brief history of web development- Server-rendered Multi-Page Application (MPA)- Client-rendered Single Page Application (SPA)

2. What is Isomorphic JavaScript?

3. Why on the Earth do we need it?

4. How can I build isomorphic app?

5. Stop talking. Show me the code!

Page 4: Isomorphic web application

TL;DR

1. Isomorphic JavaScript is the pattern of running JavaScript code on both server & client.

2. People are using it for production today.Ask Facebook, Yahoo, Asana, Airbnb, Rising Stack, …

3. This is not another talk about NodeJS!

Page 5: Isomorphic web application

once upon a time…

Page 6: Isomorphic web application

<?php

echo “Hey, a web server is talking to you !!”;

echo “How many {$item.name} do you want to buy?”

?>

<form>

<input name=“your-name” />

<input name=“quantity” />

</form>

Page 7: Isomorphic web application

then…

Page 8: Isomorphic web application

then…

Page 9: Isomorphic web application

then…

Page 10: Isomorphic web application

then…

Page 11: Isomorphic web application

then…

Page 12: Isomorphic web application

Today (Dec, 2014)

* http://modulecounts.com

Page 13: Isomorphic web application

Traditional Multi-Page Application

Page 14: Isomorphic web application

* http://www.slideshare.net/spikebrehm/jsconf-us-2014-building-isomorphic-apps

Page 15: Isomorphic web application

Traditional Multi-Page Application

• Server-rendered content,Improving user experience by using AJAX, JQuery, …

• Serving HTML on first-load is fast.

• Crawlers, screen-readers are happy with HTML.

But:

• Maintain UI render and logic in both client and server

• Duplicate application logic in (usually) two languages, two frameworks, two development stacks, two templates, …

Page 16: Isomorphic web application

Single Page Application

Page 17: Isomorphic web application

* http://www.slideshare.net/spikebrehm/jsconf-us-2014-building-isomorphic-apps

Page 18: Isomorphic web application

Single Page Application

• Client-rendered content

• Separate application logic and data retrieval

• More interactivity, optimistic UI, offline, mobile

But:

• Not SEO-friendly.• Still have to pre-render pages on server for crawlers.

• Users have to wait a few seconds of blank page or loading spinner before seeing the content.

Page 19: Isomorphic web application

Multi-Page Application

OR

Single-Page Application

Page 20: Isomorphic web application

We have another option:

Page 21: Isomorphic web application

Isomorphic Web ApplicationThe best of both worlds

We have another option:

Page 22: Isomorphic web application

* http://www.slideshare.net/spikebrehm/jsconf-us-2014-building-isomorphic-apps

Page 23: Isomorphic web application

Isomorphic Web Application

Mix server-rendered and client-rendered content

• On first page load, serve real server-rendered HTML.

• Client-side JS app bootstraps on top of server-rendered HTML rather than bootstrapping into an empty div.

• From that point on, it's a client-side JS app.

Page 24: Isomorphic web application

Taking the best of both worlds

• Performance• MPA - Serving fully-formed HTML is fast.

• SPA - AJAX, transport data instead of HTML.

• Connection quality• SPA can handle client state, request only needed data.

• SEO & Accessibility• MPA – crawlers & screen readers needs HTML. Google will always prefer MPA.

• UX• SPA - more interactivity, optimistic UI, offline, mobile.

• Maintainability

Page 25: Isomorphic web application

So, do I have to rewrite my app in NodeJs?It’s terrible!

Page 26: Isomorphic web application

So, do I have to rewrite my app in NodeJs?It’s terrible!

No, you don’t have to. Just use your preferred stack plus a few code for NodeJs.

Page 27: Isomorphic web application

Are there anyone building isomorphic app in production?

Page 28: Isomorphic web application

Are there anyone building isomorphic app in production?

Facebook Instagram RisingStack

Asana Meteor Yahoo! Mail(next version*)

* http://www.slideshare.net/rmsguhan/react-meetup-mailonreact

Page 29: Isomorphic web application

* http://www.slideshare.net/spikebrehm/jsconf-us-2014-building-isomorphic-apps

Page 30: Isomorphic web application
Page 31: Isomorphic web application

But don’t use it.

Page 32: Isomorphic web application

Let’s build our own Isomorphic application

Page 33: Isomorphic web application

Let’s build our own isomorphic application

•Understand Isomorphic JavaScript• Environment independent• Shimmed per environment

•Building blocks

Page 34: Isomorphic web application

UnderstandIsomorphic JavaScript

Page 35: Isomorphic web application

Environment-independent

•Do not depend on environment-specific features

Browser: window, DOM

NodeJs: process, “fs”

Page 36: Isomorphic web application

Shimmed per environment

•Provide shims for accessing environment-specific features so we can use the same API

Browser: xhr.open(‘GET’, ‘http://example.com’)

NodeJs: http.request({ host: ‘example.com’, path: ‘/’ })

Shim: superagent.get(‘http://example.com’)

Page 37: Isomorphic web application

Most of your favourite JS libraries are Isomorphic

Page 38: Isomorphic web application

You can use these libraries on server or client

jquery underscore

backbone handlebar

react mithril

moment numeral

superagent i18next

Page 39: Isomorphic web application

Building blocks

Page 40: Isomorphic web application

Building blocks: what do we need?

View: Render HTML with or without browser’s DOM.

Routing: Navigate through the application.

Communicating: Send AJAX or HTTP requests.

Stores: Store application states.

States: Transfer application states from server to client.

Bundling: Combine all our node modules to a single file that can run on browser.

Page 41: Isomorphic web application

Building blocks: which libraries can we use today?

View: react, handlebar

Routing: direction, flux-router-component

Communicating: superagent, fluxible-plugin-fetchr

Stores: flux, backbone model

States: express-state or implement your own

Bundling: browserify, web-pack

Page 42: Isomorphic web application

React

•Virtual DOM

Browser: React.render( <App />, document.body )

NodeJs: React.renderToString( <App /> )

Page 43: Isomorphic web application

Browserify

• require() node modules in browser.

•Provide shims for node-specific features that can run on browser.

•Bundle all modules to single script file.

Page 44: Isomorphic web application

Demousing React

Page 45: Isomorphic web application

Thanks for your listening !!

Vu [email protected]

Liti Solutions