Workbench / The TiffinBox Build
S31 · V2·31
Ship Without Breaking Open Tabs
Volume 2, chapter 31. Until now a deploy meant replacing files in web/ while kitchen tablets kept old pages open, and every visit downloaded every script again. This stage turns a build into releases: content-hashed assets that are cached for a year, an asset map the server applies to the pages it renders, and a symlink switched with one rename, so a deploy and a rollback are atomic under load. The first version passed every test except the atomic one: a rollback brought back a release that had never heard of the newer one's files (11 failed loads in 622). nginx in front adds TLS, HSTS and HTTP/2, which lifts the six-connection limit that left two of eight kitchen displays unable even to load, and the stream now tells proxies not to buffer it, after nginx's default held every event back.
Stage 31: ship without breaking open tabs
The brief
What you'll build.
web-tests/release.mjs(npm run release -- DIST): bundles the plain views with shared chunks, names every script and style by its content, writesasset-map.json, a rewrittenindex.htmland asw.jsthat knows its release, and switchesDIST/currentto the new release withrename(2);--rollbackand--list;- the server: a web root that may be a symlink (resolved on each request, so a switch needs no restart), the release's asset map applied to every page it renders, and
Cache-Control: public, max-age=31536000, immutablefor hashed assets; - the service worker: a cache named after the release, its assets precached at install, cache-first for hashed files;
deploy/nginx.conf.inanddeploy/run-nginx.sh: TLS (TLS 1.3 and 1.2), HTTP/2, HSTS, hashed assets served from disk, everything else proxied, the stream unbuffered; the server sendsX-Accel-Buffering: noon the stream as well;- tests:
suite/test_deploy.py(4): immutable hashed assets, an open tab surviving a deploy and a rollback, atomic switching under load, the precached release.
Why a real company needs this. A deploy that breaks the pages people already have open shows up as errors nobody can reproduce ("it worked after I refreshed"). A kitchen tablet keeps its page open all day, so for TiffinBox it's the normal case, not the edge case. And without immutable assets every visit pays for every script again: on a phone network that's the slowest part of opening the app.
Why now. Chapter 31 is frontend deployment: static hosting and CDNs, SSR hosting, reverse proxies and TLS, immutable assets, cache invalidation, configuration, rollouts and rollbacks. Stage 30 produced a reproducible build with a manifest; this stage turns it into something deployable. Stage 17 left the six-connection limit for HTTP/2; stage 28 left HTTPS and HSTS; stage 30 left deprecated contract fields waiting for versioned assets.
What you'll have at the end.
- releases that can be switched and rolled back atomically, with open tabs unaffected;
- repeat visits that download no unchanged scripts;
- TLS and HTTP/2 at the edge;
- ADR-0033: releases and the edge.
Theory to own
- Immutable assets. Why is a content hash in the file name what makes a one-year cache safe? (The chapter, "Immutable assets"; Companion, Explain it differently 1.)
- Atomic switches. What does "atomic" mean for a deploy, and how does a symlink and
renamegive it? (The chapter's lab, Experiment 3; Companion, Gaps 1.) - Old tabs. What does a page loaded before a deploy still need? (The chapter's lab, Experiment 2.)
- Proxies and streams. What does a reverse proxy do to a response that never ends? (Companion, Gaps 2.)
- HTTP/1.1 connection limits. Why can a seventh tab not even load, and what does HTTP/2 change? (Companion, Gaps 3.)
Build it
Step 1: a release, built
Write release.mjs. Bundle web/kitchen.js and web/search.js with esbuild (splitting: true, entryNames: '[name]-[hash]', chunkNames: 'chunk-[hash]'); copy the React and Angular bundles and app.css under content-hashed names; write the asset map, the rewritten index.html and sw.js with self.TB_RELEASE = { version, precache } in front. The release id is a hash of the asset map. Build the same release from two directories and compare names.
Checkpoint. Two builds from different directories produce the same release id and file names.
Step 2: the server serves releases
Stop resolving the web root once at startup: resolve it on each request. Read asset-map.json from the resolved root (cached by its real path) and replace ="/kitchen.js"-style references in every HTML response. Give hashed asset paths the one-year immutable header; keep no-cache for everything else.
Checkpoint. Pages name /assets/… files; those files are immutable; /kitchen.js is a 404; a second visit downloads nothing it already has.
Step 3: atomic switches and rollbacks
Make each release carry the assets of the releases before it (hard links), and switch with a temporary symlink renamed over current. Write test_deploy.py: an open tab survives a deploy and a rollback, and four threads load pages and every asset they name while the release switches ten times.
Checkpoint. 0 failures under load.
Step 4: the edge
Write the nginx template and run-nginx.sh (a self-signed certificate for localhost and 127.0.0.1, a config in a temporary directory). Serve /assets/ from DIST/current with the immutable header, nosniff and CORP repeated in that location (add_header there replaces the server-level ones), HSTS everywhere, and proxy the rest. Measure the stream through nginx with buffering on and off, and eight displays over HTTP/1.1 and HTTP/2.
Checkpoint. The stream is immediate through nginx; eight displays are live over HTTP/2.
Senior and architect lens
1. Deploying is two decisions: what's available, and what's current. Uploading files makes them available; switching a pointer makes them current. Keeping the two separate is what makes deploys atomic, rollbacks instant, and old tabs safe.
2. ADR-0033: releases and the edge. Write it with these points:
- Assets: content-hashed names from a reproducible build;
immutablefor a year; never overwritten; kept for the last five releases. - Documents: HTML, the asset map and
sw.jsareno-cacheorno-store; nonced pages never cached (stage 28). - Switching: one symlink, replaced with
rename; every release has the assets of all kept releases before it becomes current; rollback is the same operation. - Edge: TLS 1.3 and 1.2, HTTP/2, HSTS; streams unbuffered (configuration and
X-Accel-Buffering); security headers repeated whereadd_headerreplaces them. - Not yet: a CDN (the same headers make one safe to add), HSTS preload,
Secureand__Host-cookies (the theme cookie, with staff sessions in Volume 3).
3. Rollback must be tested like a deploy. The forward path worked first time; the backward one didn't. Most teams test only the forward path, and find the backward one during an incident.
4. The service worker is part of the release. It precaches the release's assets at install, so a new release is complete offline before it takes over, and its cache is named after the release so old caches go when it activates.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| carried assets accumulate | a release's assets/ keeps links to releases already pruned |
stage 33 (prune carried files older than the kept releases) |
| one server, one disk | releases exist on one machine; no CDN | Volume 7 (object storage and a CDN, same headers) |
| deprecated contract fields | old scripts can now be told apart (by asset name), but nobody measures which are still in use | stage 32 (the RUM beacon reports the release) |
| self-signed certificate | real browsers would refuse it | Volume 6 (ACME certificates, renewal) |
no Secure cookies yet |
the theme cookie can travel over plain HTTP | Volume 3 (sessions; __Host- names, expand-then-contract for the rename) |
Evidence
test_deploy.pypasses (4 tests), including0 failuresduring 10 switches; the whole suite passes (43 passed).- Two directories build the same release id.
- The second-visit and HTTP/2 measurements are in the README.
- 36 server tests and
check-js.shpass. - ADR-0033 is committed.
Carry forward
- The release id goes into stage 32's beacon, so errors and timings are reported per release.
release.mjsjoins stage 33's release check, afterrepro.sh.- The asset map is where stage 32 finds which source map belongs to which file.
- nginx is where Volume 3's API gateway concerns (rate limits, request ids) start.