Top Banner
Progressive Web Apps What, why and h ow rizafahmi.com
66

Progressive Web Apps. What, why and how

Jan 22, 2018

Download

Technology

Riza Fahmi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Progressive Web Apps. What, why and how

Progressive Web Apps

What, why and how

rizafahmi.com

Page 2: Progressive Web Apps. What, why and how

Progressive Web Apps

What is

Page 3: Progressive Web Apps. What, why and how

We already Did it!Believe it or not

Responsive, mobile first layout Cache, Localstorage, etc.

Page 4: Progressive Web Apps. What, why and how
Page 5: Progressive Web Apps. What, why and how

HTML5 is a new hotness

2009

HTML5

Page 6: Progressive Web Apps. What, why and how

HTML5

2009 2012

Facebook Hybrid App

HTML5 is a new hotness

Page 7: Progressive Web Apps. What, why and how

HTML5 is a new Notness

Page 8: Progressive Web Apps. What, why and how

HTML5 is a new Notness

"The biggest mistake we made as a company was be!ng too much on HTML5

as opposed to na"ve.”

This guyFrom Facebook

Page 9: Progressive Web Apps. What, why and how

Native Apps Rules

HTML5

20122009

Facebook Hybrid App

Mobile apps rules!

Page 10: Progressive Web Apps. What, why and how

Native Advantages

✓ Great performance

✓ Smooth anima!ons & interac!ons

✓ Instant loading & offline support

✓ Easy access through home screen

Page 11: Progressive Web Apps. What, why and how

x Distribu=on problems

x Upda=ng is a pain

x Extra care with low memory phone

x Applica=on size

Native disadvantages

Page 12: Progressive Web Apps. What, why and how

Web StrikeS Back!

HTML5

20122009

Facebook Hybrid App

Mobile apps rules!

2016

Web strikes back

Page 13: Progressive Web Apps. What, why and how

Best of Both WorldsProgressive Web Apps

Page 14: Progressive Web Apps. What, why and how

What Progressive Web Apps Is

Engaging

App-like Linkable

Reliable Fast

Secure

Page 15: Progressive Web Apps. What, why and how

Case Studies

63% users on 2G

70% increase on conversion

Page 16: Progressive Web Apps. What, why and how

Case Studies

75% increase in Tweet Sent

65% increase in pages per session

20% decrease in bounce rate

Page 17: Progressive Web Apps. What, why and how

Case Studies

104% higher conversion rate

2x page visit

74% increase "me spent

Page 18: Progressive Web Apps. What, why and how

Tech BehindProgressive Web Apps

Page 19: Progressive Web Apps. What, why and how

Service Woker App Manifest

Page 20: Progressive Web Apps. What, why and how

How ToProgressive Web Apps

Page 21: Progressive Web Apps. What, why and how

Our Project✓ Single page app for local meetup videos

✓ Offline support via browser caches

✓ Modern, ES6 JavaScript syntax, no framework

✓ Mul"pla$orm, Android and iOS

✓ Na"ve app look & feel

✓ Fullscreen app

✓ Splash screen

✓ Home screen icon

Engineers.id

Page 22: Progressive Web Apps. What, why and how

Our Plan✓ Design App Shell

✓ Ge!ng The Data from API

✓ Using Service Worker:

✓ Caching App Shell

✓ Caching Data

Engineers.id

✓ Na"ve-like apps:

✓ Standalone app

✓ Adding App Icons

✓ Adding Splas Screen

✓ Deploy and securing our

app

Page 23: Progressive Web Apps. What, why and how

ToolsChrome DevTools - Device Mode✓Simulate device web experiences ✓Responsive breakpoint visualiza"on ✓First meaningful paint, metrics, etc ✓Service worker, manifest, etc

Page 24: Progressive Web Apps. What, why and how

ToolsiOS Simulator✓ localhost is your machine ✓Cri"cal for tes"ng apple-specific features ✓Connect to desktop’s safari for debugging

Page 25: Progressive Web Apps. What, why and how

ToolsAndroid Emulator✓10.0.2.2 is your machine ✓Connect to desktop’s chrome for debugging

Page 26: Progressive Web Apps. What, why and how

ToolsLighthouse✓Chrome extension or CLI ✓Checklist for our progressive enhancements ✓Performance sugges"ons

