client vs server templating: speed up initial load for spa with angular as an example

76
Warp-Beam-speed at initial load Speeding up your JavaScript application

Upload: david-amend

Post on 27-Jan-2015

106 views

Category:

Software


0 download

DESCRIPTION

Speed up initial load for SPA with Angular as an example. Server vs Client Templating View Rendering, David Amend

TRANSCRIPT

Page 1: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Warp-Beam-speedat initial load

Speeding up your JavaScript application

Page 2: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

David Amend● JavaScript & Java Frontend

Developer &Architect

● @Dab-Bank: Angular.js, BPMN, OSGi

[email protected]

That’s me

Page 3: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Client- Server-VS

http://openmymind.net/2012/5/30/Client-Side-vs-Server-Side-Rendering/

Better User-Experience?

View-Rendering

Page 4: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Topics

1. History of Web-Rendering Techniques2. The Application Workflow3. Decision to the best architecture

Page 5: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Process of Rendering-Technologies

Static HTML

CGI Scripts

Web-specific

19951989 C,Shell,CGI-Scripts,Phyton VB-Scripts, Perl ASP, PHP, Ruby,

ColdFusion

Page 6: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Process of Rendering-Technologies

Web-plugins Web-FrameworksServer-Driven

~20101998

Applet/Flash, ActiveX, XAML

Ruby on Rails, .Net, Wicket, GWT

Node.js based“JS Everywhere”

Express, Derby, Meteor, OPA, ...

Page 7: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Process of Rendering-Technologies

Today Future?

?...

● BeforeFramework Comparison matters→ Richness & stability of Frameworks

● NowTechnique & Architecture matters→ Surpass limitations

● FutureWebtechnologies get more important, HTML-5, ECMA-Script 7→ Optimized & Fast Development

Page 8: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

“MVC”-Code Architectures

Page 9: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Server or Client rendering ?

Page 10: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Client-Centric

Business-Logic

Client Server

Render-Engine

DB

Request

Response

Page 11: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Shared Definition

http://onehungrymind.com/angularjs-dynamic-templates/

Business-Logic

Client Server

Render-Engine

Shared-Definition

DB

DefinesDefines

I18n, View-Templates

Page 12: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

UI Session-State on Server

View-StateDefinitions

Client Server

Browser

Clicked On Button

Return HTML & pass JavaScript to be executed

Expensive- Resources

Less Control over User-Experience

Page 13: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Flow of Page-Initialization

LoadHTML

LoadAssets

FrameworkInit/Bootstrap

PhaseAJAX-calls

TemplateRendering

Replace HTML

1. 2. 3. 4.

Page 14: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Worst Case Flow

LoadHTML

LoadAssets

FrameworkInit/Bootstrap

PhaseAJAX-calls

TemplateRendering

Loading Screen

Replace HTML

User sees page in one shot

1. 2. 3. 4.

Page 15: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Worst Case Flow

LoadHTML

LoadAssets

FrameworkInit/Bootstrap

PhaseAJAX-calls

TemplateRendering

Loading Screen

Replace HTML

1. 2. 3. 4.Possible Flicking

a)b)

Page 16: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Hack the intialization process

● Minify● Cache● Load assets asynchronously● Load content asynchronously● Reducing Requests

Faster file loading

Page 17: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Async Loading, splitting files

HTML

Asset

FrameworkBootstrap Deferred

Ajax & RenderTemplate

Rendering

Async(e.g. module loader)

Apply BindingJS Templ

ate

HTML AssetAsset

Page 18: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Client-Only, Single file,

LoadHTML

LoadAssets

FrameworkBootstrap Deferred

Ajax & RenderTemplate

Rendering

Concat & precomile to single file

Apply Binding

Page 19: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Combination of ng-init & UIRouter

Grunt.js build:

● Precompilehttps://github.com/ericclemmons/grunt-angular-templates

https://github.com/karma-runner/karma-ng-html2js-preprocessor

https://github.com/karlgoldstein/grunt-html2js

● Concat files

Page 20: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Reducing Requestsor

Load Asynchronously

Faster file loading

Page 21: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Situation

Public Private

Slow-Js-RenderingNon-Blocking

Page 22: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Lazy Template Rendering

<div ng-template=”pizzaTemplate”> <h2 ng-model=”{{i18n.header}}></h2> <ul> <li ng-repeat=”pizza in pizzas”> </ul></div>

● h2 and li render deferred● Render when data are available, independent● Render when data changed: Databinding

Page 23: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Less Requests & Non Blocking UI

<html><head> <script>/* Inline Angular.js asset*/</script> <script>/* Inline async asset loader*/</script> $script([ 'js/app.js'], function() { angular.bootstrap(document, ['myApp']); });</head><body> <div ng-view></div></body></html>

Page 24: Client vs Server Templating: Speed up initial load for SPA with Angular as an example
Page 25: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

ng-cloak & ng-hide

● ng-cloakDisables lazy-rendering for all children nodes

● ng-hideReplacing of initial HTMLwith deferred template-rendered

● No Flicking

Page 26: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Display Initial HTML→ Performance Boost

LoadHTML

