79
THE HITCHHIKER’S GUIDE TO BUILDING A PROGRESSIVE WEB APP Chris Nguyen @uncompiled

The Hitchhiker's Guide to Building a Progressive Web App

Embed Size (px)

Citation preview

Page 1: The Hitchhiker's Guide to Building a Progressive Web App

THE HITCHHIKER’S GUIDE TOBUILDING A PROGRESSIVE WEB APP

Chris Nguyen@uncompiled

Page 2: The Hitchhiker's Guide to Building a Progressive Web App
Page 3: The Hitchhiker's Guide to Building a Progressive Web App

https://pwa.rocks

Page 4: The Hitchhiker's Guide to Building a Progressive Web App

SOURCE: https://pokedex.org

POKEDEX.ORG

Page 5: The Hitchhiker's Guide to Building a Progressive Web App
Page 6: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

● Progressive

● Responsive

● Linkable

● Fresh

● App-like

● Safe

● Discoverable

● Installable

● Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 7: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVEENHANCEMENT

Page 8: The Hitchhiker's Guide to Building a Progressive Web App
Page 9: The Hitchhiker's Guide to Building a Progressive Web App
Page 10: The Hitchhiker's Guide to Building a Progressive Web App
Page 11: The Hitchhiker's Guide to Building a Progressive Web App
Page 12: The Hitchhiker's Guide to Building a Progressive Web App
Page 13: The Hitchhiker's Guide to Building a Progressive Web App
Page 14: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

✓ Progressive

● Responsive

● Linkable

● Fresh

● App-like

● Safe

● Discoverable

● Installable

● Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 15: The Hitchhiker's Guide to Building a Progressive Web App

RESPONSIVEWEB DESIGN

Page 16: The Hitchhiker's Guide to Building a Progressive Web App
Page 17: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

✓ Progressive

✓ Responsive

● Linkable

● Fresh

● App-like

● Safe

● Discoverable

● Installable

● Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 18: The Hitchhiker's Guide to Building a Progressive Web App

LINKABLE

Page 19: The Hitchhiker's Guide to Building a Progressive Web App

@slightlylate: "URLs are the web's superpower." #chromedevsummit

Page 20: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

✓ Progressive

✓ Responsive

✓ Linkable

● Fresh

● App-like

● Safe

● Discoverable

● Installable

● Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 21: The Hitchhiker's Guide to Building a Progressive Web App

FRESH

CREDIT: http://www.slashfilm.com

Page 22: The Hitchhiker's Guide to Building a Progressive Web App

CREDIT: Android Authority

Page 23: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

✓ Progressive

✓ Responsive

✓ Linkable

✓ Fresh

● App-like

● Safe

● Discoverable

● Installable

● Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 24: The Hitchhiker's Guide to Building a Progressive Web App

APP-LIKE

Page 25: The Hitchhiker's Guide to Building a Progressive Web App

APP-LIKE

● Familiar interface

● Fast screen transitions

● Smooth scrolling

● Feels native

SOURCE: https://app.ft.com

Page 26: The Hitchhiker's Guide to Building a Progressive Web App
Page 27: The Hitchhiker's Guide to Building a Progressive Web App
Page 28: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

✓ Progressive

✓ Responsive

✓ Linkable

✓ Fresh

✓ App-like

● Safe

● Discoverable

● Installable

● Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 29: The Hitchhiker's Guide to Building a Progressive Web App

SAFE

Page 30: The Hitchhiker's Guide to Building a Progressive Web App

SAFE

● Must be served using HTTPS

● Prevents snooping or MITM attacks

● Privacy and security for users

Page 31: The Hitchhiker's Guide to Building a Progressive Web App
Page 32: The Hitchhiker's Guide to Building a Progressive Web App

White House OMB Memorandum 15-13

SOURCE: https://https.cio.gov

Page 33: The Hitchhiker's Guide to Building a Progressive Web App

White House OMB Memorandum 15-13

SOURCE: https://https.cio.gov

Page 34: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

✓ Progressive

✓ Responsive

✓ Linkable

✓ Fresh

✓ App-like

✓ Safe

● Discoverable

● Installable

● Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 35: The Hitchhiker's Guide to Building a Progressive Web App

DISCOVERABLE

Page 36: The Hitchhiker's Guide to Building a Progressive Web App

WEB APPLICATION MANIFEST

{ "name": "My Web Application", "short_name": "My App", "description": "A Simple Progressive Web App", "icons": [ ... { "src": "launcher-icon-4x.png", "sizes": "192x192", "type": "image/png" } ], "start_url": "/index.html", "display": "standalone", "orientation": "portrait"}

