Slide 1

Slide 1 text

Single-Page Applications: Challenges Minko Gechev github.com/mgechev twitter.com/mgechev blog.mgechev.com

Slide 2

Slide 2 text

Minko Gechev github.com/mgechev twitter.com/mgechev { "job": "Freelancer", "hobbies": [ "open source", "blogging", "teaching", "sports" ], "communityEvents": [ "SofiaJS", "BeerJS Sofia" ] }

Slide 3

Slide 3 text

Challenges in the modern web

Slide 4

Slide 4 text

Agenda • Title 1 • Subtitle 1 • Subtitle 2 • Title 2 • Subtitle 1 Lets take a look back…

Slide 5

Slide 5 text

When IE6 was bleeding edge…

Slide 6

Slide 6 text

…and Justin Bieber wasn’t on the TV

Slide 7

Slide 7 text

Everything was simple…

Slide 8

Slide 8 text

Client Server

Slide 9

Slide 9 text

Client Server

Slide 10

Slide 10 text

Client Server GET /

Slide 11

Slide 11 text

Client Server GET /

Slide 12

Slide 12 text

Client Server GET / GET * loop

Slide 13

Slide 13 text

Client Server GET / GET * loop

Slide 14

Slide 14 text

Caption …the role of JavaScript was limited

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

JavaScript’s initial purpose document.body.style.backgroundImage = 'url(unicorn.gif)';

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

jQuery jQuery(function ($) { var popup = function () { if ($('.popup_close').length > 0) { $('.popup_close').on('click', function () { $(this).closest('.wrapper_outer_popup').addClass('hidden'); }); $('.popup_target').on('click', function () { $('.popup[data-popup="' + $(this).attr('data-target') + '"]').removeClass('hidden'); }); } }(popup); }(jQuery));

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

jQuery spaghetti jQuery(function ($) { var popup = function () { if ($('.popup_close').length > 0) { $('.popup_close').on('click', function () { $(this).closest('.wrapper_outer_popup').addClass('hidden'); }); $('.popup_target').on('click', function () { $('.popup[data-popup="' + $(this).attr('data-target') + '"]').removeClass('hidden'); }); } }(popup); $(document).on('ready', function () { $('.sisea-results-list').each(function () { var elements = $(this).find('.sisea-result'), size = elements.size(), find = 10; for (var i = 0; i < find; i++) elements.eq(i).show(300); $(window).on('scroll', function() { var scrollTop = window.pageYOffset || document.documentElement.scrollTop, offset = $('#scroll').offset().top; if (scrollTop > offset - 800) { find < size ? find += 10 : find = size; for (var i = 0; i < find; i++) elements.eq(i).show(300); } }); }); }); }(jQuery));

Slide 23

Slide 23 text

File.extension …you know nothing Jon Snow

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

jQuery spaghetti jQuery(function ($) { var popup = function () { if ($('.popup_close').length > 0) { $('.popup_close').on('click', function () { $(this).closest('.wrapper_outer_popup').addClass('hidden'); }); $('.popup_target').on('click', function () { $('.popup[data-popup="' + $(this).attr('data-target') + '"]').removeClass('hidden'); }); } }(popup); $(document).on('ready', function () { $('.sisea-results-list').each(function () { var elements = $(this).find('.sisea-result'), size = elements.size(), find = 10; for (var i = 0; i < find; i++) elements.eq(i).show(300); $(window).on('scroll', function() { var scrollTop = window.pageYOffset || document.documentElement.scrollTop, offset = $('#scroll').offset().top; if (scrollTop > offset - 800) { find < size ? find += 10 : find = size; for (var i = 0; i < find; i++) elements.eq(i).show(300); } }); }); }); var popup_show_delay = 0.5; $('.in_basket').each(function(){ var $popup = $(this).find('.flyout'); var timeoutId = null; var showPopup = function() { $popup.removeClass('hidden'); }; var hidePopup = function() { $popup.addClass('hidden'); console.log('hide popup'); }; var $targets = $([ this, $popup.get(0) ]); $targets.on('mouseenter', function(){ console.log('show popup'); clearTimeout(timeoutId); showPopup(); }); $targets.on('mouseleave', function(){ clearTimeout(timeoutId); timeoutId = setTimeout(hidePopup, popup_show_delay * 1000); }); }); $(".doc_4").hover(function(){ $(".Kiril").css("display","block"); $(".beckzhan").css("display","none"); $(".Sergey").css("display","none"); $(".Asiya").css("display","none"); }); $(".doc_1").hover(function(){ $(".beckzhan").css("display","block"); $(".Kiril").css("display","none"); $(".Sergey").css("display","none"); $(".Asiya").css("display","none"); }); $(".doc_2").hover(function(){ $(".Sergey").css("display","block"); $(".beckzhan").css("display","none"); $(".Kiril").css("display","none"); $(".Asiya").css("display","none"); }); $(".doc_3").hover(function(){ $(".Asiya").css("display","block"); $(".beckzhan").css("display","none"); $(".Sergey").css("display","none"); $(".Kiril").css("display","none"); }); }(jQuery));

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

