Progressive Web Apps

A presentation at Austin JavaScript in August 2016 in Austin, TX, USA by Dave Rupert

Slide 1

Slide 1

Dave Rupert / @davatron5000

Slide 2

Slide 2

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

http://shoptalkshow.com

Slide 6

Slide 6

http://godaytrip.com

Slide 7

Slide 7

Slide 8

Slide 8

How many apps does the average mobile user download per month? Percent drop-off for each stage of your mobile app onboarding? Number of companies in the Top 10 app downloads according to COMSCORE? 0 20% 4

Slide 9

Slide 9

Slide 10

Slide 10

Slide 11

Slide 11

Slide 12

Slide 12

Cats vs. Dogs, Right vs. Left, Pants vs. No pants

Slide 13

Slide 13

Slide 14

Slide 14

We received a lot of feedback like “Where can I download it?” and “If it’s not on my homescreen I forget about it.”

Slide 15

Slide 15

Slide 16

Slide 16

Requirement for an app that takes you deep into the woods, it must work deep in the woods.

Slide 17

Slide 17

Alex Russell, Google TL;DR – “The future of the Web hangs on something I invented.”

Slide 18

Slide 18

A background web worker with a few superpowers.

Slide 19

Slide 19

LEAVE THE MAIN THREAD ALONE

Slide 20

Slide 20

Slide 21

Slide 21

Slide 22

Slide 22

Offline

Slide 23

Slide 23

“I don’t have bars, but if I did, they’d be full bars. Everyone knows I have great bars. Full bars. If I ever had less than full bars, I’d remember. But I don’t recall ever having less than full bars.”

Slide 24

Slide 24

Minimum Viable Product: An Offline Service Worker

Slide 25

Slide 25

Slide 26

Slide 26

Slide 27

Slide 27

Slide 28

Slide 28

manifest.json offline.html service-worker.js (HTTPS too)

Slide 29

Slide 29

manifest.json

Slide 30

Slide 30

<meta name=”theme-color” content=”#3E8792”> <link rel=”manifest” href=”/manifest.json”>

Slide 31

Slide 31

offline.html

Slide 32

Slide 32

service-worker.js

Slide 33

Slide 33

Slide 34

Slide 34

Slide 35

Slide 35

Slide 36

Slide 36

10x Faster Than React, Angular, Ember, and jQuery Combined!

Slide 37

Slide 37

Slide 38

Slide 38

Slide 39

Slide 39

Slide 40

Slide 40

Install Activate Fetch Sync Push

Slide 41

Slide 41

