Ali Alabbas -...

165
#msedgesummit Ali Alabbas Working on service workers, storage and networking @ MSEdgeDev aliams

Transcript of Ali Alabbas -...

Page 1: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Ali AlabbasWorking on service workers, storage and networking

@MSEdgeDev

aliams

Page 2: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Service WorkersGoing beyond the page

Page 3: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

A quick history

#msedgesummit

Page 4: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

native web

#msedgesummit

Page 5: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 6: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Web Graphics

Web Crypto

Indexed Database

Content Editable

Screen Orientation

Full screen API

Peer-to-peer

Web Workers

Web Payments

Web Manifest

Web Video

Web Audio

Touch Events

Gamepad API

History API

Web Sockets

Vibration API

Web Storage

Canvas

File API

App Cache

Geolocation

Ambient Light API

Battery Status API

Web Authentication

Web Animations

Web Notifications

Speech Recognition

Speech Synthesis

Web Bluetooth

#msedgesummit

Page 7: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

native web

#msedgesummit

Page 8: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

native web

#msedgesummit

Page 9: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Start and run really REALLY fast!

Work across a variety of devices, screen sizes, etc.

Run over HTTPs

Work offline / reliably under poor network conditions

Can send push notifications

Provide better device integration

#msedgesummit

Page 10: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Start and run really REALLY fast!

Work across a variety of devices, screen sizes, etc.

Run over HTTPs

Work offline / reliably under poor network conditions

Can send push notifications

Provide better device integration

#msedgesummit

Page 11: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Start and run really REALLY fast!

Work across a variety of devices, screen sizes, etc.

Run over HTTPs

Work offline / reliably under poor network conditions

Can send push notifications

Provide better device integration

#msedgesummit

Page 12: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 13: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 14: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 15: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Start and run really REALLY fast!

Work across a variety of devices, screen sizes, etc.

Run over HTTPs

Work offline / reliably under poor network conditions

Can send push notifications

Provide better device integration

#msedgesummit

Page 16: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 17: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

