BEYOND PROGRESSIVE WEB APPS WITH SERVICE WORKERS @TrentMWillis
#ThunderPlains
Slide 2
Atwood’s Law
Slide 3
Atwood’s Law “Any application that can be written in JavaScript, will eventually be written in JavaScript.” blog.codinghorror.com/the-principle-of-least-power
Slide 4
Slide 5
Slide 6
Slide 7
Slide 8
PROGRESSIVE WEB APPS
Slide 9
BEYOND PROGRESSIVE WEB APPS WITH SERVICE WORKERS
Slide 10
@TrentMWillis
Slide 11
Love The Web. Love JavaScript. @TrentMWillis
#ThunderPlains
Slide 12
JavaScript continues to evolve. @TrentMWillis
#ThunderPlains
Slide 13
Web Hypertext Application Technology Working Group @TrentMWillis
#ThunderPlains
Slide 14
WHATWG
@TrentMWillis
#ThunderPlains
Slide 15
WHATWG Streams
@TrentMWillis
#ThunderPlains
Slide 16
WHATWG Streams “If installed inside the fetch hook of a service worker, this would allow developers to transparently polyfill new image formats.” streams.spec.whatwg.org
@TrentMWillis
#ThunderPlains
Slide 17
Idea #1: Transparently Polyfill New File Formats @TrentMWillis
#ThunderPlains
Slide 18
New Image Format
@TrentMWillis
#ThunderPlains
Slide 19
New Image Format
<img src=“./my-image.nif”> *Can Display PNG/JPEG/GIF, Not NIF
@TrentMWillis
#ThunderPlains
Slide 20
New Image Format
Proxy
Service Worker
<img src=“./my-image.nif”> *Can Display PNG/JPEG/GIF, Not NIF
@TrentMWillis
#ThunderPlains
Slide 21
New Image Format .nif
Service Worker .gif
<img src=“./my-image.nif”> *Can Display PNG/JPEG/GIF, Not NIF
@TrentMWillis
#ThunderPlains
Slide 22
All code will be available online @TrentMWillis
#ThunderPlains
Occurs for every request on the page // service-worker.js self.addEventListener(‘fetch’, (event) => {; }); Lots of details and methods…
@TrentMWillis
#ThunderPlains
Slide 25
// service-worker.js self.addEventListener(‘fetch’, (event) => {; event.respondWith(); });
@TrentMWillis
Used to implement offline functionality
#ThunderPlains
const getPortToWebWorkerFromClient = async (clientId) => { const client = await self.clients.get(clientId); return new Promise(resolve => { client.postMessage({}); }); }; Ask the page how to communicate with the web worker
@TrentMWillis
#ThunderPlains
const webWorker = new Worker(‘web-worker.js’); navigator.serviceWorker.onmessage = () => {
This will let the SW talk to the WW!
const channel = new MessageChannel(); const serviceWorker = navigator.serviceWorker.controller; serviceWorker.postMessage({port: channel.port2}, [channel.port2]); webWorker.postMessage({port: channel.port1}, [channel.port1]); };
@TrentMWillis
#ThunderPlains
Data Chunk
Data Chunk Modified Chunk
@TrentMWillis
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Modified Chunk
#ThunderPlains
Slide 78
Data Chunk
Data Chunk Modified Chunk
@TrentMWillis
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Modified Chunk
#ThunderPlains
Slide 79
Data Chunk
Data Chunk Modified Chunk
@TrentMWillis
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Modified Chunk
#ThunderPlains
Slide 80
Data Chunk
Data Chunk Modified Chunk
@TrentMWillis
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Modified Chunk
#ThunderPlains
Slide 81
Original Data Data Chunk
Data Chunk Modified Chunk
@TrentMWillis
Data Chunk Modified Chunk
Modified Data Data Chunk
Modified Chunk
Data Chunk Modified Chunk
Data Chunk Modified Chunk
Modified Chunk
#ThunderPlains
Idea #1: Transparently Polyfill New File Formats Idea #2: Compile Code At Runtime Idea #3: Process Data Off The Main Thread Idea #4: Process Data As It Streams @TrentMWillis
#ThunderPlains
Slide 92
Service Workers, Streams, MessageChannels, Web Workers, Caches, Web Assembly…
@TrentMWillis
#ThunderPlains
Slide 93
Service Workers, Streams, MessageChannels, Web Workers, Caches, Web Assembly…these are all just tools to build things, so go build something!
@TrentMWillis
#ThunderPlains
Slide 94
EXPERIMENT WITH JAVASCRIPT AND HAVE FUN! *ALSO, USE GLITCH
glitch.com/@trentmwillis/fun-with-service-workers
@TrentMWillis
#ThunderPlains