how not to get lost in the current javascript landscape

75
How NOT to get lost in the current JS landscape @rscheibinger

Upload: radoslaw-scheibinger

Post on 28-Jul-2015

545 views

Category:

Software


6 download

TRANSCRIPT

Page 1: How NOT to get lost in the current JavaScript landscape

How NOT to get lost in the current JS landscape

@rscheibinger

Page 2: How NOT to get lost in the current JavaScript landscape

Radosław Scheibinger

@rscheibingercodeabroad.com

Software Engineer at

Page 3: How NOT to get lost in the current JavaScript landscape

This talk is about

Page 4: How NOT to get lost in the current JavaScript landscape

In the context of

Progressive enhancement

Single Page Applications

Page 5: How NOT to get lost in the current JavaScript landscape

2015

● HTML5 certified last year● IE8 Usage ~ 2%● CSS3● EcmaScript 6● NodeJS● HTTP2● Users expect page load time < 1 second

Page 6: How NOT to get lost in the current JavaScript landscape

Seriously?

● JSP

● JSF

● *** Faces

● XHTML

● Google Web Toolkit

Page 7: How NOT to get lost in the current JavaScript landscape

Embrace The Web

Page 8: How NOT to get lost in the current JavaScript landscape

How do we build web apps nowadays?

Page 9: How NOT to get lost in the current JavaScript landscape

Traditional Page lifecycleServer

Client

HTML

Initial request

User Action - Post Request

HTML & Page Reload

Page 10: How NOT to get lost in the current JavaScript landscape

Single page application

...

ServerClient

HTML

Initial request

User Action - Ajax Request

JSON

Page 11: How NOT to get lost in the current JavaScript landscape
Page 12: How NOT to get lost in the current JavaScript landscape

Feeling fast?

0 -100ms Instant Perception100 - 300ms Small perceptible delay300 -1000ms Machine is not working1000+ ms Likely mental context switch10000+ ms Task is abandoned

Page 13: How NOT to get lost in the current JavaScript landscape

Permanent abandonment rate

People who never come back- Outage 9%- Slow performance 28%

source: Akamai: The impact of Web Performance on E-Retail Success

Page 14: How NOT to get lost in the current JavaScript landscape

Taking step back toProgressive Enhancement

Page 15: How NOT to get lost in the current JavaScript landscape

Progressive Enhancement

● Content (HTML)● Presentation (CSS)● Behaviour (JavaScript)

Page 16: How NOT to get lost in the current JavaScript landscape

Progressive Enhancement

is important:● Improving web performance● page load < 1s● SEO

Page 17: How NOT to get lost in the current JavaScript landscape

SPA How to

Page 18: How NOT to get lost in the current JavaScript landscape

SPA Checklist

- do I need SEO?- is your app behind a login?- is my app content driven?

- learning curve- frameworks

- do I need first page load performance?- is my app small enough for SPA?

Page 19: How NOT to get lost in the current JavaScript landscape

SPA Frameworks

- AngularJS- Backbone - ember

Page 20: How NOT to get lost in the current JavaScript landscape

SPA Frameworks

Metric AngularJS Backbone.js Ember.js

Stars on Github 27.2k 18.8k 11k

Third-Party Modules 800 ngmodules 236 backplugs 21 emberaddons

StackOverflow Questions 49.5k 15.9k 11.2k

YouTube Results ~75k ~16k ~6k

GitHub Contributors 928 230 393

Chrome Extension Users 150k 7k 38.3k

Page 21: How NOT to get lost in the current JavaScript landscape
Page 22: How NOT to get lost in the current JavaScript landscape

Cannot afford SPA?

Build your App with:• Traditional Page Lifecycle model• progressive enhancement

Page 23: How NOT to get lost in the current JavaScript landscape

Fixing Traditional Page Lifecycle problems

- Reloading entire pages when submitting forms causes Flash of the content

- Apps are not interactive- Feels slow

Page 24: How NOT to get lost in the current JavaScript landscape

Hijax

only using AJAX techniques to 'hijack' form submissions and responses

Page 25: How NOT to get lost in the current JavaScript landscape

HIJAXServer

Client

HTML

Initial request

User Action - Ajax Request

JSON/ HTML Partial

