Slide 1

Slide 1 text

Production PWAs With JavaScript Frameworks Addy Osmani Eng manager, Web DevRel Abhinav Rastogi Tech Lead, FlipkaD (Desktop)

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Frameworks can be fast if we put the work in.

Slide 4

Slide 4 text

Fast is a product of using your framework well & intelligently loading via your bundler.

Slide 5

Slide 5 text

4.2s Time-to-interactive

Slide 6

Slide 6 text

4.8s Time-to-interactive

Slide 7

Slide 7 text

bit.ly/ng2-weather Interactive in < 5s with AoT

Slide 8

Slide 8 text

bit.ly/vue-hn Interactive in < 5s

Slide 9

Slide 9 text

app coming soon Interactive in < 5s

Slide 10

Slide 10 text

What do we mean by fast?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Time-to-interactive <5s on a real-device over 3G *2s on repeat-load aVer Service Worker registered

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

bit.ly/bundling-study

Slide 16

Slide 16 text

What JS module bundler do you use? 83% webpack

Slide 17

Slide 17 text

Do you use code-splitting to chunk up your JS? 58% yes JS JS JS 10%

Slide 18

Slide 18 text

CONFIGURING WEBPACK

Slide 19

Slide 19 text

tree-shaking 19% Service Worker 11% HTTP/2 14% What other concepts are you taking advantage of?

Slide 20

Slide 20 text

POLYMER SHOP TIME TO INTERACTIVE NEXUS 5X - THROTTLED 3G NEXUS 5X - 3G W/PACKET LOSS 4.3s 5.8s

Slide 21

Slide 21 text

FLIPKART NEXUS 5X - THROTTLED 3G NEXUS 5X - 3G W/PACKET LOSS 4.5s 6.9s HOUSING.COM TIME TO INTERACTIVE

Slide 22

Slide 22 text

NEXUS 5X - THROTTLED 3G NEXUS 5X - 3G W/PACKET LOSS TIME TO INTERACTIVE (MEDIAN) 11s 12s * high of 24s. FVL in ~17s

Slide 23

Slide 23 text

550ms script eval 270ms eval 524ms eval code for view 140ms eval cookie handling ~500KB script Interactive in 13 seconds ~3s, 249KB common vendor chunk ~5s load JS for view 8s parse/eval

Slide 24

Slide 24 text

Try not to keep the main thread busy. Larger bundles impact interactivity & take longer to load, parse and execute.

Slide 25

Slide 25 text

HTTPArchive average script size per page is 408KB gzipped 1MB script (250KB minified) JS Parse Time On Mobile

Slide 26

Slide 26 text

Test on real phones & real networks. There’s no substitute.

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Less code, loaded becer improves ped. There is however, nuance.

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

CODE-SPLITTING minimal functional code for a route

Slide 32

Slide 32 text

System.import(‘./UserProfile’) .then(loadRoute(cb)) .catch(errorLoading); CODE-SPLITTING & ASYNC LOADING WEBPACK 1 WEBPACK 2 bit.ly/code-splicing-webpack2 bit.ly/code-splicing // Defines a “split-point” for a separate bundle require.ensure([], () => { const profile = require(‘./UserProfile’, cb); });

Slide 33

Slide 33 text

System.import(‘./UserProfile’) .then(loadRoute(cb)) .catch(errorLoading); CODE-SPLITTING & ASYNC LOADING WEBPACK 1 WEBPACK 2 bit.ly/code-splicing-webpack2 bit.ly/code-splicing // Defines a “split-point” for a separate bundle require.ensure([], () => { const profile = require(‘./UserProfile’, cb); }); // Request when you require() const waitForChunk = require(‘bundle!./UserProfile.js’); waitForChunk(file => { // use file like is was require()'d }); BUNDLE-LOADER CALLS REQUIRE.ENSURE FOR YOU

Slide 34

Slide 34 text

