my incredible journey - goto conference · angular, ember and react, perform well in the mobile...

90

Upload: others

Post on 24-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 2: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Service Worker technology is going to be one of the key technologies to make apps built with web frameworks, like Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app.

Let's take a tour through all the new exciting and ambitious Service Worker solutions that web frameworks are coming up with to work away their deficiencies.

Making the web framework titans feel tiny

Page 3: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

My Incredible Journey

Page 4: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 5: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Waiting sucks

Page 6: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Perception

Page 7: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Marten Schilstra@martndemus

Page 8: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 9: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 10: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Single Page Apps

Page 11: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Single Page Apps<!DOCTYPE html><html> <head> <link rel="stylesheet" href="style.css" /> </head>

<body> <script src="app.js"></script> </body></html>

Page 12: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

0 1 2 3 4 5

Page 13: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

WebPageTest.org

Page 14: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Speed

Page 15: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Full load on desktop:2 - 3s first visit

0.5 - 1.5s repeat visit

Page 16: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Full load on mobile:3 - 5s first visit

1 - 2s repeat visit

Optimistic

Page 17: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Full load on mobile:5 - 8s first visit

2 - 4s repeat visit

Realistic

Page 18: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Full load on mobile:8 - 12s first visit

4 - 6s repeat visit

Bad Connection

Page 19: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Full load on mobile:15 - 25s first visit

8 - 12s repeat visit

Bad Connection

Crappy Phone

Page 20: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Between 500KB and 1MBApp Size

Page 21: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

App Size

Images100KB

JSON10KB

Fonts50KB

JS500KB

CSS30KB

HTML2KB

Page 22: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

App Size

Page 23: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

First Frame

Page 24: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

0 1 2 3 4 5

Page 25: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

First Frame

0 1 2 3 4 5

Page 26: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Demo App

Page 27: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 28: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

First Frame

0 1 2 3 4 5

Page 29: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

First Frame

0 1 2 3 4 5

Page 30: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

App Shell

Page 31: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

App Shell - Step 1<body> <nav> <h1>My App</h1> <ul> <li><a href="/link-1">Page 1</a></li> <li><a href="/link-2">Page 2</a></li> </ul> </nav>

<main><!-- content goes here --></main> <footer>© 2017 My awesome company</footer>

<script src="app.js"></script></body>

Page 32: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 33: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Before After

Page 34: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

First Frame

0 1 2 3 4 5

Page 35: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

First Frame

0 1 2 3 4 5

Page 36: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

App Shell - Step 2<head> <style> body { /* ... */ } nav { /* ... */ } main { /* ... */ } footer { /* ... */ } </style>

<link rel="preload" href="style.css" as="style" onload="this.rel='stylesheet'" /></head>

Page 37: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

<link rel="preload" href="style.css" as="style" />

Page 38: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 39: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 40: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Before After

Page 41: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

First Frame

0 1 2 3 4 5

Page 42: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

First Frame

0 1 2 3 4 5

Page 43: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Returning visitors

Page 44: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 45: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

0 1 2 3 4 5

Page 46: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

Page 47: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

First Frame

Page 48: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

First Frame

Page 49: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

What's happening?

Page 50: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

Evaluating Script

Page 51: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

<!DOCTYPE html><html> <head> <link rel="preload" href="style.css" as="style" onload="this.rel='stylesheet'" /> </head>

<body> <script src="app.js"></script> </body></html>

Page 52: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

<!DOCTYPE html><html> <head> <link rel="preload" href="app.js" as="script" /> <link rel="preload" href="style.css" as="style" onload="this.rel='stylesheet'" /> </head>

<body> <script> window.addEventListener('load', function(event) { let script = document.createElement('script'); script.src = 'app.js'; document.head.appendChild(script); }); </script> </body></html>

Page 53: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Before After

Page 54: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

First Frame

Page 55: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

First Frame

Page 56: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

Page 57: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

304 Not Modified

Page 58: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

cache-control: max-age=31536000

Page 59: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Service Worker

Page 60: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

<!DOCTYPE html><html> <head> <link rel="preload" href="app.js" as="script" /> <link rel="preload" href="style.css" as="style" onload="this.rel='stylesheet'" /> </head>

<body> <script> window.addEventListener('load', function(event) { let script = document.createElement('script'); script.src = 'app.js'; document.head.appendChild(script); }); if ('serviceWorker' in navigator) { navigator.serviceWorker.register('sw.js'); } </script> </body></html>

Page 61: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

const cacheName = 'myapp-static-cache';const files = ['/', '/style.css', '/app.js'];

self.addEventListener('install', (event) => { event.waitUntil( caches .open(cacheName) .then((cache) => cache.addAll(files)) );});

self.addEventListener('fetch', (event) => { event.respondWith(caches.match(event.request));});

Page 62: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Before After

Page 63: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

Page 64: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

Page 65: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Are we offline yet?

Page 66: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 67: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Cache First

Page 68: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

self.addEventListener('fetch', (event) => { event.respondWith(caches.match(event.request));});

Page 69: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

self.addEventListener('fetch', (event) => { event.respondWith( caches.match(event.request) .then((response) => { if (response) { return response; }

return fetch(event.request).then((response) => { cache.put(request, response.clone()); return response; }); }) );});

Page 70: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Are we offline yet?

Page 71: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 72: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Cache Fallback

Page 73: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

self.addEventListener('fetch', (event) => { event.respondWith( caches.open(cacheName) .then((cache) => { return fetch(request) .then((response) => { cache.put(request, response.clone()) return response; }) .catch(() => cache.match(request)); }); );});

Page 74: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Are we offline yet?

Page 75: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 76: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

LocalStorage

Page 77: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

let items = [];let cachedResponse = localStorage.getItem('/api/items');

if (cachedResponse) { let json = JSON.parse(cachedResponse); items = json.items;}

fetch('/api/items') .then((response) => response.json()) .then((json) => { localstorage.setItem('/api/items', JSON.stringify(json));

json.items.forEach((item) => { let index = items.indexOf(item);

if (index !== -1) { items.splice(index, 1, item); } else { items.push(item); } }); });

Page 78: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

IndexedDB

Page 79: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

let items = [];let fetchFinished = false;let dbPromise = IDB.open('items', 1, (db) => db.createObjectStore('items', { keyPath: 'id' });

dbPromise.then((db) => { let transaction = db.transaction('items'); let store = transaction.objectStore('items');

store.getAll().then((results) => { if (!fetchFinished) { items = results.data; } });});

fetch('/api/items') .then((response) => response.json()) .then((json) => { dbPromise.then((db) => { let transaction = db.transaction('items', 'readwrite'); let store = transaction.objectStore('items');

json.items.forEach((item) => store.put(item)); });

json.items.forEach((item) => { let index = items.indexOf(item);

if (index !== -1) { items.splice(index, 1, item); } else { items.push(item); } }); fetchFinished = true; });

Page 80: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

Page 81: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Loading Timeline

0 1 2 3 4 5

Page 82: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Baseline Unblocked JS

SW - Assets cached SW - Fully offline

Page 83: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a
Page 84: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

htmlcss

js

Loading Timeline

imagesjson

0 1 2 3 4 5

Page 85: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

PRPL

Page 86: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Push, Render, Pre-Cache, Lazy-Load

Page 87: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Push the minimal to render

Page 88: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Pre-cache often used modules

Page 89: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Lazy-load other modules

Page 90: My Incredible Journey - GOTO Conference · Angular, Ember and React, perform well in the mobile space, without resorting to packaging that app as a native mobile app. Let's take a

Marten Schilstra@martndemus

Thanks!