Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Yet Another SW Talk: Images & Video Perf

Yet Another SW Talk: Images & Video Perf

Colin Bendell

August 10, 2018

Transcript

69 slides
  1. "I just want to share my photos from last night"

    "I want to buy a birthday present for mom" "I want to check the news"
  2. Your Brain Decides Without You Why do users leave when

    it's slow? http://nautil.us/issue/19/illusions/how-your-brain-decides-without-you
  3. #WebPerf: Take Action •Measure user perf. Boomerang, Lux, etc •Empathise

    •Solve with tech (USSD, Service Workers, PWA, etc) •Business grows Boss gives you a raise
  4. "A service worker is a script that your browser runs

    in the background, separate from a web page" https://developers.google.com/web/fundamentals/primers/service-workers/
  5. /** * Some Typical SW Events */ // trigger on

    first install or re-isntall addEventListener('install', function(event) { ... } // when the page connects to a SW addEventListener( 'activate', function(event) { ... } // when the browser makes a request (js/css/img/video) addEventListener( 'fetch', function(event) { ... }
  6. /** * Typical SW cache things */ let CACHE =

    'cache-only' caches.open(CACHE).then( myCache => ... )
  7. /** * Typical SW cache things */ let CACHE =

    'cache-only' return caches.open(CACHE).then( myCache => { return myCache.addall(['/logo.jpg', '/prices.json']) }
  8. /** * Typical SW cache things */ let CACHE =

    'cache-only' return caches.open(CACHE).then( myCache => { return myCache.match(fetchEvent.request) }
  9. • Image optimization • Progressive image delivery (use Intersection Observer)

    • Use bundle splitting • Deferred fetching with lazy loading • Prefetching • Take advantage of cache and web storage • Pretty much every other thing to make your web app fast and deliver better user experience
  10. addEventListener( 'fetch', ( fetchEvent ) => { if ( fetchEvent.request.destination

    !== 'image' ) { // console.log('not an image request'); return; } } );
  11. addEventListener( 'fetch', ( fetchEvent ) => { if ( fetchEvent.request.destination

    !== 'image' ) { // console.log('not an image request'); return; } const url = fetchEvent.request.url; fetchEvent.respondWith( fetch(url); ); } );
  12. addEventListener( 'fetch', ( fetchEvent ) => { if ( fetchEvent.request.destination

    !== 'image' ) { // console.log('not an image request'); return; } const url = fetchEvent.request.url; fetchEvent.respondWith( fetch( fasterrrrr( url ) ); ); } );
  13. 1 2 8 0 X 5 2 0 X 4

    B Y T E S = 2 . 6 M B !
  14. if ( // Save-Data is on fetchEvent.request.headers.get( 'save-data' ) //

    bandwidth or RTT is slower than a typical 3G connection || (navigator.connection.effectiveType.match( /2g/ ) ) // we have less than ~1GB of RAM || (navigator.deviceMemory < 1 ) ) {
  15. “…25% of new Android phones have only 512MB of RAM.”

    Jen Fitzpatrick VP of product management for Google Maps
  16. if ( // Save-Data is on fetchEvent.request.headers.get( 'save-data' ) //

    bandwidth or RTT is slower than a typical 3G connection || (navigator.connection.effectiveType.match( /2g/ ) ) // we have less than ~1GB of RAM || (navigator.deviceMemory < 1 ) ) {
  17. if ( // Save-Data is on fetchEvent.request.headers.get( 'save-data' ) //

    bandwidth or RTT is slower than a typical 3G connection || (navigator.connection.effectiveType.match( /2g/ ) ) // we have less than ~1GB of RAM || (navigator.deviceMemory < 1 ) // we are on meh cellular || (this.cell === true && this.rtt >= 100) ) {
  18. Source: Auth0 Housing.com 40% lower bounce rate 30% faster page

    load 10% longer avg session @colinbendell
  19. Service-Worker Take Action: •Remember the other users •SW to adapt

    the experience •Offline Caching •Reduce pixels to save bytes & memory

More Decks by Colin Bendell

Other Decks in Technology