Page 26: How NOT to get lost in the current JavaScript landscape

Hijax problems

- back button issue- abuse of hashbangs: #!/no/more- I like to share links to stuff I see- can’t bookmark without proper url

We need proper URLs - why?

Page 27: How NOT to get lost in the current JavaScript landscape

HTML5 History Apiand PJAX

Page 28: How NOT to get lost in the current JavaScript landscape

PJAXServer

Client

HTML

Initial request

User Action - Ajax Request

JSON/ HTML Partial

+ History pushState

Page 29: How NOT to get lost in the current JavaScript landscape
Page 30: How NOT to get lost in the current JavaScript landscape
Page 31: How NOT to get lost in the current JavaScript landscape

Code duplication Problem

- Localization- Rendering Templates- Date/Currency formatting- Form Validation- Routing Logic

Page 32: How NOT to get lost in the current JavaScript landscape
Page 33: How NOT to get lost in the current JavaScript landscape

Isomorphic JavaScript

- JavaScript on the Frontend- JavaScript on the Backend

Page 34: How NOT to get lost in the current JavaScript landscape

http://nerds.airbnb.com/wp-content/uploads/2013/11/Screen-Shot-2013-11-07-at-10.29.32-AM.png

Page 35: How NOT to get lost in the current JavaScript landscape

REST Services

Web Browsers - Multiscreen

Mobile Apps

UI Backend

PresentationLogic

Business Logic

Page 36: How NOT to get lost in the current JavaScript landscape

Need for tools

● No one likes writing plain CSS?● Reduce boilerplate code● Reduce manual work● Sprites, fonts etc...● Optimization, Website has to load super fast.

Page 37: How NOT to get lost in the current JavaScript landscape

Hold on ?

Best ecosystem for frontend tooling

Page 38: How NOT to get lost in the current JavaScript landscape

Getting Started with nodejs tooling

meet Yeoman

Page 39: How NOT to get lost in the current JavaScript landscape
Page 40: How NOT to get lost in the current JavaScript landscape
Page 41: How NOT to get lost in the current JavaScript landscape

module.exports = function (grunt) { require('load-grunt-tasks')(grunt); var pkgConfig = grunt.file.readJSON('package.json'); grunt.initConfig({ pkg: pkgConfig,

webpack: {...}, 'webpack-dev-server': {...}, connect: {...}, open: {...}, karma: {...}, copy: {...}, clean: {...} }); grunt.registerTask('serve', function (target) {}); grunt.registerTask('test', ['karma']); grunt.registerTask('build', ['clean', 'copy', 'webpack']); grunt.registerTask('default', []);};

Gruntfile.js ?

Page 42: How NOT to get lost in the current JavaScript landscape

Build tools

● gulp 13006☆● grunt 9381☆● brunch 4018☆● broccoli 2092☆

Page 43: How NOT to get lost in the current JavaScript landscape

GRUNT

Page 44: How NOT to get lost in the current JavaScript landscape

Grunt(trial)

Grunt is rapidly becoming the de facto JavaScript build tool with high adoption and a growing ecosystem. While slower than newer alternatives, such as Gulp, in terms of file processing, Grunt covers a broader set of build-related activities, has a proliferation of plugins and makes it easy to author and publish self-written plugins to npm.

Page 45: How NOT to get lost in the current JavaScript landscape
Page 46: How NOT to get lost in the current JavaScript landscape

https://github.com/osscafe/gulp-cheatsheet

Page 47: How NOT to get lost in the current JavaScript landscape

https://github.com/osscafe/gulp-cheatsheet

Page 48: How NOT to get lost in the current JavaScript landscape

Grunt - Intermediary files issue

source: http://jaysoo.ca/2014/01/27/gruntjs-vs-gulpjs/

Page 49: How NOT to get lost in the current JavaScript landscape

Fast incremental Builds

gulp.task('scripts', function () { return gulp.src('src/**/*.js') .pipe(cache('scripts')) .pipe(header('(function () {')) .pipe(footer('})();')) .pipe(remember('scripts')) .pipe(concat('app.js')) .pipe(gulp.dest('public/'))});

Page 50: How NOT to get lost in the current JavaScript landscape

Gulp