Page 27: Progressive Web Apps. What, why and how

Designing The App Shell

Content

Applica"on Shell

Step 1

Page 28: Progressive Web Apps. What, why and how

<!DOCTYPE html> <html> <head> <meta charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="x-ua-compatible" content="ie-edge"> <meta name="description" content="Best video resources for learn programming"> <link rel="stylesheet" href=“styles/main.css" type="text/css" media="screen" charset="utf-8"> <link rel="stylesheet" href="styles/spinner.css" type="text/css" media="screen" charset="utf-8"> <title>Engieers.id </title> </head>

Page 29: Progressive Web Apps. What, why and how

<body> <nav> <div class="left"> <a href="/"> <h1>Engineers.id </h1> </a> </div> <div class="right"> <button onclick="location.reload()" class="btn card__btn">Refresh </button> </div> </nav> <div class=“spinner”> ... </div> <ul class="cards"> </ul>

<script id="card" type="text/template"> <li class="cards__item"> </li> </script>

</body>

Page 30: Progressive Web Apps. What, why and how

Test it out!

Page 31: Progressive Web Apps. What, why and how

Get The DataStep 2

// scripts/app.js const url = 'https: //engineers-id-backend-ybbwzovhnl.now.sh/api/videos'

fetch(url) .then(response => response.json()) .then(json => { appendData(json) })

Page 32: Progressive Web Apps. What, why and how

Test it out!

Page 33: Progressive Web Apps. What, why and how

fetch Polyfill <!-- index.html --> <html> <head> ... </head> <body> <!-- ... --> <script src="scripts/vendors/fetch.js"> </script> <script src="scripts/app.js"> </script> </body> </html>

Page 34: Progressive Web Apps. What, why and how

Test it out!

Page 35: Progressive Web Apps. What, why and how

Zoom it out!

Naviga"on begins(17ms)

First conten$ul paint (600ms)

First meaningful paint (1.58s)

Time to interac"ve (1.8s)

Page 36: Progressive Web Apps. What, why and how

Service Worker

Not really a new technology

Parallel, not just concurrent

Independent script

Sends message to origin

Page 37: Progressive Web Apps. What, why and how

Register Service WorkerStep 3

// scripts/app.js if ('serviceWorker' in navigator) { navigator.serviceWorker.register('./service-worker.js').then(function () { console.log('Service Worker Registered') }) }

Page 38: Progressive Web Apps. What, why and how

Test it out!

Page 39: Progressive Web Apps. What, why and how

Test it out!

Page 40: Progressive Web Apps. What, why and how

Cache The App ShellStep 4

// service-worker.js var cacheName = ‘engineers-id’ var filesToCache = [ '/', '/index.html', '/scripts/app.js', ‘/styles/main.css', '/styles/spinner.css', ‘/images/logo.svg', // ... ] self.addEventListener('install', function (e) { e.waitUntil( caches.open(cacheName).then(function (cache) { return cache.addAll(filesToCache) }) ) })

Page 41: Progressive Web Apps. What, why and how

Cache The App ShellStep 4

Page 42: Progressive Web Apps. What, why and how

Wait, How Service Worker actually work?!Service worker

Installing Ac"vated

Error

Idle Push

Fetch

Terminated

Web page

Caching App Shell

Page 43: Progressive Web Apps. What, why and how

Cache The ContentStep 5

Page 44: Progressive Web Apps. What, why and how

Caching Strategy - Cache Only

//service-worker.js self.addEventListener('fetch', function (event) { // If a match isn't found in the cache, the response // will look like a connection error event.respondWith(caches.match(event.request)) })

Page 45: Progressive Web Apps. What, why and how

Caching Strategy - Network Only