:(

Page 18: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

By 2012we were able to do offline on the web

#msedgesummit

Page 19: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 20: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. <html manifest="appcache.manifest">

2. ...

3. <html>

index.html

git.io/v563g

Page 21: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. manifest="appcache.manifest"

2.

3.

index.html

git.io/v563g

Page 22: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. CACHE MANIFEST

2. # 2017-09-01 v2

3.

4. CACHE:

5. /styles.css

6. /main.js

7. /sunny-bg.png

8.

9. NETWORK:

10. *

11.

12. FALLBACK:

13. /index.html /fallback.html

appcache.manifest

git.io/v563g

Page 23: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. CACHE MANIFEST

2. # 2017-09-01 v2

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

appcache.manifest

git.io/v563g

Page 24: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. CACHE:

5. /styles.css

6. /main.js

7. /sunny-bg.png

8.

9.

10.

11.

12.

13.

appcache.manifest

git.io/v563g

Page 25: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7.

8.

9. NETWORK:

10. *

11.

12.

13.

appcache.manifest

git.io/v563g

Page 26: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12. FALLBACK:

13. /index.html /fallback.html

appcache.manifest

git.io/v563g

Page 27: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Application Cache is a Douchebag.”

#msedgesummit

“tinyurl.com/ydaabrow

Page 28: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

must change manifest to update

caching manifest file will make site stale

files come from AppCache even if online

statically defined: no dynamic caching

“magical” and difficult to debug

#msedgesummit

APP CACHE

GOTCHAS!

Page 29: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

No control!

#msedgesummit

APP CACHE

GOTCHAS!

Page 30: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

App Cache is usefulbut we can do better!

#msedgesummit

Page 31: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

What do we have to do?Give the control back to you!

#msedgesummit

Page 32: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

First,we have to start with network requests

#msedgesummit

Page 33: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Fetch!

#msedgesummit

Page 34: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

CSS

JS

IMG

XHR

HTML

#msedgesummit

Page 35: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

CSS

JS

IMG

XHR

HTML

FETCH

#msedgesummit

Page 36: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Exposed to JavaScriptfetch(), Request and Response

#msedgesummit

Page 37: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. fetch('weather.json')

2. .then(function(response) {

3. if (response.headers.get('content-type') == 'application/json') {

4. return response.json();

5. } else {

6. throw new TypeError();

7. }

8. })

9. .then(processJSON);

index.html

git.io/v563M

Page 38: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. fetch('weather.json')

2.

3.

4.

5.

6.

7.

8.

9.

index.html

git.io/v563M

Page 39: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2. .then(function(response) {

3.

4.

5.

6.

7.

8.

9.

index.html

git.io/v563M

Page 40: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. response.headers.get('content-type') == 'application/json'

4.

5.

6.

7.

8.

9.

index.html

git.io/v563M

Page 41: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. return response.json();

5.

6.

7.

8.

9.

index.html

git.io/v563M

Page 42: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. var xhr = new XMLHttpRequest();

2. xhr.open('GET', 'weather.json');

3. xhr.responseType = 'json';

4. xhr.onload = function() {

5. if (xhr.status == 200

6. && xhr.getResponseHeader('Content-Type') == 'application/json') {

7. processJSON(xhr.response);

8. } else {

9. throw new TypeError();

10. }

11. }

12. xhr.send();

index.html

git.io/v563M

Page 43: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

CSS

JS

IMG

XHR

HTML

FETCH

#msedgesummit

Page 44: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Web Workers

#msedgesummit

Page 45: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Web Workers

#msedgesummit

Page 46: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Web Workers

#msedgesummit

Service

Page 47: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 48: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onfetch = function(event) {

2. event.respondWith(

3. fetch(event.request);

4. );

5. }

sw.js

Page 49: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onfetch

2.

3.

4.

5.

sw.js

Page 50: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2. event.respondWith

3.

4.

5.

sw.js

Page 51: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. fetch(event.request);

4.

5.

sw.js

Page 52: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 53: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

We need to save Responses!

#msedgesummit

Page 54: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Cache APIs

#msedgesummit

Page 55: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 56: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

CACHES API

ANATOMY

REQUEST (URL) RESPONSE

https://weather.app/index.html <!doctype html>

https://weather.app/script.js console.log('test')

. . .

key / value storage

Page 57: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Cache APIURL-addressable resources

----- or -----

Indexed DBOther data

#msedgesummit

Page 58: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

OK! Now we’re in business!

#msedgesummit

Page 59: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. navigator.serviceWorker.register('sw.js', {scope: '/'})

2. .then(

3. function (registration) {

4. console.log('Service worker registered!');

5. },

6. function (err) {

7. console.error('Installation failed!', err);

8. }

9. );

index.html

git.io/v56sU

Page 60: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. navigator.serviceWorker.register

2.

3.

4.

5.

6.

7.

8.

9.

index.html

git.io/v56sU

Page 61: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. 'sw.js'

2.

3.

4.

5.

6.

7.

8.

9.

index.html

git.io/v56sU

Page 62: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. {scope: '/'}

2.

3.

4.

5.

6.

7.

8.

9.

index.html

git.io/v56sU

Page 63: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. function (registration) {

4. console.log('Service worker registered!');

5. }

6.

7.

8.

9.

index.html

git.io/v56sU

Page 64: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6. function (err) {

7. console.error('Installation failed!', err);

8. }

9.

index.html

git.io/v56sU

Page 65: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Service Worker Lifecycle

Beginning of registration – set everything upInstalling

Page 66: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.oninstall = function(event) {

2. event.waitUntil(

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

4. return cache.addAll([

5. '/',

6. '/index.html',

7. '/styles.css',

8. '/main.js',

9. '/fallback.html'

10. ]);

11. })

12. );

13. }

sw.js

git.io/v56sU

Page 67: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.oninstall

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

sw.js

git.io/v56sU

Page 68: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2. event.waitUntil

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

sw.js

git.io/v56sU

Page 69: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. caches.open('static-v1')

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

sw.js

git.io/v56sU

Page 70: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. return cache.addAll([

5. '/',

6. '/index.html',

7. '/styles.css',

8. '/main.js',

9. '/fallback.html'

10. ]);

11.

12.

13.

sw.js

git.io/v56sU

Page 71: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Service Worker Lifecycle

Beginning of registration – set everything upInstalling

Setup complete and awaiting other clients to be closedInstalled

No clients controlled – clean up other workersActivating

#msedgesummit

Page 72: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onactivate = function(event) {

2. var keepList = ['assets-v1'];

3.

4. event.waitUntil(

5. caches.keys().then(function(cacheNameList) {

6. return Promise.all(cacheNameList.map(function(cacheName) {

7. if (keepList.indexOf(cacheName) === -1) {

8. return caches.delete(cacheName);

9. }

10. }));

11. })

12. );

13. }

sw.js

git.io/v56sU

Page 73: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onactivate

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

sw.js

git.io/v56sU

Page 74: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2. var keepList = ['assets-v1'];

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

sw.js

git.io/v56sU

Page 75: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. event.waitUntil

5.

6.

7.

8.

9.

10.

11.

12.

13.

sw.js

git.io/v56sU

Page 76: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5. caches.keys().then(function(cacheNameList) {

6.

7.

8.

9.

10.

11.

12.

13.

sw.js

git.io/v56sU

Page 77: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6. return Promise.all(cacheNameList.map(function(cacheName) {

7.

8.

9.

10.

11.

12.

13.

sw.js

git.io/v56sU

Page 78: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7. if (keepList.indexOf(cacheName) === -1) {

8. return caches.delete(cacheName);

9. }

10.

11.

12.

13.

sw.js

git.io/v56sU

Page 79: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Service Worker Lifecycle

Beginning of registration – set everything upInstalling

Setup complete and awaiting other clients to be closedInstalled

No clients controlled – clean up other workersActivating

The worker can now handle fetch eventsActivated

#msedgesummit

Page 80: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onfetch = function(event) {

2. event.respondWith(

3. caches.match(event.request).then(function(response) {

4. return response || fetch(event.request).catch(function() {

5. return caches.match('/fallback.htm1');

6. });

7. })

8. );

9. }

sw.js

git.io/v56sU

Page 81: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. caches.match(event.request)

4.

5.

6.

7.

8.

9.

sw.js

git.io/v56sU

Page 82: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. return response

5.

6.

7.

8.

9.

sw.js

git.io/v56sU

Page 83: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. || fetch(event.request)

5.

6.

7.

8.

9.

sw.js

git.io/v56sU

Page 84: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. catch(function() {

5. return caches.match('/fallback.htm1');

6. });

7.

8.

9.

sw.js

git.io/v56sU

Page 85: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Service Worker Lifecycle

Beginning of registration – set everything upInstalling

Setup complete and awaiting other clients to be closedInstalled

No clients controlled – clean up other workersActivating

The worker can now handle fetch eventsActivated

The worker is being replaced by anotherRedundant

Page 86: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 87: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Let’s cache dynamically!

#msedgesummit

Page 88: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 89: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onfetch = function(event) {

2. event.respondWith(

3. caches.match(event.request).then(function(response) {

4. return response || fetch(event.request).catch(function() {

5. return caches.match('/fallback.htm1');

6. });

7. })

8. );

9. }

sw.js

git.io/v56sU

Page 90: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. .catch(function() {

5. return caches.match('/fallback.htm1');

6. });

7.

8.

9.

sw.js

git.io/v56sU

Page 91: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. .then(function(response) {

5. caches.open('dynamic').then(function(cache) {

6. cache.put(event.request, response);

7. });

8. return response.clone();

9. });

10.

11.

12.

sw.js

git.io/v56s4

Page 92: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5. caches.open('dynamic').then(function(cache) {

6. cache.put(event.request, response);

7. });

8.

9.

10.

11.

12.

sw.js

git.io/v56s4

Page 93: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7.

8. return response.clone();

9.

10.

11.

12.

sw.js

git.io/v56s4

Page 94: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

But… we can do better!

#msedgesummit

Page 95: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 96: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onfetch = function(event) {

2. if (event.request.url.indexOf('weather.json') !== -1) {

3. event.respondWith(fetchAndCache(event.request));

4. } else {

5. event.respondWith(

6. caches.match(event.request).then(function(response) {

7. return response || fetchAndCache(event.request);

8. })

9. );

10. }

11. }

sw.js

git.io/v56s4

Page 97: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2. if (event.request.url.indexOf('weather.json') !== -1)

3.

4.

5.

6.

7.

8.

9.

10.

11.

sw.js

git.io/v56s4

Page 98: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. event.respondWith(fetchAndCache(event.request));

4.

5.

6.

7.

8.

9.

10.

11.

sw.js

git.io/v56s4

Page 99: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. } else {

5. event.respondWith(

6. caches.match(event.request).then(function(response) {

7. return response || fetchAndCache(event.request);

8. })

9. );

10. }

11.

sw.js

git.io/v56s4

Page 100: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. function fetchAndCache(request) {

2. return caches.open('dynamic').then(function(cache) {

3. return fetch(request).then(function(response) {

4. cache.put(request, response.clone());

5.

6. return response;

7. });

8. });

9. }

sw.js

git.io/v56s4

Page 101: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2. return caches.open('dynamic')

3.

4.

5.

6.

7.

8.

9.

sw.js

git.io/v56s4

Page 102: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. return fetch(request)

4.

5.

6.

7.

8.

9.

sw.js

git.io/v56s4

Page 103: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. cache.put(request, response.clone());

5.

6.

7.

8.

9.

sw.js

git.io/v56s4

Page 104: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6. return response;

7.

8.

9.

sw.js

git.io/v56s4

Page 105: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. showLoading();

2.

3. var networkDone = false;

4.

5. var networkRequest = fetch('weather.json')

6. .then(function(response) {

7. return response.json();

8. })

9. .then(function(json) {

10. networkDone = true;

11. updatePage(json);

12. });

index.html

git.io/v56s4

Page 106: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. showLoading();

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

index.html

git.io/v56s4

Page 107: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. var networkDone = false;

4.

5.

6.

7.

8.

9.

10.

11.

12.

index.html

git.io/v56s4

Page 108: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5. var networkRequest = fetch('weather.json')

6.

7.

8.

9.

10.

11.

12.

index.html

git.io/v56s4

Page 109: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7. return response.json();

8.

9.

10.

11.

12.

index.html

git.io/v56s4

Page 110: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7.

8.

9. {

10. networkDone = true;

11. updatePage(json);

12.

index.html

git.io/v56s4

Page 111: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. caches.match('weather.json')

2. .then(function(response) {

3. if (!response) throw Error('No data');

4.

5. return response.json();

6. })

7. .then(function(json) {

8. if (!networkDone) updatePage(json);

9. })

10. .catch(function() {return networkRequest;})

11. .catch(function() {console.log('We have nothing.');})

12. .then(hideLoading);

index.html

git.io/v56s4

Page 112: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. caches.match('weather.json')

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

index.html

git.io/v56s4

Page 113: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. if (!response) throw Error('No data');

4.

5. return response.json();

6.

7.

8.

9.

10.

11.

12.

index.html

git.io/v56s4

Page 114: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7.

8. if (!networkDone) updatePage(json);

9.

10.

11.

12.

index.html

git.io/v56s4

Page 115: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7.

8.

9.

10. .catch(function() {return networkRequest;})

11.

12.

index.html

git.io/v56s4

Page 116: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11. .catch(function() {console.log('We have nothing.');})

12.

index.html

git.io/v56s4

Page 117: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12. .then(hideLoading);

index.html

git.io/v56s4

Page 118: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Service Worker Events

Lifetimeinstall

activate

Functionalfetch

Page 119: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Service Worker Events

Lifetimeinstall

activate

Functionalfetch

Page 120: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Start and run really REALLY fast!

Work across a variety of devices, screen sizes, etc.

Run over HTTPs

Work offline / reliably under poor network conditions

Can send push notifications

Provide better device integration

#msedgesummit

Page 121: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

A web app model that enables event-driven JavaScript to run independent of web pages

#msedgesummit

Service workers

Page 122: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Start and run really REALLY fast!

Work across a variety of devices, screen sizes, etc.

Run over HTTPs

Work offline / reliably under poor network conditions

Can send push notifications

Provide better device integration

#msedgesummit

Page 123: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Kristi Owens says

Thank you for sending that over. I’ll

let you know how it goes!

Monday

×

#msedgesummit

Page 124: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 125: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 126: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 127: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 128: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. navigator.serviceWorker.register('sw.js')

2. .then(function(reg) {

3. return reg.pushManager.getSubscription().then(function(subscription) {

4. if (subscription) return subscription;

5. return registration.pushManager.subscribe({

6. userVisibleOnly: true, applicationServerKey: appPubkey

7. });

8. });

9. })

index.html

git.io/v56s1

Page 129: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. navigator.serviceWorker.register('sw.js')

2.

3.

4.

5.

6.

7.

8.

9.

index.html

git.io/v56s1

Page 130: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. reg.pushManager.getSubscription()

4.

5.

6.

7.

8.

9.

index.html

git.io/v56s1

Page 131: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. if (subscription) return subscription;

5.

6.

7.

8.

9.

index.html

git.io/v56s1

Page 132: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4.

5. return registration.pushManager.subscribe({

6. userVisibleOnly: true, applicationServerKey: appPubkey

7. });

8.

9.

index.html

git.io/v56s1

Page 133: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onpush = function(event) {

2. var payload = event.data ? event.data.text() : 'no payload';

3. event.waitUntil(

4. registration.showNotification('WEATHER ADVISORY', {

5. body: payload,

6. icon: 'icon.png'

7. })

8. );

9. }

sw.js

git.io/v56s1

Page 134: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onpush

2.

3.

4.

5.

6.

7.

8.

9.

sw.js

git.io/v56s1

Page 135: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2. var payload = event.data ? event.data.text() : 'no payload';

3.

4.

5.

6.

7.

8.

9.

sw.js

git.io/v56s1

Page 136: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. event.waitUntil

4.

5.

6.

7.

8.

9.

sw.js

git.io/v56s1

Page 137: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3.

4. registration.showNotification('WEATHER ADVISORY', {

5. body: payload,

6. icon: 'icon.png'

7. })

8.

9.

sw.js

git.io/v56s1

Page 138: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onnotificationclick = function(event) {

2. event.notification.close();

3. event.waitUntil(clients.openWindow('https://weather.app/advisory'));

4. }

sw.js

git.io/v56s1

Page 139: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1. self.onnotificationclick

2.

3.

4.

sw.js

git.io/v56s1

Page 140: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2. event.notification.close();

3.

4.

sw.js

git.io/v56s1

Page 141: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

1.

2.

3. event.waitUntil(clients.openWindow('https://weather.app/advisory'));

4.

sw.js

git.io/v56s1

Page 142: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Service Worker Events

Lifetimeinstall

activate

Functionalfetchpush

notificationclick

Page 143: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Service Worker Events

Lifetimeinstall

activate

Functionalfetchpush

notificationclicksync…

Page 144: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

HTTPS-only

no DOM access

trumps App Cache

script can’t be on CDN

can be terminated any time

only async requests allowed

executes separate from page

service worker to scope is 1:1

#msedgesummit

SERVICE WORKERS

PROPERTIES

Page 145: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

But… can I use it?

#msedgesummit

Page 146: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 147: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 148: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Fall Creators UpdateEnable in Edge using about:flags

#msedgesummit

Page 149: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 150: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 151: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

F12Developer Tools

#msedgesummit

Page 152: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 153: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 154: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 155: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 156: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

NotificationsAction Center and Permissions

#msedgesummit

Page 157: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 158: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 159: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Page 160: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Storage QuotasIndexedDB, Cache APIs, and Service Workers

#msedgesummit

Page 161: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

VOLUME SIZE DOMAIN LIMIT OVERALL LIMIT

≤ 8 GB 10 MB 50 MB

> 8 - 32 GB 50 MB 500 MB

> 32 - 128 GB 250 MB 4% of volume size

> 128 GB 500 MB 4% or 20 GB (whichever is smaller)

QuotasIndexedDB

Page 162: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

VOLUME SIZE DOMAIN LIMIT OVERALL LIMIT

≤ 8 GB 20% of overall 50 MB

> 8 - 32 GB “ 500 MB

> 32 - 128 GB “ 4% of volume size

> 128 GB “ 4% or 20 GB (whichever is smaller)

QuotasIndexedDB & Caches API

Page 163: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Try it today!Windows Insider Preview: enable flag in Edge

#msedgesummit

Page 164: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

Tell us what you think!We want to hear from you

#msedgesummit

Page 165: Ali Alabbas - video.ch9.msvideo.ch9.ms/sessions/c1f9c808-82bc-480a-a930-b340097f6cc1/05Service... · Start and run really REALLY fast! Work across a variety of devices, screen sizes,

#msedgesummit

Thank you!

aka.ms/SWSlides2017

@MSEdgeDev

aliams