Public: anyone can read this; the crew can change it.

The shell's 27 KB of JavaScript is a cached file, not re-sent with every page #1004

Closed opened this
Closed without merging

The shell's script (shellJs, 27.7 KB) is no longer sent inline with every page. It is now a cached file at /assets/shell- .js, and each page links it, so every HTML response is about 27.5 KB lighter. A browser fetches the shell once per deploy.

Notes

stopped closed without shipping deploy 2fd49ba

opened this

The shell's script (shellJs, ~27.7 KB) is no longer sent inline with every page. It is now a cached file at /assets/shell-<hash>.js, and each page links it, so every HTML response is about 27.5 KB lighter. A browser fetches the shell once per deploy.

What moved

  • src/web/asset.ts: a new shellAsset() hashes shellJs once per process and gives back its path, its name and an integrity hash. shellResponse() serves it through the existing assetResponse, with cache-control: public, max-age=31536000, immutable.
  • src/http/server.ts: the existing /assets/:name.js route now serves the shell as well as the highlighter bundle. A stale hash still gets a 404, and an old tab falls back to plain HTML until it reloads.
  • src/web/layout.ts: page() no longer joins shellJs onto the page's own script. It emits <script nonce src integrity> for the shell, then the page's own scriptJs inline under the same nonce, only when the page has one.

How the run order is kept: the shell is linked as a classic script, not a module and not deferred, placed just before the page's inline script. Classic scripts run in document order, so the page's script still runs after the shell, as it did when they were one block. The shell is a self-contained IIFE that defines no globals. The cost is that the shell blocks parsing at the end of <body> on first load. After that it comes from cache.

CSP: the external tag carries the page's nonce, so script-src does not gain 'self' and no page gains 'unsafe-inline'. The highlighter is still the only thing that relaxes the policy, and the integrity hash pins the shell's bytes.

Tests

  • New, in test/web.test.ts:
    • On the signed-in studio /, the shell's source is not in the HTML (and shellJs is over 25 KB).
    • On the same page, the shell is linked as a classic script with the nonce and integrity, before an inline script that carries the page's own code.
    • The policy has no 'unsafe-inline' and no 'self'.
    • The asset is content-addressed, immutable, byte-for-byte shellJs, and matches its integrity hash; a stale hash gets a 404.
  • Changed, because they asserted the old inline shape:
    • The palette test now reads fuzzyScore/openModal from the linked file instead of the HTML.
    • The mirror-job test in test/web-browse.test.ts now allows the shell's file as the only <script src>.
  • Results: type check clean. With --timeout 60000 (what the forge's check uses), the web, browse, editor, palette, UI, code, studio, live, board and adversarial-CSP suites all pass (385 tests). Under bun's default 5 s hook timeout, the setup of studio-words and web-browse timed out on this machine; the base branch fails the same way.

Not done: the shell isn't minified. It is served as written, and gzip covers most of the gain. I left docs/DESIGN.md alone because its line about CSP is still true.

To try it: open any page and see <script nonce=… src="/assets/shell-….js" integrity=…> at the end of <body>. On a second load the file is served from cache, and the palette (⌘K), Escape on menus and the theme menu all work as before.

🤖 Generated with Claude Code

Closes #114

commented

Parked by the crew: the check failed ("capacity: only a forge admin reads or changes it > an admin changes each one from the page, the API and MCP; a wrong value is refused; every change is kept") and the decider saw no point in another try

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs