single page webapp architecture

61
Single-Page-WebApp Architecture 程程 Morgan Cheng @morgancheng

Upload: mocheng

Post on 12-May-2015

10.828 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Single Page WebApp Architecture

Single-Page-WebApp Architecture

程墨 Morgan Cheng@morgancheng

Page 2: Single Page WebApp Architecture

Traditional Web

Web Page

Web PagePage HTML

Server Browser

Navigate

Page HTML

Page 3: Single Page WebApp Architecture

Single Page WebApp

Web Page

XHR Response

Server Browser

Navigate

XHR Response

Navigate

Page HTML

Page 4: Single Page WebApp Architecture

Not Just AJAX

Page 5: Single Page WebApp Architecture

It Should Work as Web

Page 6: Single Page WebApp Architecture

Bookmark-able

Navigable

Search-Engine-Friendly, if necessary

Page 7: Single Page WebApp Architecture

VS

Page 8: Single Page WebApp Architecture

Why to Single-Page?

Page 9: Single Page WebApp Architecture

Squeeze Bits for Better Experience

Page 10: Single Page WebApp Architecture

When to Single-Page?

Page 11: Single Page WebApp Architecture

Frequently Navigated Page

Partial Difference Among Pages

Performance Critical

Page 12: Single Page WebApp Architecture

How to Single-Page?

Page 13: Single Page WebApp Architecture

Now we totally depend on JavaScript

Page 14: Single Page WebApp Architecture

"The secret to building large apps is never build large apps. Break your applications into small pieces. Then, assemble those testable, bite-sized pieces into your big application"

-Justin Meyer, author JavaScriptMVC

Page 15: Single Page WebApp Architecture

Singe Page App

Client Side Routing Client Side Rendering

=

+

Page 16: Single Page WebApp Architecture

First, Client-Side Routing

Page 17: Single Page WebApp Architecture

Multiple Framework Choices

https://github.com/addyosmani/todomvc

Page 18: Single Page WebApp Architecture

3.4.0 App F/W

Page 19: Single Page WebApp Architecture

Ryan Grove

Why YUI3.4 app framework doesn't allow to use hash style URL for all browsers?

It was very important to make doing the right thing easy and doing the wrong thing hard.

Page 20: Single Page WebApp Architecture

Server Just Render HTML Skeleton

Page 21: Single Page WebApp Architecture

Define Routesvar controller = new Y.Controller({

routes: [

{path: “/”,callback: onHomePage

},{

path: “/user/:guid”,callback: onUserPage

}

]});

onHomePage is invoked when route “/” is triggered

Page 22: Single Page WebApp Architecture

Dispatch on DOM Ready

controller.dispatch();

Trigger route according to current page URL

Page 23: Single Page WebApp Architecture

Save on Navigation

Y.delegate('click', onNaviLinkClick, 'body', '.navi-link', this);

function onNaviLinkClick() { … var newPath = currTarget.getAttribute('href');

if (controller.getPath() != newPath) {controller.save(newPagePath);

}} Trigger route according to

newPagePath

Page 24: Single Page WebApp Architecture

Routing Module

Page A Page B Page C

Page Load&Page Navigation

Page 25: Single Page WebApp Architecture

Second, Client-Side Rendering

Page 26: Single Page WebApp Architecture

Routing Module

Page A Page B Page C

Page Load&Page Navigation

Widget X Widget Y Widget Z

Page 27: Single Page WebApp Architecture

Widget or View-Model

Page 28: Single Page WebApp Architecture

Decouple Modules with Events

Page 29: Single Page WebApp Architecture

Routing Module

Page A Page B Page C

Page Load&Page Navigation

Widget X

Event System

Widget Y Widget Z

Page 30: Single Page WebApp Architecture

Don’t Repeat Yourself

Page 31: Single Page WebApp Architecture

Logic-less Template: Mustache

http://mustache.github.com/

Page 32: Single Page WebApp Architecture

<div id="post_{{pid}}”{{#post}}data-original-pid="{{pid}}" {{/post}}

>

<div id="post_123”data-original-pid=”456"

>

{pid: ‘123’, post: {

pid: ‘456’}

}

Page 33: Single Page WebApp Architecture

Ask Again :Why to Single-Page?

Page 34: Single Page WebApp Architecture

It should be Fast

Page 35: Single Page WebApp Architecture

近身 == Download It Fast发力 == Run It Fast

Page 36: Single Page WebApp Architecture

Download It Fast

Page 37: Single Page WebApp Architecture

Non-Blocking JavaScript

Page 38: Single Page WebApp Architecture

function loadJS(path) { var script = document.createElement("script"); script.type = "text/javascript"; script.src = path; document.getElementsByTagName("head")[0].appendChild(script);}

Insert into <head>

Page 39: Single Page WebApp Architecture

Flush It!

Page 40: Single Page WebApp Architecture

…</head><?phpob_flush();flush();?><body>

Flush for browser incremental rendering

Page 41: Single Page WebApp Architecture

WTF?

Page 42: Single Page WebApp Architecture

Position Inline JavaScript

http://www.stevesouders.com/blog/2009/05/06/positioning-inline-scripts/

Page 43: Single Page WebApp Architecture

function loadJS(path) { var script = document.createElement("script"); script.type = "text/javascript"; script.src = path; var first = document.getElementsByTagName(‘script’)[0]; first.parentNode.insertBefore(script, first);}

Insert before first <script>

Page 44: Single Page WebApp Architecture

Position Inline JavaScript Before External Style Link

Page 45: Single Page WebApp Architecture

Caching:Better Than Downloading

Page 46: Single Page WebApp Architecture

Widget

Data Source

CacheAJAX Inline Script

Page 47: Single Page WebApp Architecture

Run It Fast

Page 48: Single Page WebApp Architecture

JavaScript Execution is Not Free

Page 49: Single Page WebApp Architecture

Client-side Rendering Depends on CPU & JS Engine

Page 50: Single Page WebApp Architecture

Mustache Performance Suffers From Nested Data

Page 51: Single Page WebApp Architecture

{ pid: ‘123’, entities: { images:

[{image_medium: … ,image_small: … ,image_big: … ,}]

}}

Performance is Not Good

Page 52: Single Page WebApp Architecture

{ pid: ‘123’, entities_images_0_image_medium: …, entities_images_0_image_medium: …, entities_images_0_image_medium: …,}

Twice faster!

Page 53: Single Page WebApp Architecture

Chunked Computation

Page 54: Single Page WebApp Architecture

Web Worker

Page 55: Single Page WebApp Architecture

How to Render First Page?

Page 56: Single Page WebApp Architecture

Twitter Approach

• The first page response is just HTML skeleton

• Web is a client of its Open API

• The page is initialized with multiple AJAX response

Page 57: Single Page WebApp Architecture

Facebook Approach

• The first page response is HTML Skeleton with tailing inline JavaScript

• Init data is flushed in tailing JavaScript block– It is called BigPipe

• The sequence of module rendering depends

Page 58: Single Page WebApp Architecture

Google+ Approach

• The first page response is complete HTML

Page 59: Single Page WebApp Architecture

Which is Better?

A. Twitter Approach

B. Facebook Approach

C. Google+ Approach

Page 60: Single Page WebApp Architecture

Take-Away

• Leverage Framework

• Make decision according to app requirement

• Watch the Performance

Page 61: Single Page WebApp Architecture

Thank You!