“Scaling” this way simply doesn’t work…

Slide 29

Slide 29 text

https://speakerdeck.com/jaffathecake/in-your-font-face

Slide 30

Slide 30 text

architecture |ˈɑːkɪtɛktʃəә| noun [ mass noun ] 1 the art or practice of designing and constructing buildings. schools of architecture and design. • the style in which a building is designed and constructed, especially with regard to a specific period, place, or culture: Georgian architecture. 2 the complex or carefully designed structure of something: the chemical architecture of the human brain. • the conceptual structure and logical organization of a computer or computer-based system.

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

– Martin Fowler “…decisions that are hard to change…” Architecture

Slide 33

Slide 33 text

Scalable JavaScript Application Architecture

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Decoupled modules, which communicate through an event bus

Slide 43

Slide 43 text

…a bit later

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

followed by the MV* fashion

Slide 46

Slide 46 text

A few MV* frameworks Backbone.js Flight KnockoutJS Knockback.js AngularJS Ember.js React MarionetteJS CanJS Vue.js Ampersand Mithril Aurelia Kendo UI PureMVC

Slide 47

Slide 47 text

A few MV* frameworks Backbone.js Flight KnockoutJS Knockback.js AngularJS Ember.js React MarionetteJS CanJS Vue.js Ampersand Mithril Aurelia Kendo UI PureMVC

Slide 48

Slide 48 text

A few MV* frameworks Backbone.js Flight KnockoutJS Knockback.js AngularJS Ember.js React MarionetteJS CanJS Vue.js Ampersand Mithril Aurelia Kendo UI PureMVC

Slide 49

Slide 49 text

A few MV* frameworks Backbone.js Flight KnockoutJS Knockback.js AngularJS Ember.js React MarionetteJS CanJS Vue.js Ampersand Mithril Aurelia Kendo UI PureMVC

Slide 50

Slide 50 text

A few MV* frameworks Backbone.js Flight KnockoutJS Knockback.js AngularJS Ember.js React MarionetteJS CanJS Vue.js Ampersand Mithril Aurelia Kendo UI PureMVC

Slide 51

Slide 51 text

A few MV* frameworks Backbone.js Flight KnockoutJS Knockback.js AngularJS Ember.js React MarionetteJS CanJS Vue.js Ampersand Mithril Aurelia Kendo UI PureMVC

Slide 52

Slide 52 text

Most frameworks were following the same model

Slide 53

Slide 53 text

– Wikipedia “Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces.” Model–view–controller

Slide 54

Slide 54 text

Controller Model View

Slide 55

Slide 55 text

Controller Model View Creates

Slide 56

Slide 56 text

Controller Model View Delegates

Slide 57

Slide 57 text

Controller Model View Manipulates

Slide 58

Slide 58 text

Controller Model View Notifies

Slide 59

Slide 59 text

…and a bit later…

Slide 60

Slide 60 text

Controller Model View

Slide 61

Slide 61 text

Controller Model View

Slide 62

Slide 62 text

However, they kept the same base…

Slide 63

Slide 63 text

– Wikipedia “The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their method.” Observer Pattern

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

this.name  =  name;

Slide 66

Slide 66 text

Update the user badge!

Slide 67

Slide 67 text

this.name  =  name;   $(‘#badge  .name’)      .html(name);

Slide 68

Slide 68 text

Notify all other users!

Slide 69

Slide 69 text

this.name  =  name;   $(‘#badge  .name’)      .html(name);   foreach  u  in  users  {      u.send({
        type:  ‘change’,
        name:  name      });   }

Slide 70

Slide 70 text

Update the database!

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

this.name  =  name;   foreach  o  in  obs  {      o.update(name);
 }

Slide 73

Slide 73 text

which has two big pitfalls…

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Tangled data-flow

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Complex mutable state

Slide 89

Slide 89 text

Flux came out!

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

Flux does: • Enforces the uni-directional data-flow • Keeps the UI as stateless as possible

Slide 92

Slide 92 text

Flux does: • Enforces the uni-directional data-flow • No more cascading updates and hidden dependencies • Keeps the UI as stateless as possible • No more troubles to reason about the state

Slide 93

Slide 93 text

But flux is not the silver bullet

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

JavaScript has some good parts…

Slide 96

Slide 96 text

• Dynamic • Has unusual object-oriented model However, it is…

Slide 97

Slide 97 text

What will make us be more productive less error-prone?

Slide 98

Slide 98 text

Easy to learn

Slide 99

Slide 99 text

Agenda • Title 1 • Subtitle 1 • Subtitle 2 • Title 2 • Subtitle 1 Better IDE/text editors support

Slide 100

Slide 100 text

Agenda • Title 1 • Subtitle 1 • Subtitle 2 • Title 2 • Subtitle 1 Compiler to take care of us

Slide 101

Slide 101 text

