Number server highlighting's ADR 0027 and tighten the web's byte limits
Before adoption: this commit came in with the history the project adopted, so no landing records it.
The web's JavaScript measured 2,774,689 and its entry 309,341 once the minified build, Workers Cache's entry and server highlighting had landed: limits 3,191,000 and 356,000, 15% above.
authored by oscabriel · committed by oscabriel · parent 51a1ca5 · Browse files · 10 files·+45 added−43 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+45 added−43 removed
- tests:+4 added−4 removed
- other:+41 added−39 removed
Provenance
- Commit
fe7313d- Tree
4123dbf- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent
51a1ca5
10 files changed+45 added−43 removed
modified:apps/web/src/features/code/blob-view.tsx+1 added-1 removed
3 unmodified lines4567889101112173 unmodified linesExpand all3 unmodified lines * context bar: the path once, the file in words, Code | Why, Raw, * Permalink (`y`) and Copy. The code takes the pane's whole width and * scrolls sideways inside itself, never wrapping: the server sends it * highlighted where it highlights its language and size (`ServerCode`, * ADR 0026), else as numbered, anchored plain text (`NumberedLines`), and * ADR 0027), else as numbered, anchored plain text (`NumberedLines`), and * the browser's drawing (`code`, from `client-code.tsx`) replaces it in * place. * * A Markdown file is rendered (its source on `?plain=1`), an image is shown173 unmodified linesExpand allmodified:apps/web/src/features/code/plain-diff.tsx+1 added-1 removed
5 unmodified lines6 * 13 px code on 20 px lines, line numbers in a gutter of at least three7 * figures with two before and one after, a sign in the first of two8 * leading columns, unchanged runs as 32 px "N unmodified lines" bars. So9 * the highlighting arrives in place and nothing moves. The first files the10- * server highlights (`prerenderedPaths`, ADR 0026) are its drawing instead10+ * server highlights (`prerenderedPaths`, ADR 0027) are its drawing instead11 * of plain lines, under the same header.12 *13 * Each file is a disclosure whose summary is its header: folded, only the14 * header shows (an added or deleted file past 80 lines starts folded).337 unmodified linesmodified:apps/web/src/features/code/prerender.ts+1 added-1 removed
45 unmodified lines46 export type ServerLanguage = (typeof SERVER_LANGUAGES)[number];47 48 /**49 * The most lines the server highlights for one page. Measured in the dev50- * world's workerd (ADR 0026), warm: 2,400 lines of TypeScript took about50+ * world's workerd (ADR 0027), warm: 2,400 lines of TypeScript took about51 * 315 ms, 600 about 85 ms and 400 about 57 ms, so a page spends at most52 * some 60 ms highlighting, and a longer file is sent plain.53 */54 export const SERVER_LINES = 400;217 unmodified linesmodified:apps/web/test/code-prerender.test.tsx+1 added-1 removed
1 /**2- * Code the server sends highlighted (ADR 0026): a blob page's file and a2+ * Code the server sends highlighted (ADR 0027): a blob page's file and a3 * commit page's first files, drawn by `@pierre/diffs/ssr` into a4 * declarative shadow root, in a fixed set of languages and within the5 * server's own limit; anything else is sent as plain text, as before.6 *363 unmodified linesmodified:apps/web/test/production-bundle.test.ts+2 added-2 removed
12 unmodified lines13 * renderer, no grammar and nothing of the server's highlighting, and stays14 * under a size ceiling, and the hashed assets it serves are kept by15 * browsers for good (`public/_headers`). The Worker bundles the grammars16 * it highlights in and no others, none on its entry, and no theme17- * collection or WebAssembly (ADR 0026).17+ * collection or WebAssembly (ADR 0027).18 *19 * Modules are read from the build's source maps (each map's `sources`),20 * strings from every other file it wrote. The Cloudflare plugin rewrites21 * `.wrangler/deploy/config.json` at each build to point at its output; the49 unmodified lines71 const GRAMMAR = /node_modules\/@shikijs\/langs\/dist\/(?<name>[\w-]+)\.mjs$/u;72 73 /**74 * The grammars the Worker highlights in (`features/code/prerender-server.ts`,75- * ADR 0026), by Shiki's module names: HTML brings JavaScript and CSS with it.75+ * ADR 0027), by Shiki's module names: HTML brings JavaScript and CSS with it.76 */77 const SERVER_GRAMMARS = [78 "css",79 "html",401 unmodified linesmodified:docs/adr/0019-the-web-reads-repos-through-public-contracts.md+1 added-1 removed
2 unmodified lines3 ---4 5 # The web reads a project's code through public contracts the platform serves6 7-> **Superseded in part (2026-10-10).** Code is no longer drawn in the browser only. ADR 0026 records that the web Worker highlights a blob page's file and a commit page's first files on the server, in a fixed set of languages and within its own limit, and that the browser's drawing takes their place once it can draw the same code at once. The tree is still drawn in the browser only. The record below is kept as it was decided.7+> **Superseded in part (2026-10-10).** Code is no longer drawn in the browser only. ADR 0027 records that the web Worker highlights a blob page's file and a commit page's first files on the server, in a fixed set of languages and within its own limit, and that the browser's drawing takes their place once it can draw the same code at once. The tree is still drawn in the browser only. The record below is kept as it was decided.8 9 The web's code browser (tree, file, raw bytes, log, commit, refs, and why each line is there) reads only through public, read-only contracts on `/rpc`. The platform Worker serves them over its existing `RepoHost` on the public Artifacts namespace. The logic lives in one host-free module, `@repo/project/code`, which the platform's handlers and the web's offline dev world both call. No payload names a repo. Each names a project, and the module reads that project's canonical repo only, never a fork or a layer. A revision is `main` (the project's landed SHA, from its record and never from a ref), a full commit SHA in the canonical repo, or a tag name matched exactly against the port's tag list, never joined into a ref path. Every answer names the commit it resolved, so any page becomes a permalink.10 11 ## Considered Options17 unmodified linesdeleted:docs/adr/0026-code-is-highlighted-on-the-server-in-a-fixed-set-of-languages.md-31 removed
1----2-status: accepted3----4-5-# Code is highlighted on the server in a fixed set of languages6-7-This amends ADR 0019, which drew code in the browser only. A blob page's file and a commit page's first files now arrive highlighted in the document: the web Worker draws them with `@pierre/diffs/ssr`, using the browser's own options (`apps/web/src/features/code/pierre.ts`), and sends each drawing as a declarative shadow root on Pierre's `diffs-container`. The browser paints it with the document, before any script. The browser's own drawing (`File`, or the commit's `CodeView`) still takes over, with line selection, the address's `#L` and the worker pool, but only once it is ready to draw the same code highlighted at once: its module loaded, the pool up, and the code highlighted in the pool first (`prepareFile`, `prepareDiffs`). It then replaces the server's drawing in one render (`apps/web/src/features/code/client-code.tsx`), with every line in the same place. Code the server does not highlight still arrives as plain text and changes once, to highlighted.8-9-The server highlights only within its own limits (`apps/web/src/features/code/prerender.ts`):10-11-- **Languages.** TypeScript, TSX, JavaScript (with `.mjs` and `.cjs`), JSX, JSON, Markdown, YAML, TOML, shell, CSS and HTML, by the names Pierre gives a file's type. Any other language is sent as numbered plain text, as before, and the browser highlights it.12-- **Lines.** At most 400 lines a page. A diff counts both sides, since Pierre highlights both whole files. A commit page considers at most its first 8 files that start open (an added or deleted file past 80 lines starts folded), the ones a reader sees first. `CodeView` highlights the rest as they scroll into view.13-- **The drawing's own limits.** A file past `tokenizeMaxLength`, or with any line past `tokenizeMaxLineLength` (1,000, the same as `maxLineDiffLength`), is sent plain, so a minified file is never tokenized on the server.14-15-Both sides decide from the page's own data, so the browser knows which code arrived highlighted without being told. The Worker runs Shiki's JavaScript regex engine, since a Worker may not compile WebAssembly. Its build resolves `shiki` to core alone (`apps/web/src/server/shiki.ts`) and `@pierre/theming/themes` to an empty collection (`apps/web/src/server/pierre-themes.ts`), through a plugin in `apps/web/vite.config.ts`. So it bundles the eleven grammars it names, as chunks off its entry, and no other grammar, theme or WebAssembly. Pierre will not resolve a grammar or a theme by name in a Worker's global scope (`isWorkerContext`), and `registerCustomLanguage` only registers a loader for that resolution. The server therefore loads each grammar and the token theme itself and attaches them to Pierre's shared highlighter (`apps/web/src/features/code/prerender-server.ts`).16-17-## Considered Options18-19-- **Keep drawing in the browser only** (ADR 0019 as it stood). The plain text already matched Pierre's measures, so nothing moved. But the code changed colour after hydration, and that swap was the largest paint on code pages.20-- **Highlight on the platform and keep the HTML in R2.** Each drawing would be made once for good. But it would need a new public read for the HTML, and every Worker reading it would take a hop on every code page.21-- **Every grammar on the server.** About 10.7 MB of grammars, far past the web's byte budget.22-- **Ship the HTML in the loader's data.** The browser could then adopt it on any page, but the document would carry every drawing twice: once as markup and once as a string in the router's state. Instead the loader draws the code ahead, the drawing is kept by its blob ids until the render reads it, and the browser needs nothing but the markup.23-24-## Consequences25-26-- **Bytes.** The web Worker's JavaScript grows from 1,528,378 to 2,773,744 bytes (budget 4,075,000), about 940 KB of it grammars. Its entry stays at 308,418 (budget 801,000): the grammars and `@pierre/diffs/ssr` load by dynamic import on a page's first highlighted code. `apps/web/test/production-bundle.test.ts` holds the Worker to exactly these grammars, none on its entry, and no theme collection or Oniguruma. It also holds the chunks every page preloads to no grammar and nothing of this module.27-- **CPU.** Measured in the dev world's workerd, highlighting TypeScript takes about 0.14 ms a line warm: 2,400 lines about 315 ms, 600 about 85 ms, 400 about 57 ms, 100 about 18 ms. A fresh isolate's first 400-line file took 180 ms, which includes making the highlighter and compiling the grammar. 2,400 lines would cost hundreds of milliseconds, so the server's limit is 400 lines, well below the browser's.28-- **Document size.** A highlighted line costs about 700 to 1,600 bytes of markup, depending on how many tokens it holds: a 400-line TypeScript file is about 640 KB. Each drawing also carries Pierre's stylesheet and icons (about 48 KB). Both compress well: an 11-line file's page is 100 KB raw and 16 KB gzipped.29-- **Cache.** The drawings are kept in the isolate (`prerender.ts`): at most 24, and 8,000,000 characters of HTML, keyed by blob ids and path, which never change what they name. A file read again is not drawn again. A loader that finds its drawing evicted before the render makes it again, and the render streams it.30-- **The page.** A page reached by a link, not loaded, has no server drawing. It shows the plain text, which the browser's drawing then replaces. A split diff (`?style=split` on a wide screen) is plain until drawn, as before, since the server draws unified only.31-- **Without script.** A highlighted file has no `#L` anchors: Pierre's lines live in a shadow root, which a fragment cannot reach. Plain text keeps its anchors.added:docs/adr/0027-code-is-highlighted-on-the-server-in-a-fixed-set-of-languages.md+31 added
1+---2+status: accepted3+---4+5+# Code is highlighted on the server in a fixed set of languages6+7+This amends ADR 0019, which drew code in the browser only. A blob page's file and a commit page's first files now arrive highlighted in the document: the web Worker draws them with `@pierre/diffs/ssr`, using the browser's own options (`apps/web/src/features/code/pierre.ts`), and sends each drawing as a declarative shadow root on Pierre's `diffs-container`. The browser paints it with the document, before any script. The browser's own drawing (`File`, or the commit's `CodeView`) still takes over, with line selection, the address's `#L` and the worker pool, but only once it is ready to draw the same code highlighted at once: its module loaded, the pool up, and the code highlighted in the pool first (`prepareFile`, `prepareDiffs`). It then replaces the server's drawing in one render (`apps/web/src/features/code/client-code.tsx`), with every line in the same place. Code the server does not highlight still arrives as plain text and changes once, to highlighted.8+9+The server highlights only within its own limits (`apps/web/src/features/code/prerender.ts`):10+11+- **Languages.** TypeScript, TSX, JavaScript (with `.mjs` and `.cjs`), JSX, JSON, Markdown, YAML, TOML, shell, CSS and HTML, by the names Pierre gives a file's type. Any other language is sent as numbered plain text, as before, and the browser highlights it.12+- **Lines.** At most 400 lines a page. A diff counts both sides, since Pierre highlights both whole files. A commit page considers at most its first 8 files that start open (an added or deleted file past 80 lines starts folded), the ones a reader sees first. `CodeView` highlights the rest as they scroll into view.13+- **The drawing's own limits.** A file past `tokenizeMaxLength`, or with any line past `tokenizeMaxLineLength` (1,000, the same as `maxLineDiffLength`), is sent plain, so a minified file is never tokenized on the server.14+15+Both sides decide from the page's own data, so the browser knows which code arrived highlighted without being told. The Worker runs Shiki's JavaScript regex engine, since a Worker may not compile WebAssembly. Its build resolves `shiki` to core alone (`apps/web/src/server/shiki.ts`) and `@pierre/theming/themes` to an empty collection (`apps/web/src/server/pierre-themes.ts`), through a plugin in `apps/web/vite.config.ts`. So it bundles the eleven grammars it names, as chunks off its entry, and no other grammar, theme or WebAssembly. Pierre will not resolve a grammar or a theme by name in a Worker's global scope (`isWorkerContext`), and `registerCustomLanguage` only registers a loader for that resolution. The server therefore loads each grammar and the token theme itself and attaches them to Pierre's shared highlighter (`apps/web/src/features/code/prerender-server.ts`).16+17+## Considered Options18+19+- **Keep drawing in the browser only** (ADR 0019 as it stood). The plain text already matched Pierre's measures, so nothing moved. But the code changed colour after hydration, and that swap was the largest paint on code pages.20+- **Highlight on the platform and keep the HTML in R2.** Each drawing would be made once for good. But it would need a new public read for the HTML, and every Worker reading it would take a hop on every code page.21+- **Every grammar on the server.** About 10.7 MB of grammars, far past the web's byte budget.22+- **Ship the HTML in the loader's data.** The browser could then adopt it on any page, but the document would carry every drawing twice: once as markup and once as a string in the router's state. Instead the loader draws the code ahead, the drawing is kept by its blob ids until the render reads it, and the browser needs nothing but the markup.23+24+## Consequences25+26+- **Bytes.** The web Worker's JavaScript grows from 1,528,378 to 2,773,744 bytes (budget 4,075,000), about 940 KB of it grammars. Its entry stays at 308,418 (budget 801,000): the grammars and `@pierre/diffs/ssr` load by dynamic import on a page's first highlighted code. `apps/web/test/production-bundle.test.ts` holds the Worker to exactly these grammars, none on its entry, and no theme collection or Oniguruma. It also holds the chunks every page preloads to no grammar and nothing of this module.27+- **CPU.** Measured in the dev world's workerd, highlighting TypeScript takes about 0.14 ms a line warm: 2,400 lines about 315 ms, 600 about 85 ms, 400 about 57 ms, 100 about 18 ms. A fresh isolate's first 400-line file took 180 ms, which includes making the highlighter and compiling the grammar. 2,400 lines would cost hundreds of milliseconds, so the server's limit is 400 lines, well below the browser's.28+- **Document size.** A highlighted line costs about 700 to 1,600 bytes of markup, depending on how many tokens it holds: a 400-line TypeScript file is about 640 KB. Each drawing also carries Pierre's stylesheet and icons (about 48 KB). Both compress well: an 11-line file's page is 100 KB raw and 16 KB gzipped.29+- **Cache.** The drawings are kept in the isolate (`prerender.ts`): at most 24, and 8,000,000 characters of HTML, keyed by blob ids and path, which never change what they name. A file read again is not drawn again. A loader that finds its drawing evicted before the render makes it again, and the render streams it.30+- **The page.** A page reached by a link, not loaded, has no server drawing. It shows the plain text, which the browser's drawing then replaces. A split diff (`?style=split` on a wide screen) is plain until drawn, as before, since the server draws unified only.31+- **Without script.** A highlighted file has no `#L` anchors: Pierre's lines live in a shadow root, which a fragment cannot reach. Plain text keeps its anchors.modified:packages/config/test/web-wiring.test.ts+1 added-1 removed
249 unmodified lines250 it("holds each Worker it measures to limits of its own", () => {251 expect(budgets).toStrictEqual({252 "apps/platform": { entry: 1_900_000, javascript: 1_900_000 },253 "apps/sandbox": { entry: 656_000, javascript: 656_000 },254- "apps/web": { entry: 801_000, javascript: 4_075_000 },254+ "apps/web": { entry: 356_000, javascript: 3_191_000 },255 });256 expect(Object.keys(budgets).toSorted()).toStrictEqual(257 workers.map(({ dir }) => dir).toSorted()258 );23 unmodified linesmodified:scripts/startup-budget.ts+6 added-4 removed
44 unmodified lines45 * web's JavaScript 4,074,781 (4,075,000: no room left, until the server46 * build is minified) and the web's entry 696,419, which fell, so its limit47 * is tightened to 801,000 (15%). Minifying the web's server build took48 * its JavaScript to 1,528,378 and its entry to 308,418. Highlighting code49- * on the server (ADR 0026) then added its grammars and `@pierre/diffs/ssr`50- * as chunks off the entry, with no limit raised: the web's JavaScript51- * measured 2,773,744 (4,075,000) and its entry stayed 308,418 (801,000).49+ * on the server (ADR 0027) then added its grammars and `@pierre/diffs/ssr`50+ * as chunks off the entry, with no limit raised. Once Workers Cache's51+ * entry had landed beside them, the web's JavaScript measured 2,774,68952+ * and its entry 309,341, so its limits are tightened to 3,191,000 and53+ * 356,000 (15%).54 */55 import { BunRuntime, BunServices } from "@effect/platform-bun";56 import { Console, Data, Effect, FileSystem, Path, pipe } from "effect";57 import { ChildProcess, ChildProcessSpawner } from "effect/process";7 unmodified lines65 /** Each Worker's limits, by its folder. */66 export const budgets = {67 "apps/platform": { entry: 1_900_000, javascript: 1_900_000 },68 "apps/sandbox": { entry: 656_000, javascript: 656_000 },67- "apps/web": { entry: 801_000, javascript: 4_075_000 },69+ "apps/web": { entry: 356_000, javascript: 3_191_000 },70 } as const satisfies Record<string, Budget>;71 72 /**73 * Each Worker, the file its `main` bundles to, and the build a dry run needs131 unmodified lines