LoadAssets

FrameworkBootstrap Deferred

Ajax & RenderTemplate

Rendering

Apply Binding

● Load assets after HTML is loaded● Will not freeze UI

Page 27: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

How to combine Initial HTML withdeferred template Rendering ?

● Share Model Information● Share HTML within template

● SEO ( Disabled JS support)

Page 28: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Mixture of Client-Server Code

<h1>{{pizza.title}}</h1>#{for pizza:pizzas}<li ng-repeat={{pizza}}>

#{pizza.name}<li>#{for:end}

● Hard to understand● No separation

Page 29: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Complete Separation

index.vm, Server side:<h1>#{pizza.title}</h1> ...

index.html, Client Side:

<h1>{{pizza.title}}</h1> …● Separation● Duplication

Page 30: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Share model Information:Save in variable

http://mircozeiss.com/how-to-pass-javascript-variables-from-a-server-to-angular/

var model = #{pizzas.toJson()}

● Works with all frameworks● Simple● Need to touch logic code

Page 31: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Share model Information:Separate Service call

http://mircozeiss.com/how-to-pass-javascript-variables-from-a-server-to-angular/

app.get(‘pizzas’), function(request, response){$scope.pizzas = response;

}

● Clear separation of Data● No server-side code in client-code● Additional service call● Blocking UI

Page 32: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Share model Information:Separate Service call

http://mircozeiss.com/how-to-pass-javascript-variables-from-a-server-to-angular/

<div ng-init=’#{pizzas.toJSObject()}’>{{pizza.header}}

</div>

● No need to touch logic● Executed before Angular.js Bootstrap● Minimum Server-Templating needed● Needs Angular assets to be loaded● Angular.js specific

Page 33: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Render Client-Template before init-phase

LoadHTML

LoadAssets

FrameworkBootstrap Deferred

Ajax & RenderTemplate

Rendering

Apply Binding

Usage of ng-init

Page 34: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Share init HTML within template

Page 35: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Reuse of HTML within template at Initial StateSituationTemplate Engine not rendered, yet

<div>{{pizza.price}}</div>

Page 36: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Reuse of HTML within template at Initial StateSituationTemplate Engine not rendered, yet

<e:div>{{pizza.price}}</e:div>

Page 37: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Reuse of HTML within template at Initial StateSituationTemplate Engine not rendered, yet

<div ng-model={{pizza.price}}></div>

● Reuse of html within the template for initial state

Page 38: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Client or Server-Side Rendering

revised

Page 39: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Client-Only

Server

Client

Data-ServiceJSON, XML

Page 41: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Considerations

● Knownledge in team○ Java or Web-Developers?

● Context of Webapplication○ Webpage behind Login?

■ Prefetch & Initialize○ Only used in company or

■ Performance & browser supportcan be ignored

○ Public

● Save Money○ Do not do overegineering

Page 42: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Client-Server-Hybrid:Same Template Framework

Client WebServerTemplate Files

Initial Request

HTML

● e.g. MustacheGoogle Closure, Dual,...

● Share template files between Client & Server

Page 43: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Client-Server-Hybrid:Render JS on Server

Client

RenderingEngine

WebServerInitial Request

HTML-String

Identical Client Code

RegisterBindings

● Independant of template engine● No extra development effort● Could be Dependant on Browser Node.js

Page 44: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Lessons learned

● Twitterhttp://engineering.twitter.com/2012/05/improving-performance-on-twittercom.htmlhttp://www.youtube.com/watch?v=sGpHDHAIpYE

● Yahoohttps://github.com/davglass/nodejs-yui3

● Angular.js 2.0: Announced http://angularjs.org/

● Google, Closure Tools● Google Apps Script

Page 45: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Hybrid:Executed page in JavaScript Environment

Are in experimental state:

● GWT● Headless browsers: PhantomJS● Services: BromBone● Ruby solutions● Rendr (Backbone & Handelbars)

http://de.slideshare.net/spikebrehm

● Google Scripts● Meteor● Mojito

http://de.slideshare.net/spikebrehmhttp://sebgoo.blogspot.de/2013/05/angularjs-and-seo-pre-render-content-on.html

https://speakerdeck.com/seanami/bridging-the-client-side-server-side-divide

Page 46: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Any Questions ?

Page 47: Client vs Server Templating: Speed up initial load for SPA with Angular as an example
Page 48: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Additonal Slides

Page 49: Client vs Server Templating: Speed up initial load for SPA with Angular as an example
Page 50: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Adoption

Ways● By Server-centric● By Client-centric● By complete Client-Server Separation● By sharing same Client-Server Template API

→ How about i18n-texts ?● By sharing logic between Client-Server

(Validation, DataTransferObjects...)→ Angular.js as an example

Page 51: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Server or Client rendering ?

ClientServer

Different Syntax forces separation

JavaScript, XML:Reuse of code:Validation, DataTransferObjects

Caching

Less Money

For Slow Devices

Slow Network

Separation

...

Different Syntax forces separation

SEO

Initial Load is x5 faster

Natural Programming for Applications: Controller&View on the Client machine

Full control over Client