self.addEventListener(‘install’, function( event ) { event.waitUntil( // // Prime the caches // ) });

Slide 42

Slide 42

“Hey event, wait until the stuff in these parenthesis are done before you tell the other events you fired.”

Slide 43

Slide 43

self.addEventListener(‘activate’, function( event ) { event.waitUntil( // // Clear out old caches // ) });

Slide 44

Slide 44

self.addEventListener(‘fetch’, function( event ) { event.respondWith( // // Instead of fetching stuff from the network, // try this stuff instead. // ) });

Slide 45

Slide 45

“Hey (fetch) event, respond with this instead of what you were gonna do.”

Slide 46

Slide 46

Quite literally one of the hardest problems in Computer Science. But at least we get to, you know, use JavaScript to do it.

Slide 47

Slide 47

caches.open( cacheName ) caches.keys() caches.match( request ) caches.delete( key )

Slide 48

Slide 48

~100 Lines of Fun, Stolen from adactio.com/serviceworker.js

Slide 49

Slide 49

var staticCacheName = ‘static’; var version = ‘v1::’;

Slide 50

Slide 50

function updateStaticCache() { return caches.open(version + staticCacheName) .then(function (cache) { return cache.addAll([ ‘/assets/application.css’ ‘/assets/application.js’, ‘/offline’ ]); }); };

Slide 51

Slide 51

self.addEventListener(‘install’, function(event) { event.waitUntil( updateStaticCache() ); });

Slide 52

Slide 52

self.addEventListener(‘activate’, function (event) { event.waitUntil( caches.keys().then(function (keys) { // Remove caches whose name is no longer valid return Promise.all( keys.filter(function (key) { return key.indexOf(version) !== 0; }) .map(function (key) { return caches.delete(key); }) ); })); });

Slide 53

Slide 53

self.addEventListener(‘fetch’, function (event) { // Step 1: Always fetch non-GET requests from the network // Step 2: For TEXT/HTML do this: // a) Try the network first // b) If that fails, fallback to the cache // c) If that doesn’t exist, show the offline page // Step 3: For non-TEXT/HTML (e.g. Images) do this: // a) Try the cache first // b) If that fails, try the network // c) If that fails, hijack the request });

Slide 54

Slide 54

// Step 1: Always fetch non-GET requests from the network var request = event.request; if (request.method !== ‘GET’) { event.respondWith( fetch(request) .catch(function () { return caches.match(‘/offline’); }) ); return; }

Slide 55

Slide 55

// Step 2 For TEXT/HTML do this… if (request.headers.get(‘Accept’).indexOf(‘text/html’) !== -1) { event.respondWith( fetch(request) // Then Stuff // Catch Stuff ); return; }

Slide 56

Slide 56

// Step 2: Then Stuff… .then(function (response) { // Stash a copy of this page in the cache var copy = response.clone(); caches.open(version + staticCacheName) .then(function (cache) { cache.put(request, copy); }); return response; })

Slide 57

Slide 57

// Step 2: Catch Stuff… .catch(function () { return caches.match(request).then(function (response) { return response || caches.match(‘/offline’); }) })

Slide 58

Slide 58

// Step 3: For non-TEXT/HTML (e.g. Images) do this… event.respondWith( caches.match(request).then(function (response) { return response || fetch(request) .catch(function () { // If the request is for an image, show an offline placeholder if (request.headers.get(‘Accept’).indexOf(‘image’) !== -1) { return new Response(‘<svg>…</svg>’, { headers: { ‘Content-Type’: ‘image/svg+xml’ } }); } }); }) );

Slide 59

Slide 59

The Last Step?

Slide 60

Slide 60

if (‘serviceWorker’ in navigator) { navigator.serviceWorker.register( ‘/service-worker.js’, { scope: ‘/’ }).then(function(reg) { console.log(‘Works! Scope is ’ + reg.scope); }).catch(function(error) { console.log(‘Failed with ’ + error); }); }

Slide 61

Slide 61

Slide 62

Slide 62

Slide 63

Slide 63

Browser Cache With Service Worker

Slide 64

Slide 64

Multiple visits

2 minute session time

~5 minutes apart Depends on browser’s own heuristics.

Slide 65

Slide 65

Asset pipelines, digest fingerprinting, and scope issues.

Slide 66

Slide 66

function updateStaticCache() { return caches.open(version + staticCacheName) .then(function (cache) { return cache.addAll([ ‘<%= url_to_stylesheet “application” %>’ ‘<%= url_to_javascript “application” %>’, ‘/offline’ ]); }); };

Slide 67

Slide 67

Nope Yep • assets/ • assets/ • serviceworker.js • logo.png • index.html • offline.html • manifest.json • logo.png • index.html • offline.html • manifest.json • serviceworker.js

Slide 68

Slide 68

• Fixes Scope Issues • Sets up route to Dynamic Service Worker in the root • Sets appropriate Expires headers

Slide 69

Slide 69

The… ahem… intricacies… of building with Service Workers

Slide 70

Slide 70

localhost:4000 localhost:4000

Slide 71

Slide 71

Slide 72

Slide 72

Slide 73

Slide 73

Enterprise Ruby on Rails, Finally!

Slide 74

Slide 74

Slide 75

Slide 75

@davatron5000