Requirements • Statically typed • More powerful syntax with similar semantics • Possibly superset of JavaScript

Slide 102

Slide 102 text

Adding new language in the browser is impossible

Slide 103

Slide 103 text

Our Awesome Language JavaScript

Slide 104

Slide 104 text

Our Awesome Language JavaScript Transpiler

Slide 105

Slide 105 text

Adding new language in the browser is impossible

Slide 106

Slide 106 text

Languages aiming to “fix” JS TypeScript CoffeeScript ClojureScript Dart Elm ToffeeScript Coco Caffeine Jack LispyScript GorillaScript LiteScript asm.js JSX Typecast.js PureScript AtScript Flow Pyjamas

Slide 107

Slide 107 text

Languages aiming to “fix” JS TypeScript CoffeeScript ClojureScript Dart Elm ToffeeScript Coco Caffeine Jack LispyScript GorillaScript LiteScript asm.js JSX Typecast.js PureScript AtScript Flow Pyjamas

Slide 108

Slide 108 text

TypeScript is:

Slide 109

Slide 109 text

TypeScript is: • Statically typed ✓ Has compiler ✓ Has better IDE/text editors support • Superset of ECMAScript 2015+ ✓ Is easier to learn ✓ Has syntax sugar

Slide 110

Slide 110 text

TypeScript is: • Statically typed ✓ Has compiler ✓ Has better IDE/text editors support • Superset of ECMAScript 2015+ ✓ Is easier to learn ✓ Has syntax sugar

Slide 111

Slide 111 text

TypeScript is: • Statically typed ✓ Has compiler ✓ Has better IDE/text editors support • Superset of ECMAScript ✓ Is easier to learn ✓ Has syntax sugar

Slide 112

Slide 112 text

TypeScript is: • Statically typed ✓ Has compiler ✓ Has better IDE/text editors support • Superset of ECMAScript ✓ Is easier to learn ✓ Has syntax sugar

Slide 113

Slide 113 text

TypeScript + Flux =

Slide 114

Slide 114 text

Applications that scale

Slide 115

Slide 115 text

In SPA we get all or nothing

Slide 116

Slide 116 text

Client Server

Slide 117

Slide 117 text

Client Server

Slide 118

Slide 118 text

Client Server GET / GET * loop

Slide 119

Slide 119 text

Client Server GET / GET * loop Running JavaScript

Slide 120

Slide 120 text

Client Server GET / GET * loop GET * loop

Slide 121

Slide 121 text

Client Server GET / GET * loop GET * loop

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

Performance

Slide 128

Slide 128 text

Initial load performance

Slide 129

Slide 129 text

In loading a SPA • DNS resolution • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 130

Slide 130 text

In loading a SPA • DNS lookup • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 131

Slide 131 text

In loading a SPA • DNS lookup • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 132

Slide 132 text

In loading a SPA • DNS lookup • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 133

Slide 133 text

In loading a SPA • DNS lookup • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 134

Slide 134 text

In loading a SPA • DNS lookup • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 135

Slide 135 text

In loading a SPA • DNS lookup • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 136

Slide 136 text

In loading a SPA • DNS lookup • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 137

Slide 137 text

Where we can do something?

Slide 138

Slide 138 text

In loading a SPA • DNS resolution • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 139

Slide 139 text

In loading a SPA • DNS resolution • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 140

Slide 140 text

In loading a SPA • DNS resolution • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns • Minification • Concatenation • Sprites • HTTP 2.0

Slide 141

Slide 141 text

In loading a SPA • DNS resolution • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns • Minification • Concatenation • Sprites • HTTP 2.0 • Deferred script parsing

Slide 142

Slide 142 text

In loading a SPA • DNS resolution • Establishment of a TCP connection • Establishment of secure connection • Fetching all the resources • Blocking scripts • Parsing all the JavaScript files • Bootstrapping the application • Rendering the content Slowdowns

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

What if… • Make a request to the server with our SPA URL • Server: • Parses the URL • Invokes the JavaScript associated to it • Returns the markup generated by the JavaScript • Browser adds event listeners to the server generated DOM

Slide 145

Slide 145 text

Client Server

Slide 146

Slide 146 text

Client Server

Slide 147

Slide 147 text

Client Server GET / GET * loop

Slide 148

Slide 148 text

Client Server GET / GET * loop Running JavaScript

Slide 149

Slide 149 text

Client Server GET / GET * loop

Slide 150

Slide 150 text

Server-Side Rendering

Slide 151

Slide 151 text

Server-side rendering • Render our single-page application on the server • SEO friendly applications • Improved user experience

Slide 152

Slide 152 text

We can use server-side rendering today!

Slide 153

Slide 153 text

No content

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

Resources • Flux Overview • Flux in Depth. Overview and Components. • Flux in Depth. Store and Network Communication. • TypeScript • Angular 2 • React server-side rendering • Angular 2 server-side rendering

Slide 156

Slide 156 text

Thank you! github.com/mgechev twitter.com/mgechev blog.mgechev.com