feat(bankai): Phase 4 follow-ups — css convention, live titles, prerender, HTTP/2, size budget

- style.css convention: <entry dir>/style.css is imported by the virtual
  client entry (CSS is a client concern; server code never sees it — the
  v8 answer to sheetify), extracted and hashed by Vite, stylesheet-linked
  in the head and preloaded via Early Hints. Counter example styled.
- SSR <title> from state: the server reads the first stream chunk before
  writing the head, by which point stores and the first render slice have
  run — DOMTitleChange emits land in the document title. Counter emits.
- bankai build --prerender /,/about renders routes through the same
  toStream path to static <route>/index.html (precompressed, precached
  by the service worker, served with Early Hints).
- bankai serve --h2: HTTP/2 with a generated local cert (openssl,
  cached; allowHTTP1) — browsers only act on Early Hints over h2/h3.
  Tested with a real h2 client observing the 103 interim response.
- npm run size: the framework wire-size budget, enforced in CI. Whole
  framework (core + html engine + morph + hydrate) is 7.97 kB min+gzip /
  7.15 kB brotli; budget 8.5 kB with the v7 '4kb' context documented
  (that number excluded the html engine, which lived in the browserify
  transform).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014NgfSjHE11oFpoSnLVKLXd
This commit is contained in:
Bendik Aagaard Lynghaug
2026-09-08 19:39:25 +02:00
co-authored by Claude Fable 5
parent 0a351d7f33
commit 99f9da1e3b
11 changed files with 305 additions and 37 deletions
+36 -6
View File
@@ -4,28 +4,33 @@
// dist/assets/* hashed client chunks + css
// dist/.vite/manifest.json Vite's build manifest
// dist/bankai.json bankai's route/asset metadata for serving
// dist/**/index.html prerendered routes (--prerender)
// dist/sw.js service worker (if <entry dir>/sw.js exists),
// with the precached asset list defined in
// *.br / *.gz precompressed siblings for every text asset
import { readFile, writeFile, readdir, stat, access } from 'node:fs/promises'
import { readFile, writeFile, readdir, stat, access, mkdir } from 'node:fs/promises'
import { join, dirname, resolve } from 'node:path'
import { pathToFileURL } from 'node:url'
import { brotliCompress, gzip, constants } from 'node:zlib'
import { promisify } from 'node:util'
import bankaiPlugin, { CLIENT_ID } from './plugin.js'
import bankaiPlugin, { findCss, CLIENT_ID } from './plugin.js'
import { resolveApp } from './app.js'
import { documentHead, documentTail } from './document.js'
const brotli = promisify(brotliCompress)
const gzipP = promisify(gzip)
export default async function buildApp ({ entry, outDir = 'dist', title = 'choo' }) {
export default async function buildApp ({ entry, outDir = 'dist', title = 'choo', prerender = [] }) {
const { build } = await import('vite')
entry = resolve(entry)
outDir = resolve(outDir)
const css = await findCss(entry)
await build({
appType: 'custom',
plugins: [bankaiPlugin(entry)],
plugins: [bankaiPlugin(entry, { css })],
logLevel: 'warn',
build: {
outDir,
@@ -48,11 +53,36 @@ export default async function buildApp ({ entry, outDir = 'dist', title = 'choo'
const meta = { title, entry, assets, builtAt: new Date().toISOString() }
await writeFile(join(outDir, 'bankai.json'), JSON.stringify(meta, null, 2))
// static prerender: routes rendered through the same toStream path the
// server uses, written as <route>/index.html
const prerendered = []
if (prerender.length) {
const mod = await import(pathToFileURL(entry))
for (const route of prerender) {
const app = resolveApp(mod, entry)
const state = {}
let body = ''
const decoder = new TextDecoder()
for await (const chunk of app.toStream(route, state)) {
body += decoder.decode(chunk, { stream: true })
}
const page = documentHead({ title: state.title || title, ...assets }) + body + documentTail(state)
const rel = route === '/' ? 'index.html' : join(route.replace(/^\//, '').replace(/\/$/, ''), 'index.html')
const target = join(outDir, rel)
await mkdir(dirname(target), { recursive: true })
await writeFile(target, page)
prerendered.push(route)
}
}
// service worker: <entry dir>/sw.js, built standalone with the asset
// list injected — the choo-service-worker convention, manifest edition
const swSource = join(dirname(entry), 'sw.js')
if (await exists(swSource)) {
const precache = ['/', ...assets.scripts, ...assets.modulepreload, ...assets.css]
const precache = [
...(prerendered.length ? prerendered : ['/']),
...assets.scripts, ...assets.modulepreload, ...assets.css
]
await build({
appType: 'custom',
logLevel: 'warn',
@@ -71,7 +101,7 @@ export default async function buildApp ({ entry, outDir = 'dist', title = 'choo'
// precompress text assets so the server never compresses at request time
const compressed = await precompress(outDir)
return { outDir, meta, compressed }
return { outDir, meta, compressed, prerendered }
}
function collectCss (manifest, entryChunk) {