The Web FrontendDeeper

Kayavak / The Web Frontend / FE·29 PWAs, Workers & WebAssembly / Deeper

Deeper · The Web Frontend

PWAs, Workers & WebAssembly

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Web pages were only usable online, and only on one thread. Each limitation got a first attempt that failed and a second that stuck. AppCache (about 2009) tried to make pages offline with a declarative manifest and became known for its traps; the Service Worker (shipped in Chrome in 2015) replaced it with a programmable proxy between the page and the network. In 2015 Alex Russell and Frances Berriman coined progressive web apps for sites that use it to install and work offline like apps. Web Workers (around 2009) gave pages background threads with no shared memory.

For running other languages at near-native speed, Mozilla's asm.js (2013) showed that a strict subset of JavaScript could be compiled ahead of time very efficiently. Browser vendors then designed WebAssembly together: a compact, sandboxed binary format, shipped in all major browsers in 2017 and a W3C Recommendation in 2019. It's how this library runs Python, SQL and Go in your browser.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

The browser gives you three extra powers, each with walls: a service worker that sits between your pages and the network, workers that run code off the main thread without sharing memory, and WebAssembly that runs compiled code in a sandbox.

Question Answer
What is it? Service workers (offline, caching, push), Web Workers (parallelism), WebAssembly (compiled code in the browser)
Problem it solves Apps that work offline, stay responsive under heavy computation, and reuse code written in other languages
Parts SW lifecycle and caching strategies, workers and message passing, Wasm modules with linear memory and imports
How it works The SW intercepts requests per scope; workers exchange structured-cloned or transferred data; Wasm calls only what it's given
Assumptions Messages are the only channel between threads; Wasm has no access to anything not imported
When they change SharedArrayBuffer adds shared memory, so it requires cross-origin isolation (a Spectre defence)
Where it fails Service worker updates can leave old code serving for a while; copying big data between threads is slow
Related ideas Caching (ch19); processes and threads (Foundations, ch08); compilers (Foundations, ch10)

What if?

What if a service worker cached everything, forever, cache-first?

Reason it through, then open

The app would load instantly offline, and never update: users would keep the version they first saw until they cleared their data, including your bugs. That's why HTML is usually network-first and only content-hashed assets are cached "forever" (a new version gets a new name), and why service workers need a versioned cache they clean up on activation.

What if WebAssembly could call any browser API directly?

Reason it through, then open

Compiled code from anywhere would have the page's full powers with none of the visibility JavaScript has; auditing what a module can do would mean reverse-engineering it. Wasm can only call functions its host explicitly imports into it, so the page decides its capabilities. That design is also what makes Wasm useful outside browsers, for plug-ins and sandboxed server code.

Where the boundary is

Aspect Web Worker Service Worker
Purpose Parallel computation for one page A network proxy and background agent for a whole scope
Lives As long as its page keeps it Independently of pages; started by events
Can intercept requests No Yes
Typical use Parsing, image processing, running Python Offline support, caching, push notifications

How sure are we?

Claim How it's known Where from
"Progressive web apps" coined in 2015 Historical Russell's essay, June 2015
WebAssembly shipped in all major browsers in 2017 Historical Browser release notes
Wasm runs close to native speed Measured, conditionally Often within about 1.5–2× of native in studies; varies by workload
Workers share no memory by default Convention HTML specification; SharedArrayBuffer is the opt-in exception

Still open

WebAssembly is gaining garbage collection, threads and a component model, which may make it a general compilation target for higher-level languages. How much installed PWAs can do on iOS compared with native apps keeps changing with platform policy.

What would change your mind? If shipping a native app became as cheap to distribute as a URL, PWAs' main advantage would shrink. App stores still add review, fees and installation friction.

Recall

Why do service workers usually fetch HTML network-first?

So users get updates; only content-hashed assets are safe to serve cache-first forever.

How do Web Workers communicate with the page?

By message passing (structured clone or transfer), not shared memory by default.

What can a WebAssembly module access?

Only its own linear memory and the functions its host imports into it.

What's the difference between a Web Worker and a service worker?

A Web Worker does parallel computation for a page; a service worker intercepts network requests for a scope.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.