Slide 1

Slide 1 text

@adunkman Executing JavaScript Across Browser Contexts

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

@adunkman Standard Browser APIs

Slide 5

Slide 5 text

@adunkman Between Tabs Outside Tabs

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

@adunkman donate.splcenter.org

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

@adunkman rest in peace, vine.

Slide 22

Slide 22 text

@adunkman Standard Browser APIs

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

@adunkman

Slide 38

Slide 38 text

@adunkman

Slide 39

Slide 39 text

@adunkman Cache-Control

Slide 40

Slide 40 text

@adunkman

Slide 41

Slide 41 text

@adunkman There’s more to the web than web pages

Slide 42

Slide 42 text

@adunkman Browser Contexts

Slide 43

Slide 43 text

@adunkman Window Sandbox

Slide 44

Slide 44 text

@adunkman Domain-Level Objects

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

@adunkman Why isn’t this better?

Slide 48

Slide 48 text

@adunkman document.cookie

Slide 49

Slide 49 text

Bob Dunkman c. 1977

Slide 50

Slide 50 text

Bob Dunkman c. 2016

Slide 51

Slide 51 text

@adunkman document.cookie

Slide 52

Slide 52 text

In document.cookie = cookieName + "=" + encodeURIComponent(json);

Slide 53

Slide 53 text

In Out document.cookie = cookieName + "=" + encodeURIComponent(json); var regex = new RegExp( "(?:(?:^|.*;)\\s*" + n + "\\s*\\=\\s*([^;]*).*$)|^.*$");

Slide 54

Slide 54 text

In Out Detect document.cookie = cookieName + "=" + encodeURIComponent(json); var regex = new RegExp( "(?:(?:^|.*;)\\s*" + n + "\\s*\\=\\s*([^;]*).*$)|^.*$"); setInterval(checkForChanges, pollingInterval);

Slide 55

Slide 55 text

@adunkman Please don’t.

Slide 56

Slide 56 text

@adunkman document.cookie localStorage

Slide 57

Slide 57 text

@adunkman QuotaExceededError

Slide 58

Slide 58 text

@adunkman document.cookie localStorage

Slide 59

Slide 59 text

In localStorage.setItem( keyName, json);

Slide 60

Slide 60 text

In Out localStorage.setItem( keyName, json); localStorage.getItem( keyName);

Slide 61

Slide 61 text

