Slide 1

Slide 1 text

Blink and you’ll miss it Building a Progressive Web App with HTTP/2

Slide 2

Slide 2 text

Dean Hume @deanohume

Slide 3

Slide 3 text

I work for a company called Settled

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Instead ➔ Transparent ➔ Online ➔ Everything to help you get “Settled” into your new home

Slide 6

Slide 6 text

Our Customers 56%

Slide 7

Slide 7 text

● Was accessible on any device We needed an experience that:

Slide 8

Slide 8 text

● Was accessible on any device ● Was fast We needed an experience that:

Slide 9

Slide 9 text

● Was accessible on any device ● Was fast ● Works with little or no internet connection We needed an experience that:

Slide 10

Slide 10 text

● Was accessible on any device ● Was fast ● Works with little or no internet connection ● Used one codebase We needed an experience that:

Slide 11

Slide 11 text

● Was accessible on any device ● Was fast ● Works with little or no internet connection ● Used one codebase ● Had an “app-like” feel We needed an experience that:

Slide 12

Slide 12 text

Progressive Web Apps

Slide 13

Slide 13 text

This is our journey

Slide 14

Slide 14 text

This talk 1. Basics of progressive web apps

Slide 15

Slide 15 text

This talk 1. Basics of progressive web apps 2. Fast

Slide 16

Slide 16 text

This talk 1. Basics of progressive web apps 2. Fast 3. Reliable

Slide 17

Slide 17 text

This talk 1. Basics of progressive web apps 2. Fast 3. Reliable 4. Look and feel

Slide 18

Slide 18 text

The basics of Progressive Web Apps

Slide 19

Slide 19 text

These apps aren’t packaged and deployed through stores, they’re just websites that took all the right vitamins. Alex Russell “

Slide 20

Slide 20 text

Building blocks Website

Slide 21

Slide 21 text

Building blocks Website Service Worker

Slide 22

Slide 22 text

Building blocks Website Service Worker Manifest File

Slide 23

Slide 23 text

Building blocks Website HTTPS Service Worker Manifest File

Slide 24

Slide 24 text

Think of your web apps requests as planes taking off. ServiceWorker is the air traffic controller that routes the requests. Jeff Posnick “

Slide 25

Slide 25 text

Behind the scenes

Slide 26

Slide 26 text

With Service Workers

Slide 27

Slide 27 text

With Service Workers

Slide 28

Slide 28 text

With Service Workers

Slide 29

Slide 29 text

With Service Workers

Slide 30

Slide 30 text

Service Workers are the key to unlocking the power

Slide 31

Slide 31 text

The perfect progressive enhancement.

Slide 32

Slide 32 text

HTTPS Only

Slide 33

Slide 33 text

Free SSL

Slide 34

Slide 34 text

Support

Slide 35

Slide 35 text

You are already using them

Slide 36

Slide 36 text

2. Fast

Slide 37

Slide 37 text

Caching

Slide 38

Slide 38 text

Caching HTTP/2

Slide 39

Slide 39 text

Caching HTTP/2 Techniques

Slide 40

Slide 40 text

Caching

Slide 41

Slide 41 text

With Service Workers

Slide 42

Slide 42 text

With Service Workers

Slide 43

Slide 43 text

First Visit

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Register

Slide 47

Slide 47 text

Register Install

Slide 48

Slide 48 text

Register Install Activate

Slide 49

Slide 49 text

const cacheName = ‘pageCache’; Caching

Slide 50

Slide 50 text

const cacheName = ‘pageCache’; this.addEventListener('install', event => { event.waitUntil(caches.open(cacheName) .then(function(cache) { }) ); }); Caching

Slide 51

Slide 51 text

const cacheName = ‘pageCache’; this.addEventListener('install', event => { event.waitUntil(caches.open(cacheName) .then(function(cache) { return cache.addAll(['result.min.css', './js/material.min.js']); }) ); }); Caching

Slide 52

Slide 52 text

const cacheName = ‘pageCache’; this.addEventListener('install', event => { event.waitUntil(caches.open(cacheName) .then(function(cache) { return cache.addAll(['result.min.css', './js/material.min.js']); }) ); }); this.addEventListener('fetch', event => { event.respondWith(caches.match(event.request) .then(function (response) { return response || fetch(event.request); }) ); }); Caching