System.import(‘./UserProfile’) .then(loadRoute(cb)) .catch(errorLoading); CODE-SPLITTING & ASYNC LOADING WEBPACK 1 WEBPACK 2 bit.ly/code-splicing-webpack2 bit.ly/code-splicing // Defines a “split-point” for a separate bundle require.ensure([], () => { const profile = require(‘./UserProfile’, cb); }); // Request when you require() const waitForChunk = require(‘bundle!./UserProfile.js’); waitForChunk(file => { // use file like is was require()'d }); BUNDLE-LOADER CALLS REQUIRE.ENSURE FOR YOU { require.ensure([], require => { callback(null, require('./UserProfile')) }) }} /> DECLARATIVE CODE-SPLITTING WITH REACT ROUTER

Slide 35

Slide 35 text

COMMON LIBRARY CHUNKS

Slide 36

Slide 36 text

const CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin"); module.exports = { entry: { p1: "./route-1", p2: "./route-2", p3: "./route-3" }, output: { filename: "[name].entry.chunk.js" }, plugins: [ new CommonsChunkPlugin("commons.chunk.js") ] } COMMON/VENDOR LIBRARY CHUNK SPLITTING

Slide 37

Slide 37 text

THE PRPL PATTERN bit.ly/prpl-pacern

Slide 38

Slide 38 text

require.ensure() async getComponent() React Router link rel=“preload” or HTTP/2 Push PRPL WITH WEBPACK bit.ly/prpl-webpack

Slide 39

Slide 39 text

LINK REL=PRELOAD use with asset-webpack-plugin bit.ly/link-preload * For most apps using preload over H/2 Server Push will lead to better wins.

Slide 40

Slide 40 text