In Out Detect localStorage.setItem( keyName, json); localStorage.getItem( keyName); window.addEventListener( “storage", function (event) { ...

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

var tabId = Math.random(); video.addEventListener("play", function () { localStorage.setItem("playing_tab", tabId); }); window.addEventListener("storage", function (event) { var isThisKey = event.key === "playing_tab"; var notThisTab = event.newValue !== tabId; if (isThisKey && notThisTab) { video.pause(); } });

Slide 64

Slide 64 text

var tabId = Math.random(); video.addEventListener("play", function () { localStorage.setItem("playing_tab", tabId); }); window.addEventListener("storage", function (event) { if (event.key === "playing_tab" && event.newValue !== tabId) { video.pause(); } });

Slide 65

Slide 65 text

var tabId = Math.random(); video.addEventListener("play", function () { localStorage.setItem("playing_tab", tabId); }); window.addEventListener("storage", function (event) { if (event.key === "playing_tab" && event.newValue !== tabId) { video.pause(); } }); 9

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

@adunkman Between Tabs Outside Tabs

Slide 68

Slide 68 text

@adunkman WebWorker

Slide 69

Slide 69 text

@adunkman SharedWorker

Slide 70

Slide 70 text

@adunkman Tab 1 Tab 2 Tab 3 worker.js 1 worker.js 2 worker.js 3

Slide 71

Slide 71 text

@adunkman Tab 1 Tab 2 Tab 3 worker.js 1

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

%

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

External Page

Slide 79

Slide 79 text

External Page Harvest Script

Slide 80

Slide 80 text

External Page Harvest Script

Slide 81

Slide 81 text

External Page Harvest Script

Slide 82

Slide 82 text

External Page Harvest Script SharedWorker

Slide 83

Slide 83 text

External Page Harvest Script SharedWorker WebSocket

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

@adunkman SharedWorker

Slide 92

Slide 92 text

@adunkman Great for sharing processing and resources Not great for 
 working in Safari

Slide 93

Slide 93 text

@adunkman SharedWorker

Slide 94

Slide 94 text

@adunkman ServiceWorker

Slide 95

Slide 95 text

@adunkman Service workers essentially act as proxy servers that sit between web applications, and the browser and network (when available). … They will also allow access to push notifications and background sync APIs. –MDN

Slide 96

Slide 96 text

@adunkman Service workers essentially act as proxy servers that sit between web applications, and the browser and network (when available). … They will also allow access to push notifications and background sync APIs. –MDN

Slide 97

Slide 97 text

@adunkman A service worker is an event- driven worker registered against an origin and a path. –MDN

Slide 98

Slide 98 text

@adunkman A service worker is an event- driven worker registered against an origin and a path. –MDN

Slide 99

Slide 99 text

@adunkman Service workers are a new browser feature that provide event-driven scripts that run independently of web pages. –W3C

Slide 100

Slide 100 text

@adunkman Service workers are a new browser feature that provide event-driven scripts that run independently of web pages. –W3C

Slide 101

Slide 101 text

@adunkman A service worker is a process which is tied to domain events rather than to a browsing context. –me

Slide 102

Slide 102 text

@adunkman Tab 1 worker.js 1 Domain Events worker.js 1

Slide 103

Slide 103 text

@adunkman Domain Events worker.js 1 push sync fetch

Slide 104

Slide 104 text

@adunkman “push” event

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

@adunkman Notification Service

Slide 114

Slide 114 text

Page navigator.serviceWorker.register("sw.js") .then(function (reg) { console.log("success", reg); }).catch(function (err) { console.log("fail", err); });

Slide 115

Slide 115 text

Page navigator.serviceWorker.register("sw.js") .then(function (reg) { console.log("success", reg); }).catch(function (err) { console.log("fail", err); }); Tab 1 worker.js 1

Slide 116

Slide 116 text

Page navigator.serviceWorker.register("sw.js") .then(function (reg) { console.log("success", reg); }).catch(function (err) { console.log("fail", err); }); Tab 1 worker.js 1 (new version)

Slide 117

Slide 117 text

Page navigator.serviceWorker.register("sw.js") .then(function (reg) { console.log("success", reg); }).catch(function (err) { console.log("fail", err); }); Tab 1 worker.js 1 Tab 2 (new version)

Slide 118

Slide 118 text

Page navigator.serviceWorker.register("sw.js") .then(function (reg) { console.log("success", reg); }).catch(function (err) { console.log("fail", err); }); Tab 1 worker.js 1 Tab 2 (old) worker.js 1 (new version)

Slide 119

Slide 119 text

Page navigator.serviceWorker.register("sw.js") .then(function (reg) { console.log("success", reg); }).catch(function (err) { console.log("fail", err); });

Slide 120

Slide 120 text

Page navigator.serviceWorker.register("sw.js") .then(function (reg) { console.log("success", reg); }).catch(function (err) { console.log("fail", err); }); Tab 2 (new) worker.js 1

Slide 121

Slide 121 text

Page navigator.serviceWorker.register("sw.js") .then(function (reg) { console.log("success", reg); }).catch(function (err) { console.log("fail", err); }); Worker self.addEventListener("install", function (event) { self.skipWaiting(); });

Slide 122

Slide 122 text

Worker Page navigator.serviceWorker.register("sw.js") .then(function (reg) { console.log("success", reg); }).catch(function (err) { console.log("fail", err); }); self.addEventListener("push", function (event) { console.log("message", event); }); self.addEventListener("install", function (event) { self.skipWaiting(); });

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

@adunkman “push” event

Slide 125

Slide 125 text

@adunkman Browser message server (like native app) Wakes ServiceWorker to process event

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

@adunkman “sync” event

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

No content

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

Worker Page navigator.serviceWorker.ready .then(function (reg) { return reg.sync.register("syncTime"); }); self.addEventListener("sync", function (event) { if (event.tag === "syncTime") { event.waitUntil(stuff()); } });

Slide 135

Slide 135 text

Tab 1 worker.js 1

Slide 136

Slide 136 text

Tab 1 worker.js 1 sync “syncTime”

Slide 137

Slide 137 text

Tab 1 worker.js 1 sync “syncTime” Browser

Slide 138

Slide 138 text

worker.js 1 Browser

Slide 139

Slide 139 text

worker.js 1 Browser

Slide 140

Slide 140 text

@adunkman “sync” event

Slide 141

Slide 141 text

@adunkman Great for backgrounding a sync process, even in limited or no connectivity

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

@adunkman “fetch” event

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

Worker self.addEventListener("fetch", function (event) { console.log(event.request.url); event.respondWith( caches.match(event.request) .then(function (response) { if (response) { return response; } return fetch(event.request) .then(function (response) { return response; }).catch(function (error) { throw error; }); }) ); });

Slide 147

Slide 147 text

Worker self.addEventListener("fetch", function (event) { console.log(event.request.url); event.respondWith( caches.match(event.request) .then(function (response) { if (response) { return response; } return fetch(event.request) .then(function (response) { return response; }).catch(function (error) { throw error; }); }) ); });

Slide 148

Slide 148 text

Worker self.addEventListener("fetch", function (event) { console.log(event.request.url); event.respondWith( caches.match(event.request) .then(function (response) { if (response) { return response; } return fetch(event.request) .then(function (response) { return response; }).catch(function (error) { throw error; }); }) ); });

Slide 149

Slide 149 text

Worker self.addEventListener("fetch", function (event) { console.log(event.request.url); event.respondWith( caches.match(event.request) .then(function (response) { if (response) { return response; } return fetch(event.request) .then(function (response) { return response; }).catch(function (error) { throw error; }); }) ); });

Slide 150

Slide 150 text

Worker self.addEventListener("fetch", function (event) { console.log(event.request.url); event.respondWith( caches.match(event.request) .then(function (response) { if (response) { return response; } return fetch(event.request) .then(function (response) { return response; }).catch(function (error) { throw error; }); }) ); });

Slide 151

Slide 151 text

Worker self.addEventListener("fetch", function (event) { console.log(event.request.url); event.respondWith( caches.match(event.request) .then(function (response) { if (response) { return response; } return fetch(event.request) .then(function (response) { return response; }).catch(function (error) { throw error; }); }) ); });

Slide 152

Slide 152 text

@adunkman “fetch” event

Slide 153

Slide 153 text

@adunkman Programmatic access to browser cache Replaces AppCache Allows custom offline logic Is completely encapsulated

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

@adunkman push, sync, fetch

Slide 157

Slide 157 text

@adunkman ServiceWorker

Slide 158

Slide 158 text

@adunkman HTTPS/localhost only Not persistent Not tied to browser context Domain events

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

@adunkman There’s more to the web than web pages

Slide 161

Slide 161 text

getharvest.com/careers dunkman.me/talks/tabs @adunkman