Rich User Experience

Use of Data-Services(JSON, XML)

Page 52: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

● Authentication-Information Request● i18n Request

Page 53: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Client Only

“Server is Overkill”, Tim Oxley http://www.youtube.com/watch?v=BgXjJ3aDwtc

“Goodbye Server, Welcome Client”, Sandro Sonntaghttp://es.slideshare.net/sandrosonntag/goodby-server-hello-client

Page 55: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Acceptance Criteria

● Peformance● Component model

Goals for the View-Part

Page 56: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Software Quality/Features

● Data binding● Standard supports● Internationalization● SEO● Long term● Reduce duplication● Less Development Time

Goals for the View-Part

Page 57: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Want to hide/wrap/ignore the Web?

Code Generation

Too Abstract Layering

Easy extendable

vs.

Easy combination of Frameworks

Easy Customizeable

Performance Optimization?

Angular.js Primary

Nice Entry Points

Page 58: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Bad: Different model concepts

LoadHTML

LoadAssets

FrameworkBootstrap Deferred

Ajax & RenderTemplate

Rendering

Apply Binding

<script src=”#{locale}.js>

● 3 Different ways to save i18n

Server-Sidetemplating

static asset.js I18n: within JSON data

Page 59: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Techniques & Webtechnologies

Page 60: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Angular.js possibilities

<div ng-init=”pizzaModel”> <h2 ng-cloak ng-bind=”{{pizza.header}} ></h2> <img src=”loading.gif” ng-hide=”true”></img> <ul ng-hide=”{{doneLoading}}”> <li ng-repeat=”pizza in pizzas”> </ul></div>

Page 61: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Combination of View-Rendering

DeclarativeImperative

Logic ViewLogicObject- Oriented

XML,XSLT Hard to merge Component-Frameworks

Sencha, Dojo, jQueryUI

CompositionFits to HTML/DOM

Always Code-Generation

ViewLogic

Page 62: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Types Of View-Definition

Page 63: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Drag & Drop / Generation

Drag-&Drop-Designer

Wavemaker, Oracle Forms...

Page 64: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

UI Session-State on Server, Client-Renderer

http://onehungrymind.com/angularjs-dynamic-templates/

View-StateDefinitions

Client Server

Render-Engine

Clicked On Button

Trigger Rendering of Window

e.g. GWT(Vaadin), Metawidget,...

Expensive- Resources

Page 65: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Imperative vs. Declarative

if(condition){ $(‘#pizzaHeader’).hide()}else{ $(‘#pizzaHeader’).show();}

vs. <div ng-hide=”condition”>content</div>

Page 66: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Performance: Flow of Rendering

LoadHTML

LoadAssets

FrameworkBootstrap Deferred

Ajax & RenderTemplate

Rendering

Apply Binding

Angular.js does Lazy Loading

TemplateRendering

TemplateRendering

TemplateRendering

Page 67: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Types of View Definition

Page 68: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Declarative

Simple String-Replacement

Bi-Directional Databindingvs.

e.g. JSP, PHP, Ruby, ASP

Dojo, Angular, JSF

Initial state is smooth

Do not fight the DOMExpensive !!!

Server-Side:Expensive

Page 69: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Load All In One Shot/Requestvs.

Lazy/Async Modularized Loading

e.g. jQuery Mobile

http://ify.io/lazy-loading-in-angularjs/

Page 70: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Declarative

Full Conditions, Loop, Logic Support

Logic-Free Templatesvs.

Less as possible

e.g. HandlebarsRead-Logic only

Page 71: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Templatinghttp://stackoverflow.com/questions/13103050/angularjs-client-side-data-binding-and-server-side-templating

http://mircozeiss.com/how-to-pass-javascript-variables-from-a-server-to-angular/

● http://ziade.org/2013/02/13/fun-with-angularjs-amp-cornice/

● http://blog.thousandeyes.com/improving-end-user-experience-with-angularjs/

● 2.0v (server-side-prerendering, modularized, compile)

●● Maintaining?● Speed?-Initial Load ? Flicker?● Needed? - SEO, Work without JS?●●

Page 72: Client vs Server Templating: Speed up initial load for SPA with Angular as an example

Templating 2nd● http://docs.angularjs.org/api/ng.directive:script● https://github.com/ericclemmons/grunt-angular-templates● https://github.com/karlgoldstein/grunt-html2js● https://github.com/wmluke/grunt-inline-angular-templates (shameless plug)

http://www.bennadel.com/blog/2443-Rendering-DOM-Elements-With-ngRepeat-In-AngularJS.htm

● Angular magically does template rendering of the view ○ Optimization○ Huge potential of optimization on the client side○ Optimization of server side rendering is an illusion.

● Async, ng-cloak● ng-bind vs. {{}}● class usage disturbs designers● make the DOM your friend

“80% DOM, 20% logic” ( Misko Hevery)● Load all in one shot vs load all lazy loaded● lazy loading in the background (jquery Mobile)

Page 74: Client vs Server Templating: Speed up initial load for SPA with Angular as an example
Page 75: Client vs Server Templating: Speed up initial load for SPA with Angular as an example