Slide 1

Slide 1 text

SERVICE WORKERS BEYOND THE CACHE

Slide 2

Slide 2 text

WHY? @philnash

Slide 3

Slide 3 text

BETTER EXPERIENCES @philnash

Slide 4

Slide 4 text

Phil Nash @philnash http:/ /philna.sh [email protected]

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Service Worker Support ✅ ✅ ✅ ✅ ✅ @philnash

Slide 7

Slide 7 text

Service workers in Angular $ ng add @angular/pwa @philnash

Slide 8

Slide 8 text

OFFLINE

Slide 9

Slide 9 text

{ "index": "/index.html", "assetGroups": [{ "name": "app", "installMode": "prefetch", "resources": { "files": ["/favicon.ico", "/index.html", "/*.css", "/*.js"] } }, { "name": "assets", "installMode": "lazy", "updateMode": "prefetch", "resources": { "files": ["/assets/**"] } }] } @philnash

Slide 10

Slide 10 text

MANIFEST.JSON ICONS @philnash

Slide 11

Slide 11 text

PROGRESSIVE WEB APP @philnash

Slide 12

Slide 12 text

BETTER EXPERIENCES @philnash

Slide 13

Slide 13 text

APP LIKE FEATURES @philnash

Slide 14

Slide 14 text

PUSH NOTIFICATIONS BACKGROUND SYNC @philnash

Slide 15

Slide 15 text

PUSH NOTIFICATIONS

Slide 16

Slide 16 text

Push Noti cation Support ✅ ✅ ✅ ❌ ✅ @philnash

Slide 17

Slide 17 text

@philnash

Slide 18

Slide 18 text

@philnash

Slide 19

Slide 19 text

@philnash

Slide 20

Slide 20 text

PUSH NOTIFICATIONS ARE INCREDIBLY USEFUL @philnash

Slide 21

Slide 21 text

Good noti cations • Appointment reminders • Chat alerts • ETA alerts • Event updates @philnash

Slide 22

Slide 22 text

TIMELY, ACTIONABLE AND PERSONAL @philnash

Slide 23

Slide 23 text

TIMELY @philnash

Slide 24

Slide 24 text

IN TIME AT THE RIGHT TIME OF DAY, IN THE RIGHT TIME ZONE, THE RIGHT NUMBER OF NOTIFICATIONS @philnash

Slide 25

Slide 25 text

ACTIONABLE @philnash

Slide 26

Slide 26 text

Actionable noti cations "Daisy sent you a message" "Your car is here" "You can check in now" @philnash

Slide 27

Slide 27 text

PERSONAL @philnash

Slide 28

Slide 28 text

Personal noti cations "Daisy sent you a message" "Your car is here" "You can check in now" @philnash

Slide 29

Slide 29 text

HOW TO PUSH @philnash

Slide 30

Slide 30 text

@philnash

Slide 31

Slide 31 text

VAPID @philnash

Slide 32

Slide 32 text

VOLUNTARY APPLICATION SERVER IDENTIFICATION FOR WEB PUSH @philnash

Slide 33

Slide 33 text

VOLUNTARY APPLICATION SERVER IDENTIFICATION FOR WEB PUSH @philnash

Slide 34

Slide 34 text

VAPID npx web-push generate-vapid-keys ======================================= Public Key: BPARzOEvMc0MkHDN7TT25vorJ1vA9pmbEVUE6T-C6Ori3f8hylaeflA89QF... Private Key: 0mLkNulmnEB1jQxvrZ7hyWDfbWMQh2IcdxmeZtEbUIw ======================================= @philnash

Slide 35

Slide 35 text

VAPID Subscribe to push noti cations with public key Sign a JWT with private key when sending a push This identi es your application @philnash

Slide 36

Slide 36 text

IN ANGULAR? @philnash

Slide 37

Slide 37 text

swPush @philnash

Slide 38

Slide 38 text

DEMO

Slide 39

Slide 39 text