module.exports = { entry: "./example", output: { path: path.join(__dirname, "js"), filename: "[chunkhash].js", chunkFilename: "[chunkhash].js" }, plugins: [ new webpack.optimize.AggressiveSplittingPlugin({ minSize: 30000, maxSize: 50000 }), // ... HTTP/2 with the AggressiveSplittingPlugin bit.ly/wepback-hcp2

Slide 41

Slide 41 text

Code-splitting itself is not a panacea 9.8s TTI average

Slide 42

Slide 42 text

#WhatsInYourBundle

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

bit.ly/webpack-ped-preview

Slide 48

Slide 48 text

3KB alternative to React with same ES6 API Interactive in <5s on real devices over 3G

Slide 49

Slide 49 text

bit.ly/source-map-explorer Before (with React)

Slide 50

Slide 50 text

bit.ly/source-map-explorer AVer (with Preact)

Slide 51

Slide 51 text

bit.ly/source-map-explorer { resolve: { alias: { 'react': 'preact-compat', 'react-dom': 'preact-compat' } } } SETUP: USE THE PREACT-COMPAT ALIAS IN WEBPACK

Slide 52

Slide 52 text

Layer your app so the network is an enhancement.

Slide 53

Slide 53 text

INSTANT LOADING ON REPEAT. FASTER TTI FOR SUBSEQUENT VISITS.

Slide 54

Slide 54 text

var CACHE_NAME = 'my-pwa-cache-v1'; var urlsToCache = [ '/', '/styles/styles.css', '/script/webpack-bundle.js' ]; self.addEventListener('install', function(event) { event.waitUntil( caches.open(CACHE_NAME) .then(function(cache) { // Open a cache and cache files return cache.addAll(urlsToCache); }) ); }); SERVICE WORKER

Slide 55

Slide 55 text

APPLICATION SHELL ARCHITECTURE

Slide 56

Slide 56 text

plugins: [ new SWPrecacheWebpackPlugin( { cacheId: "my-app", filename: "my-service-worker.js", staticFileGlobs: [ "app/css/**.css", "app/**.html", "app/js/**.js", "app/images/**.*" ], verbose: true } )] SW-PRECACHE WEBPACK PLUGIN bit.ly/wepback-precache

Slide 57

Slide 57 text

HOUSING.COM FLIPKART Oline loading states

Slide 58

Slide 58 text

SuppoD all target users using progressive enhancement.

Slide 59

Slide 59 text

UNIVERSAL JS RENDERING & DATA-FLOW ReactDOMServer.renderToString() ComponentWillMount() Async data-flow with React Router React Resolver

Slide 60

Slide 60 text

Universal JS has issues. Makes it easy to get stuck in uncanny valley renderToString() is synchronous so TTFB is long Streaming server rendered React can get a better TTBH. See react-dom-stream. renderToString() can monopolize the CPU, waste it re-rendering components for each page request. Bottleneck for pages w/lots of VDOM nodes Component memoization helps. See react-ssr-optimization.

Slide 61

Slide 61 text

HIGH-PERFORMANCE REACT PWAs bit.ly/pwa-react

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

Building Flipkart.com Abhinav Rastogi Tech Lead, Web Team @_abhinavrastogi

Slide 64

Slide 64 text

Intro to Flipkart India’s largest e-commerce site and a first-class PWA across all form factors and browsers!

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

Mobile —> desktop

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

React Flux/Redux React-Router Webpack Express Node Fetch Promises Babel Handlebars PM2 Karma

Slide 69

Slide 69 text

Architecture Both mobile and desktop sites follow a similar architecture at the high level.

Slide 70

Slide 70 text

Similarities • Route-based code splitting • Smart pre-loading of chunks and PRPL • Partial server-rendering • Service Workers

Slide 71

Slide 71 text

Significant differences: Mobile vs. Desktop Requirements User Behaviour Network Conditions Device Capabilities Browser Fragmentation

Slide 72

Slide 72 text

Flipkart on Mobile • Build-time rendering • App shells • SW caches shell, offline-first • Composition of multiple SPAs

Slide 73

Slide 73 text

Flipkart on Desktop • Partial server-side rendering • No app shells • Chunked response for the first request, allowing faster TTFP • SW used for caching data and resources

Slide 74

Slide 74 text

What it looks like - Code Splitting

Slide 75

Slide 75 text

What it looks like - PRPL

Slide 76

Slide 76 text

Time to Interactive Search is available in the first render and works without JS!

Slide 77

Slide 77 text

Major Wins • Route-based code-splitting amortizes the high cost for the first visit over the session • Smart preloading of chunks and PRPL makes the experience seam-less • Chunked-encoding allows us to download JS chunks while HTML is still being parsed • Based on UX requirement, solved repeat visits for mobile, first visit for desktop

Slide 78

Slide 78 text

Impact • Business: Upto 2x conversion during sale events • Business: Significantly reduced bounce rate • SEO: 50% reduction in time taken by Google Search bots to crawl a page • SEO: 50% increase in number of pages indexed by Google Search • DevOps: Massive 70% reduction in desktop website tickets, lesser errors

Slide 79

Slide 79 text

Gotchas • CORS in Webpack & route-based code-splitting • Cache-invalidation & the Webpack Manifest

Slide 80

Slide 80 text

What’s Next

Slide 81

Slide 81 text

Thank You! +Abhinav Rastogi @_abhinavrastogi @flipkart_tech

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

code.nasa.gov old Angular 1 site

Slide 85

Slide 85 text

investigated ped issues

Slide 86

Slide 86 text

bit.ly/nasa-ped-audit

Slide 87

Slide 87 text

Best Practices Should Be Automated PRPL

Slide 88

Slide 88 text

PRPL with code-splitting Lazy-loading fragments Offline Caching with Service Worker HTTP/2 + Server Push bundling Polymer App Toolbox

Slide 89

Slide 89 text

Open Source Software Contributed by

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

code.nasa.gov Implemented in <1 week 90+ score on Lighthouse Open Source and on GitHub now

Slide 92

Slide 92 text

github.com/nasa/code-nasa-gov

Slide 93

Slide 93 text

Frameworks can cross the mnish line on mobile ped * if we put the work in

Slide 94

Slide 94 text

Let’s ped the web forward together.

Slide 95

Slide 95 text

Thank You! +AddyOsmani @addyosmani