Slide 53

Slide 53 text

bit.ly/sw-toolbox

Slide 54

Slide 54 text

★ Fresh visit - 2 secs ★ Cached visit - 500 ms ★ Less load on the server

Slide 55

Slide 55 text

HTTP/2

Slide 56

Slide 56 text

source: blog.cloudflare.com

Slide 57

Slide 57 text

source: blog.cloudflare.com

Slide 58

Slide 58 text

HTTPS

Slide 59

Slide 59 text

npm install spdy

Slide 60

Slide 60 text

npm install spdy yarn add

Slide 61

Slide 61 text

const spdy = require('spdy'); const express = require('express'); const app = express(); HTTP/2

Slide 62

Slide 62 text

const spdy = require('spdy'); const express = require('express'); const app = express(); // Any incoming requests app.get('/home', (req, res) => { res.status(200).json({message: 'ok'}); }); HTTP/2

Slide 63

Slide 63 text

const spdy = require('spdy'); const express = require('express'); const app = express(); // Any incoming requests app.get('/home', (req, res) => { res.status(200).json({message: 'ok'}); }); // Create the Server spdy.createServer({ key: fs.readFileSync('./privatekey.key'), cert: fs.readFileSync('./certificate.crt') }, app) .listen(8012, (err) => { if (err) { throw new Error(err); } console.log('Listening on port: 8012.'); }); HTTP/2

Slide 64

Slide 64 text

Client Side

Slide 65

Slide 65 text

Server Side

Slide 66

Slide 66 text

Techniques

Slide 67

Slide 67 text

We used no JavaScript frameworks.

Slide 68

Slide 68 text

We rendered the page server-side.

Slide 69

Slide 69 text

We experimented with HTTP/2 Push.

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

Without H2 Push

Slide 72

Slide 72 text

With H2 Push

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

H2 Push bit.ly/server-push-http2

Slide 75

Slide 75 text

We also used prefetch.

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Slide 79

Slide 79 text

We tested using Lighthouse.

Slide 80

Slide 80 text

Audit your Progressive Web App

Slide 81

Slide 81 text

bit.ly/pwa-lighthouse

Slide 82

Slide 82 text

15 x less data

Slide 83

Slide 83 text

15 x less data 3x faster

Slide 84

Slide 84 text

Caching HTTP/2 Techniques Summary

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Offline

Slide 87

Slide 87 text

With Service Workers

Slide 88

Slide 88 text