<link rel="manifest" href="/manifest.json">

Page 37: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

✓ Progressive

✓ Responsive

✓ Linkable

✓ Fresh

✓ App-like

✓ Safe

✓ Discoverable

● Installable

● Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 38: The Hitchhiker's Guide to Building a Progressive Web App

INSTALLABLE

CREDIT: https://xkcd.com/1367/

Page 39: The Hitchhiker's Guide to Building a Progressive Web App

INSTALLABLE

● Multiple visitstrigger prompt

● Low friction

● No app store

SOURCE: https://www.devfestdc.rocks

Page 40: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

✓ Progressive

✓ Responsive

✓ Linkable

✓ Fresh

✓ App-like

✓ Safe

✓ Discoverable

✓ Installable

● Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 41: The Hitchhiker's Guide to Building a Progressive Web App

RE-ENGAGEABLE

Page 42: The Hitchhiker's Guide to Building a Progressive Web App
Page 43: The Hitchhiker's Guide to Building a Progressive Web App

https://gauntface.github.io/simple-push-demo/

Page 44: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

✓ Progressive

✓ Responsive

✓ Linkable

✓ Fresh

✓ App-like

✓ Safe

✓ Discoverable

✓ Installable

✓ Re-engageable

● Connectivity independent

SOURCE: https://developers.google.com/web/fundamentals/getting-started/your-first-progressive-web-app/

Page 45: The Hitchhiker's Guide to Building a Progressive Web App

CONNECTIVITY INDEPENDENT

Page 46: The Hitchhiker's Guide to Building a Progressive Web App
Page 47: The Hitchhiker's Guide to Building a Progressive Web App

MOBILE NETWORKS ARE UNRELIABLE

● LTE service isn’t always LTE

● Mobile devices constantly transfer between antennas, 3G, LTE, and Wi-Fi

● Slow response is worse than fast connection failure

SOURCE: https://hpbn.co

Page 48: The Hitchhiker's Guide to Building a Progressive Web App
Page 49: The Hitchhiker's Guide to Building a Progressive Web App

LATENCY: THE NEW WEB PERFORMANCE BOTTLENECK

SOURCE: https://www.igvita.com/2012/07/19/latency-the-new-web-performance-bottleneck/

Page 50: The Hitchhiker's Guide to Building a Progressive Web App

MOBILE-FIRST WEB DEVELOPMENT

● Responsive Web Design

○ Addresses the mobile user interface

● Service Worker API

○ Addresses the mobile network

● Design for slow mobile devices and slow networks

○ It’ll be ⚡ on high-end phones and fast networks

Page 51: The Hitchhiker's Guide to Building a Progressive Web App

CREDIT: MTV

Page 52: The Hitchhiker's Guide to Building a Progressive Web App
Page 53: The Hitchhiker's Guide to Building a Progressive Web App
Page 54: The Hitchhiker's Guide to Building a Progressive Web App
Page 55: The Hitchhiker's Guide to Building a Progressive Web App

SERVICE WORKER

● Client-side programmable network proxy

● Registered against a domain & path

○ www.devfestdc.org != images.devfestdc.org

○ www.devfestdc.org != www.devfestdc.org/js

● Intercept network requests

● Gives developers control of network failure

Page 56: The Hitchhiker's Guide to Building a Progressive Web App

OFFLINE SUPPORT(USING A SERVICE WORKER)

Page 57: The Hitchhiker's Guide to Building a Progressive Web App
Page 58: The Hitchhiker's Guide to Building a Progressive Web App
Page 59: The Hitchhiker's Guide to Building a Progressive Web App

DEVFESTDC.ORG: INSTALL SERVICE WORKER

● Register a service worker for www.devfestdc.org

● Install at root scope (“/”) so it can handle the entire site

if('serviceWorker' in navigator) {

navigator.serviceWorker

.register('/sw.js')

.then(function() { console.log('Service Worker Registered'); });

}

Page 60: The Hitchhiker's Guide to Building a Progressive Web App

DEVFESTDC.ORG: CACHE ASSETS

● Inside “/sw.js”, cache all the assets

● Include all HTML, CSS, and JS required to load the site

self.addEventListener('install', function(event) {

event.waitUntil(

caches.open('devfestdc-static-v1').then(function(cache) {

return cache.addAll(['/index.html', '/js/all.min.js', ... /* etc */ ]);

});

);

});

Page 61: The Hitchhiker's Guide to Building a Progressive Web App

DEVFESTDC.ORG: RESPOND WITH CACHED ASSETS

