service workers - velocity 2016 training

39
DEBUGGING FRONT-END PERFORMANCE Tim Kadlec (@tkadlec) & Pat Meenan (@patmeenan) June 20-21, 2016 at Velocity Conference (#velocityconf)

Upload: patrick-meenan

Post on 19-Feb-2017

213 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Service workers - Velocity 2016 Training

DEBUGGINGFRONT-END PERFORMANCE

Tim Kadlec (@tkadlec) & Pat Meenan (@patmeenan)June 20-21, 2016 at Velocity Conference (#velocityconf)

Page 2: Service workers - Velocity 2016 Training

SERVICE WORKERS

Page 3: Service workers - Velocity 2016 Training

RESOURCE FETCHING (IN CHROME)

DOM

Page 4: Service workers - Velocity 2016 Training

RESOURCE FETCHING (IN CHROME)

DOM

In-memory Resource

Cache

Page 5: Service workers - Velocity 2016 Training

RESOURCE FETCHING (IN CHROME)

DOM

In-memory Resource

Cache

Resource Fetcher

Page 6: Service workers - Velocity 2016 Training

RESOURCE FETCHING (IN CHROME)

DOM Resource Fetcher

Net Stack

In-memory Resource

Cache

Page 7: Service workers - Velocity 2016 Training

RESOURCE FETCHING (IN CHROME)

DOM

In-memory Resource

Cache

Resource Fetcher

Net Stack

Disk Cache

Page 8: Service workers - Velocity 2016 Training

RESOURCE FETCHING (IN CHROME)

DOM

In-memory Resource

Cache

Resource Fetcher

Net Stack Net

Disk Cache

Page 9: Service workers - Velocity 2016 Training

RESOURCE FETCHING (IN CHROME)

DOM

In-memory Resource

Cache

Resource Fetcher

Net Stack Net

Disk Cache

Service Worker

Page 10: Service workers - Velocity 2016 Training

• Sees every request for your documento Including cross-origino And headers

• Can synthesize responses• Supports fetch• Has a programmable cache and Indexed DB

CAPABILITIES

Page 11: Service workers - Velocity 2016 Training

• HTTPS documents only• Not active for first view• iFrames are separate documents• Non-CORS Cross-origin responses are opaque• No global stateo or concept of a “page”

• No Sync API’s (localstorage, XHR, etc)

LIMITATIONS

Page 12: Service workers - Velocity 2016 Training

BROWSER SUPPORT

http://caniuse.com/#feat=serviceworkers

Page 13: Service workers - Velocity 2016 Training

if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/my-app/sw.js', { scope: '/my-app/' });}

REGISTRATION

Page 14: Service workers - Velocity 2016 Training

• Honors caching headers• Forced re-download every 24 hours

DOWNLOAD

Page 15: Service workers - Velocity 2016 Training

self.addEventListener('install', function(event) { event.waitUntil( fetchStuffAndInitDatabases() );});

To install immediately:event.waitUntil(self.skipWaiting());

INSTALLATION EVENT

Page 16: Service workers - Velocity 2016 Training

self.addEventListener('activate', function(event) { // You're good to go!});

To activate immediately:event.waitUntil(self.clients.claim());

ACTIVATION EVENT

Page 17: Service workers - Velocity 2016 Training

From Page:navigator.serviceWorker.controller.postMessage(message);

In Service Worker:self.addEventListener('message', function(event) {}

MESSAGING

Page 18: Service workers - Velocity 2016 Training

self.addEventListener('fetch', function(event) { event.respondWith(new Response("Hello world!"));});

REQUEST INTERCEPTION

Page 19: Service workers - Velocity 2016 Training

self.importScripts('foo.js');self.importScripts(‘https://ads/offlineads.js');

IMPORTS

Page 20: Service workers - Velocity 2016 Training

Some Use Cases

Page 21: Service workers - Velocity 2016 Training

• Chrome New Tab Page• Flipkart

OFFLINE

Page 22: Service workers - Velocity 2016 Training

• Fetch Request Normally• For non-200 responses serve a local error page• Not just server errors:o DNS failureso CDN/Proxy Errorso Intermediaries

CUSTOM ERROR PAGES

Page 23: Service workers - Velocity 2016 Training

• Identify requests of interesto 3rd-party javascripto Fonts

• Set a timer• Pass fetch requests through• If timer expires before fetch completes

generate error response

SPOF PREVENTION / SLA

Page 24: Service workers - Velocity 2016 Training

• Identify CDN requests by URL• Set a timer• Pass fetch request through• If timer expires, create fetch request to origin• Respond with first fetch request to complete

CDN / ORIGIN FAILOVER

Page 25: Service workers - Velocity 2016 Training

• Identify CDN requests by URL• Replace CDN domain with alternate CDN (or

origin)• Keep track of performance by origin• Prefer faster origin (keep measuring)

• Could also race the CDNs in parallelo Be careful about increased data

MULTI ORIGIN / CDN

Page 26: Service workers - Velocity 2016 Training

• Respond with local cached resourceo Ignoring Expires, max-age, etc.

• Pass fetch request through in parallel• Update cache with new response

• Works best for known resources (analytics/ads JS, etc)

STALE-WHILE-REVALIDATE

Page 27: Service workers - Velocity 2016 Training

• Custom response headers with prefetch resources

• When idle, prefetch suggested resources

PREFETCH

Page 28: Service workers - Velocity 2016 Training

• Delta compression for build->build updateso Include version in URL schemeo Get latest version number from cacheo Request delta from servero Apply patcho Cache new version

• New compression algorithms

DELTA COMPRESSION

Page 29: Service workers - Velocity 2016 Training

• New compression algorithmso Brotlio Fractal image compressiono JSON-specific dictionarieso Application-specific

• Prove-out new algorithms before standardizing• …as long as it can be implemented in JS and

code size is reasonable

CUSTOM COMPRESSION

Page 30: Service workers - Velocity 2016 Training

• Identify JPEG image requests from known origin

• Synthesize response• Range request (or smarter) for first few scans• Stream initial range into synthesized response• Range request for remaining image (some

point later)• Append remaining data into synthesized

response

INCREMENTAL PROGRESSIVE IMAGES

Page 31: Service workers - Velocity 2016 Training

• 930 x 11,362 px WebPageTest waterfall• 351KB compressed PNG• 42 MB in-memory (server) to generate• < 20KB compressed JSON data to describe

• Prefer actual images for WPT waterfalls for easier embeddingo Otherwise SVG, Canvas or DOM would work

DRAWING IMAGES LOCALLY

Page 32: Service workers - Velocity 2016 Training

• Identify appropriate requests (i.e. waterfall.png?...)

• Fetch data necessary to draw image• Draw to canvas• Extract as PNG• Synthesize PNG response

DRAWING IMAGES LOCALLY

Page 33: Service workers - Velocity 2016 Training

• Pass all requests through• Passively observe timings and success• Report metrics back• Gives visibility into failures and requests that are

in flighto Unlike resource timing

METRICS / DEBUGGING

Page 34: Service workers - Velocity 2016 Training

• Background Sync• WebRTC Data Channels?o Custom protocol/fetching

o GeoFencing?o Required for:o Push Notifications

EVOLVING…

Page 35: Service workers - Velocity 2016 Training

Debugging

Page 36: Service workers - Velocity 2016 Training
Page 37: Service workers - Velocity 2016 Training
Page 38: Service workers - Velocity 2016 Training
Page 39: Service workers - Velocity 2016 Training

THANK YOU!

Tim Kadlec (@tkadlec) & Pat Meenan (@patmeenan)June 20-21, 2016 at Velocity Conference (#velocityconf)

Icons courtesy of The Noun Project