self.addEventListener('fetch', function (event) { event.respondWith(fetch(event.request)) // or simply don't call event.respondWith, which // will result in default browser behaviour })

Page 46: Progressive Web Apps. What, why and how

Caching Strategy - Cache, failing back to network

//service-worker.js self.addEventListener('fetch', function (event) { event.respondWith( caches.match(event.request).then(function (response) { return response || fetch(event.request) }) ) })

Page 47: Progressive Web Apps. What, why and how

Caching Strategy - Cache, NetworK Race // service-worker.js function promiseAny (promises) { return new Promise((resolve, reject) => { promises = promises.map(p => Promise.resolve(p)) promises.forEach(p => p.then(resolve)) promises .reduce((a, b) => a.catch(() => b)) .catch(() => reject(Error('All failed'))) }) }

self.addEventListener('fetch', function (event) { event.respondWith( promiseAny([caches.match(event.request), fetch(event.request)]) ) })

Page 48: Progressive Web Apps. What, why and how

Caching Strategy - Cache Then Network

// service-worker.js self.addEventListener('fetch', function (event) { event.respondWith( caches.open('mysite-dynamic').then(function (cache) { return fetch(event.request).then(function (response) { cache.put(event.request, response.clone()) return response }) }) ) })

Page 49: Progressive Web Apps. What, why and how

Caching Strategy - Network Falling Back To Cache

self.addEventListener('fetch', function (event) { event.respondWith( fetch(event.request).catch(function () { return caches.match(event.request) }) ) })

Page 50: Progressive Web Apps. What, why and how

Cache The ContentStep 5

// service-worker.js self.addEventListener('fetch', e => { e.respondWith( caches.open(dataCacheName).then(cache => { return fetch(e.request) .then(networkResponse => { cache.put(e.request, networkResponse.clone()) return networkResponse }) .catch(() => { return caches.match(e.request) }) }) ) })

Page 51: Progressive Web Apps. What, why and how

When Our Users Going Rogue...

Page 52: Progressive Web Apps. What, why and how

Recap

Page 53: Progressive Web Apps. What, why and how

App Manifest

Page 54: Progressive Web Apps. What, why and how

App Manifest // manifest.json { "name": "Engineers.id", "short_name": "eng.id", "lang": "en-US", "start_url": "/index.html", "display": "standalone", "theme_color": "#fff", "icons": [ { "src": "images/icons/icon-128x128.png", "sizes": "128x128", "type": "image/png" }, ...

], "background_color": “#fff”, “orientation”: “portrait” }

Page 55: Progressive Web Apps. What, why and how

App Manifest - Display Mode‘fullscreen’

‘standalone’

‘minimal-ui’

‘browser’

Page 56: Progressive Web Apps. What, why and how

App Manifest - Icons144px by 144px

128px by 128px

192px by 192px

256px by 256px

512px by 512px

Page 57: Progressive Web Apps. What, why and how

App Manifest - Home Screen Icons

48 dp icons for home screen and task switcher

144px by 144px

192px by 192px

48px by 48px

96px by96px

Page 58: Progressive Web Apps. What, why and how

App Manifest - Splash Screen Icons

128 dp icons for splash screen

128px by 128px

256px by 256px

512px by 512px

{ "name": "Engineers.id", "short_name": "eng.id", "lang": "en-US", "start_url": "/index.html", "display": "standalone", "theme_color": "#fff", "icons": [], "background_color": “#fff”, “orientation”: “portrait” }

Page 59: Progressive Web Apps. What, why and how

App Manifest <!-- index.html --> <link rel="manifest" href="manifest.json">

Page 60: Progressive Web Apps. What, why and how

Install Banners

{ "name": "Engineers.id", "short_name": "eng.id", "lang": "en-US", "start_url": "/index.html", "display": "standalone", "theme_color": "#fff", "icons": [

... {

"src": "images/icons/icon-144x144.png", "sizes": "144x144", "type": "image/png" }, ],

"background_color": “#fff”, “orientation”: “portrait” }

Page 61: Progressive Web Apps. What, why and how

Test It Out!

61

Page 62: Progressive Web Apps. What, why and how

Test It Out!

62

Page 63: Progressive Web Apps. What, why and how

One last thing: Deploy h%ps://zeit.co/now

Page 64: Progressive Web Apps. What, why and how

Recap!

Page 65: Progressive Web Apps. What, why and how

More.. More..PaymentPush No"fica"on Connect to Hardware

Page 66: Progressive Web Apps. What, why and how

THOUGHTS??!

facebook.com/rizafahmi

twi%er.com/rizafahmi22 linkedin.com/in/rizafahmi

github.com/rizafahmi

slideshare.com/rizafahmi

hack"v8.comrizafahmi.com