Missing self.addEventListener('notificationclick', (event) => { // handle click }); 01. 02. 03. @philnash

Slide 40

Slide 40 text

WE NEED TO TALK ABOUT... @philnash

Slide 41

Slide 41 text

PERMISSIONS

Slide 42

Slide 42 text

Ketan Joshi @KetanJ0 Dear website, - I don't want you to post notifications to my desktop - I don't want to subscribe to your thing via a popup - I don't want you to know my location - Please stop autoplaying that video - I am angry and sad now 7:17 PM - May 13, 2018 31.3K 8,720 people are talking about this @philnash

Slide 43

Slide 43 text

WEB DEVELOPERS ARE MAKING USERS SAD AND ANGRY @philnash

Slide 44

Slide 44 text

Permissions • Push noti cations • Geolocation • Media Devices • Bluetooth, MIDI, WebUSB... @philnash

Slide 45

Slide 45 text

@philnash

Slide 46

Slide 46 text

@philnash

Slide 47

Slide 47 text

@philnash

Slide 48

Slide 48 text

@philnash

Slide 49

Slide 49 text

@philnash

Slide 50

Slide 50 text

NO CONTEXT @philnash

Slide 51

Slide 51 text

STOP THIS!

Slide 52

Slide 52 text

@philnash

Slide 53

Slide 53 text

PLEASE

Slide 54

Slide 54 text

DO NOT DEMAND NOTIFICATION PERMISSIONS ON PAGE LOAD @philnash

Slide 55

Slide 55 text

WORRY

Slide 56

Slide 56 text

PERMISSIONS WILL BECOME THE NEW POPUPS @philnash

Slide 57

Slide 57 text

PERMISSIONS WILL BE REVILED AND DISMISSED BY USERS @philnash

Slide 58

Slide 58 text

@philnash

Slide 59

Slide 59 text

@philnash

Slide 60

Slide 60 text

PLEASE

Slide 61

Slide 61 text

DO NOT DEMAND NOTIFICATION PERMISSIONS ON PAGE LOAD @philnash

Slide 62

Slide 62 text

SUGGESTIONS

Slide 63

Slide 63 text

WAIT @philnash

Slide 64

Slide 64 text

@philnash

Slide 65

Slide 65 text

@philnash

Slide 66

Slide 66 text

UNLESS IT'S OBVIOUS @philnash

Slide 67

Slide 67 text

@philnash

Slide 68

Slide 68 text

LET THEM OPT OUT WITHIN YOUR APPLICATION @philnash

Slide 69

Slide 69 text

@philnash

Slide 70

Slide 70 text

THE ALTERNATIVE? @philnash

Slide 71

Slide 71 text

@philnash

Slide 72

Slide 72 text

USE PUSH NOTIFICATIONS FOR GOOD @philnash

Slide 73

Slide 73 text

BACKGROUND SYNC

Slide 74

Slide 74 text

IF CACHES ARE FOR GET REQUESTS @philnash

Slide 75

Slide 75 text

THEN BACKGROUD SYNC IS FOR POST REQUESTS @philnash

Slide 76

Slide 76 text

Background sync • Sending messages • Posting comments • Adding to a shopping cart • Liking or favouriting @philnash

Slide 77

Slide 77 text

@philnash

Slide 78

Slide 78 text

@philnash

Slide 79

Slide 79 text

BACK ONLINE API

Slide 80

Slide 80 text

Background Sync Support ✅ ❌ ❌ ❌ ❌ @philnash

Slide 81

Slide 81 text

INDEXEDDB @philnash

Slide 82

Slide 82 text

The plan 1. Request fails due to network 2. Save request data in IndexedDB 3. User comes back online 4. Sync event res, replay request @philnash

Slide 83

Slide 83 text

DEMO

Slide 84

Slide 84 text

Workarounds npm install patch-package @philnash

Slide 85

Slide 85 text

ISSUES? @philnash

Slide 86

Slide 86 text

Issues Support background sync: github.com/angular/angular/issues/22145 Handle notificationclick : github.com/angular/angular/issues/22311 @philnash

Slide 87

Slide 87 text

@philnash

Slide 88

Slide 88 text

Workbox const bgSyncPlugin = new workbox.backgroundSync.Plugin('myQueueName', { maxRetentionTime: 24 * 60 // Retry for max of 24 Hours }); workbox.routing.registerRoute( /\/api\/.*\/*.json/, workbox.strategies.networkOnly({ plugins: [bgSyncPlugin] }), 'POST' ); 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. @philnash

Slide 89

Slide 89 text

ENHANCE YOUR FORMS WITH BACKGROUND SYNC @philnash

Slide 90

Slide 90 text

EXPERIMENTAL

Slide 91

Slide 91 text

BACKGROUND FETCH

Slide 92

Slide 92 text

On the page navigator.serviceWorker.ready.then(function(reg) { const url = "https://example.com/largeFile.mp3"; reg.backgroundFetch.fetch('downloads', [url], { icons: [], title: 'Downloading largeFile.mp3', totalDownloadSize: 1048576 }); }); 01. 02. 03. 04. 05. 06. 07. 08. @philnash

Slide 93

Slide 93 text

In the service worker self.addEventListener('backgroundfetched', function(event) { if (event.tag === 'downloads') { event.updateUI('largeFile.mp3 downloaded.'); // add the file to the cache } }); 01. 02. 03. 04. 05. 06. @philnash

Slide 94

Slide 94 text

Background fetch • Audio and video • Games • WebAssembly @philnash

Slide 95

Slide 95 text

CONCLUSION

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

Easy mode $ ng add @angular/pwa @philnash

Slide 98

Slide 98 text

Not just features • Push noti cations • Background Sync • Background Fetch @philnash

Slide 99

Slide 99 text

BETTER EXPERIENCES @philnash

Slide 100

Slide 100 text

THANKS!

Slide 101

Slide 101 text

Icon Credits • Browser by Kimmi Studio from the Noun Project • Cloud by Kimmi Studio from the Noun Project • Gears by TS Graphics from the Noun Project • wi by i cons from the Noun Project @philnash

Slide 102

Slide 102 text

Thanks! @philnash http:/ /philna.sh [email protected]