this.addEventListener('fetch', event => { };

Slide 89

Slide 89 text

this.addEventListener('fetch', event => { // Check if the user navigated if (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html')) { // Respond appropriately } };

Slide 90

Slide 90 text

this.addEventListener('fetch', event => { // Check if the user navigated if (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html')) { // Respond appropriately event.respondWith( fetch(event.request.url).catch(error => { // Return the offline page return caches.match('the-offline-page'); })); } };

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Network First

Slide 93

Slide 93 text

Offline Applications bit.ly/offline-page

Slide 94

Slide 94 text

Offline Analytics github.com/GoogleChrome/sw-helpers

Slide 95

Slide 95 text

3rd Party Scripts

Slide 96

Slide 96 text

Server Fails

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

Using Service Workers

Slide 99

Slide 99 text

Using Service Workers

Slide 100

Slide 100 text

With Service Workers

Slide 101

Slide 101 text

function timeout(delay) { return new Promise(function(resolve, reject) { setTimeout(function(){ resolve(new Response('', { status: 408, statusText: 'Request timed out.' })); }, delay); }); }

Slide 102

Slide 102 text

function timeout(delay) { return new Promise(function(resolve, reject) { setTimeout(function(){ resolve(new Response('', { status: 408, statusText: 'Request timed out.' })); }, delay); }); } self.addEventListener('fetch', event => { });

Slide 103

Slide 103 text

function timeout(delay) { return new Promise(function(resolve, reject) { setTimeout(function(){ resolve(new Response('', { status: 408, statusText: 'Request timed out.' })); }, delay); }); } self.addEventListener('fetch', event => { event.respondWith( Promise.race([timeout(6000), fetch(event.request.url)])); });

Slide 104

Slide 104 text

bit.ly/sw-toolbox

Slide 105

Slide 105 text

toolbox.router.get('/(.*)', global.toolbox.cacheFirst, { });

Slide 106

Slide 106 text

toolbox.router.get('/(.*)', global.toolbox.cacheFirst, { origin: /\.google\.com$/, });

Slide 107

Slide 107 text

toolbox.router.get('/(.*)', global.toolbox.cacheFirst, { origin: /\.google\.com$/, cache: { name: 'javascript', networkTimeoutSeconds: 4 } });

Slide 108

Slide 108 text

bit.ly/sw-timeout

Slide 109

Slide 109 text

App-like Picture of mobile phones Look & Feel

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

App-like Picture of mobile phones Look & Feel ❖ Manifest file

Slide 112

Slide 112 text

App-like Picture of mobile phones ❖ Manifest file ❖ Add to Home Screen Look & Feel

Slide 113

Slide 113 text

Manifest A simple JSON file that allows you to control how your app appears to the user

Slide 114

Slide 114 text

Look and feel

Slide 115

Slide 115 text

{ lang: "en", background_color: "#09adec", name: "Settled", short_name: "Settled Dashboard", display: "standalone", icons: [ { src: "./images/logo-144.png", sizes: "144x144", type: "image/png" } ], start_url: "/?start=a2hs", orientation: "portrait" }

Slide 116

Slide 116 text

{ lang: "en", background_color: "#09adec", name: "Settled", short_name: "Settled Dashboard", display: "standalone", icons: [ { src: "./images/logo-144.png", sizes: "144x144", type: "image/png" } ], start_url: "/?start=a2hs", orientation: "portrait" }

Slide 117

Slide 117 text

{ lang: "en", background_color: "#09adec", name: "Settled", short_name: "Settled Dashboard", display: "standalone", icons: [ { src: "./images/logo-144.png", sizes: "144x144", type: "image/png" } ], start_url: "/?start=a2hs", orientation: "portrait" }

Slide 118

Slide 118 text

Slide 119

Slide 119 text

Manifest bit.ly/manifest-json

Slide 120

Slide 120 text

Add to home screen

Slide 121

Slide 121 text

Add to home screen 1. You need a manifest.json file

Slide 122

Slide 122 text

Add to home screen 1. You need a manifest.json file 2. Your manifest file needs a start URL

Slide 123

Slide 123 text

Add to home screen 1. You need a manifest.json file 2. Your manifest file needs a start URL 3. You need a 144x144 PNG icon

Slide 124

Slide 124 text

Add to home screen 1. You need a manifest.json file 2. Your manifest file needs a start URL 3. You need a 144x144 PNG icon 4. Your site is using a Service Worker running over HTTPS

Slide 125

Slide 125 text

Add to home screen 1. You need a manifest.json file 2. Your manifest file needs a start URL 3. You need a 144x144 PNG icon 4. Your site is using a Service Worker running over HTTPS 5. The user has visited your site at least twice, with at least five minutes between visits.

Slide 126

Slide 126 text

{ lang: "en", background_color: "#09adec", name: "Settled", short_name: "Settled Dashboard", display: "standalone", icons: [ { src: "./images/logo-144.png", sizes: "144x144", type: "image/png" } ], start_url: "/?start=a2hs", orientation: "portrait" }

Slide 127

Slide 127 text

H S

Slide 128

Slide 128 text

App-like Picture of mobile phones Summary

Slide 129

Slide 129 text

App-like Picture of mobile phones Summary ❖ Fast

Slide 130

Slide 130 text

App-like Picture of mobile phones Summary ❖ Fast ❖ Reliable

Slide 131

Slide 131 text

App-like Picture of mobile phones Summary ❖ Fast ❖ Reliable ❖ Look & Feel

Slide 132

Slide 132 text

What’s still to come?

Slide 133

Slide 133 text

Push Notifications

Slide 134

Slide 134 text

Web Share

Slide 135

Slide 135 text

Web Share bit.ly/webshare-api

Slide 136

Slide 136 text

Brotli

Slide 137

Slide 137 text

We are still learning

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

No content

Slide 140

Slide 140 text

Thank you! @deanohume