● Inside “/sw.js”, add fetch event listener

● Respond to requests for static assets using cache

self.addEventListener('fetch', function(event) {

event.respondWith(caches.open('devfestdc-static-v1').then(function(cache) {

cache.match(event.request).then(function(response) {

return response || fetch(event.request);

});

}));

});

Page 62: The Hitchhiker's Guide to Building a Progressive Web App
Page 63: The Hitchhiker's Guide to Building a Progressive Web App

YEAH! IT WORKED!(BUT SHOULD WE DO IT?)

Page 64: The Hitchhiker's Guide to Building a Progressive Web App

MANAGING CACHES

● Static assets (HTML/CSS/JSS) should be optimized for caching

● If we change “/schedule-day-2/”, we need to update the cache:caches.open('devfestdc-static-v1')

● Hard to maintain

Page 65: The Hitchhiker's Guide to Building a Progressive Web App

SW-PRECACHEhttps://github.com/GoogleChrome/sw-precache

Page 66: The Hitchhiker's Guide to Building a Progressive Web App
Page 67: The Hitchhiker's Guide to Building a Progressive Web App

PRECACHE STATIC ASSETS

● sw-precache is a node module for generating a service worker that pre-caches resources at build time

● Can be integrated into Gulp, Grunt, Webpack or CLI-based build scripts

● Install: npm install sw-precache --save-dev

Page 68: The Hitchhiker's Guide to Building a Progressive Web App

PRECACHE STATIC ASSETS

● Run sw-precache at build time

● Gulp task outputs “/sw.js”:

gulp.task('generate-service-worker', function(callback) {

var swPrecache = require('sw-precache');

swPrecache.write('build/sw.js'), {

staticFileGlobs: [app/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff}']

}, callback);

});

Page 69: The Hitchhiker's Guide to Building a Progressive Web App

THE WAY TO PWAAN INCREMENTAL APPROACH

Page 70: The Hitchhiker's Guide to Building a Progressive Web App

WEB APPLICATION MANIFEST

{ "name": "DevFestDC 2016", "short_name": "DevFestDC", "icons": [ ... { "src": "launcher-icon-4x.png", "sizes": "192x192", "type": "image/png" } ], "start_url": "index.html", "display": "standalone", "orientation": "portrait"}

<link rel="manifest" href="/manifest.json">

Page 71: The Hitchhiker's Guide to Building a Progressive Web App

APP-SHELL ARCHITECTURE

● App shell is the minimal HTML, CSS, and JavaScript required to render the user interface

● Its first load should be extremely quick, then immediately cached (sw-precache)

SOURCE: https://www.devfestdc.rocks/

Page 72: The Hitchhiker's Guide to Building a Progressive Web App

PRPL PATTERN

● Push critical resources for the initial route

● Render initial route (as fast as possible)

● Pre-cache remaining routes

● Lazy-load and create remaining routes on demand

SOURCE: https://www.polymer-project.org/1.0/toolbox/server

Page 73: The Hitchhiker's Guide to Building a Progressive Web App

SW-TOOLBOXhttps://github.com/GoogleChrome/sw-toolbox

Page 74: The Hitchhiker's Guide to Building a Progressive Web App

RUN-TIME CACHING

● sw-toolbox provides handlers for common caching patterns that are useful during run-time:

○ toolbox.cacheFirst

○ toolbox.networkFirst

○ toolbox.fastest

● sw-precache can configure and include sw-toolbox

SOURCE: https://github.com/GoogleChrome/sw-toolbox

Page 75: The Hitchhiker's Guide to Building a Progressive Web App

LIGHTHOUSEhttps://github.com/GoogleChrome/lighthouse

Page 76: The Hitchhiker's Guide to Building a Progressive Web App

AUDIT PROGRESS

● lighthouse audits Progressive Web Apps

● Uses the Chrome Debugging Protocol

● Helps find problems during development

● Available from npm or as Chrome Extension

● github.com/GoogleChrome/lighthouse

Page 77: The Hitchhiker's Guide to Building a Progressive Web App
Page 78: The Hitchhiker's Guide to Building a Progressive Web App
Page 79: The Hitchhiker's Guide to Building a Progressive Web App

PROGRESSIVE WEB APPS

● PWAs are still websites

● Progressively enhanced

● Connectivity independent

● Mobile-first & offline-first

● https://www.devfestdc.rocks/

● github.com/GoogleChrome/sw-toolbox

● github.com/GoogleChrome/sw-precache

● github.com/GoogleChrome/lighthouse

Chris Nguyen@uncompiled