In the last radar we called out Gulp as a strong competitor to Grunt, with a clean API and fast builds thanks to its streaming approach.

...

We do see some teams successfully using Gulp inside Grunt, when the speed of intermediate result caching is required, but we are not recommending it as the default JavaScript build tool.

Page 51: How NOT to get lost in the current JavaScript landscape

Gulp vs Grunt

- No intermediary files- Fast builds- Code over Configuration- Streaming api

Page 52: How NOT to get lost in the current JavaScript landscape

Structuring your code

Page 53: How NOT to get lost in the current JavaScript landscape

Sock Drawer

+ Good for medium size project

+ Easy to decide where to put each file

+ Well known pattern

Page 54: How NOT to get lost in the current JavaScript landscape

Modular

- easy to identify dependencies from specific domain

Page 55: How NOT to get lost in the current JavaScript landscape

Modular with assets

+ everything in one place- need for advanced code

bundling tools

Page 56: How NOT to get lost in the current JavaScript landscape

Module Pattern

Page 57: How NOT to get lost in the current JavaScript landscape

Namespace pattern

helloWorld.js

var app = app || {};var thing1 = app.thing1;var thing2 = app.thing2;var thing3 = app.thing3;

app.helloWorld = function () { console.log(thing1, thing2, thing3);}

Gruntfile.js

uglify: { dist: { files: { 'dest/output.min.js': [ 'src/thing1.js' 'src/thing2.js' 'src/thing3.js' 'src/helloWorld.js' ]...

Page 58: How NOT to get lost in the current JavaScript landscape

AMD

require([ './thing1', './thing2', './thing3'], function(thing1, thing2, thing3) { // Tell the module what to return/export return function() { console.log(thing1, thing2, thing3); };});

Asynchronous module definition

Page 59: How NOT to get lost in the current JavaScript landscape

CommonJS

var thing1 = require('./thing1');var thing2 = require('./thing2');var thing3 = require('./thing3');

// Tell the module what to return/exportmodule.exports = function() { console.log(thing1, thing2, thing3);};

Page 60: How NOT to get lost in the current JavaScript landscape

- CommonJS modules support

- First step to isomorphic JS

- works with dependency managers like npm

Webpack

Page 61: How NOT to get lost in the current JavaScript landscape

Manage your dependencies

- npm- Bower- WebJars

Page 62: How NOT to get lost in the current JavaScript landscape

npm install calendar-widgetnpm install date-formatternpm install dropdown-widget

Modular payoff

Page 63: How NOT to get lost in the current JavaScript landscape

Asset Bundling Optimize Web Performance

- concat- minify- gzip- cache-busting

Page 64: How NOT to get lost in the current JavaScript landscape

Simple asset bundling with Grunt

● grunt● uglify● gzip● browser cache busting

Page 65: How NOT to get lost in the current JavaScript landscape

Advanced Asset Bundling

Tools that support:- code splitting- CommonJS syntax- pluggable transforms

- e.g. precompiling templates

Page 66: How NOT to get lost in the current JavaScript landscape

● webpack 4922☆● browserify 7274☆● lasso-js 69☆

Advanced Asset Bundlers

Page 67: How NOT to get lost in the current JavaScript landscape

Test runners

● karma● jsdom + mocha● phantomjs + mocha

Page 68: How NOT to get lost in the current JavaScript landscape

Choosing the right tech

For progressive enhancement

Page 69: How NOT to get lost in the current JavaScript landscape

Build your tech incrementally

Avoid Technology Lock-in

Page 70: How NOT to get lost in the current JavaScript landscape

{dust}

Easy Medium Advanced

Page 71: How NOT to get lost in the current JavaScript landscape

Frontend Technology Radar 2015

Page 72: How NOT to get lost in the current JavaScript landscape

Adopt

● HTML5 History Api● PJAX● Grunt/Gulp● CommonJS● Asset Bundlers● karma● AngularJS for SPA

Page 73: How NOT to get lost in the current JavaScript landscape

Consider

● NodeJS as a UI Backend● Isomorphic JS● ReactJS

Page 74: How NOT to get lost in the current JavaScript landscape

Stop

● AMD● JSF● WebJars● GWT

Page 75: How NOT to get lost in the current JavaScript landscape

Q/A