medieval-kit/scripts/build-site.ts
CRT f62975f948 Hash the site's bundles, and take the ops notes out of the README
Two corrections, and the first was a bug I had just written.

`vercel.json` served `/viewer.js` and `/viewer.css` with a year of immutable
caching, and those names carry no content hash — they are pinned in the vite
config. Immutable caching on a fixed name means a returning visitor is handed
the bundle they already have, for a year, no matter how many times the site is
rebuilt. The rule and the filenames were written a few minutes apart and
contradicted each other.

So the bundles are hashed now, which makes the caching correct rather than
wrong, and the cost is exactly the thing the rule creates: nothing downstream
may name them literally. `build-artifact.ts` finds them by pattern. So does
`build-site.ts` — whose own first version listed `viewer.js` by name in its
size report, and fell over on the very build that introduced the hash. The
mistake and its consequence landed in the same commit.

The cache rules now live in a `_headers` file written into the output, where
the host looks for them: a year on the hashed bundles, and revalidation on the
HTML, which must NOT be cached that way — it is what points at the current
hash, and a stale copy of it pins a visitor to a build that no longer exists.

Second: the README had grown a hosting comparison — which providers are free
with a private repository, what GitHub Pages costs. None of that belongs in a
README. It is a public-facing document about a model registry, read by people
deciding whether to install it, and my deployment arithmetic is neither their
business nor true for their situation. What stays is the part that is about
this project: the command, what the output is, and that nothing is fetched at
run time. `vercel.json` goes with it, since it was config for a host this
project does not use.
2026-08-27 06:27:29 +00:00

53 lines
2.3 KiB
TypeScript

/**
* Builds the published site: the viewer, at a domain root.
*
* The viewer build already emits everything a host needs — `viewer.html` plus
* a `viewer.js` and a `viewer.css` it loads by absolute path, which is correct
* at the root of a domain and wrong nowhere. What it does not emit is an
* `index.html`, so a static host serving the directory answers `/` with a 404
* and the site looks broken while every file on it is fine.
*
* So this is a copy, and it is a script rather than a line in `package.json`
* because there is a decision in it: the site's landing page is the VIEWER,
* not the demo app. Someone arriving at the address should be able to turn a
* model over and press play, not read a page about a model they cannot see.
*
* `artifact.html` is left beside it. It is the same viewer collapsed into one
* self-contained file, which is what to hand anyone who wants the thing
* without a server — a VDS, an email, a USB stick.
*
* bun run site:build → dist-viewer/, ready for any static host
*/
import { copyFile, readdir, stat, writeFile } from 'node:fs/promises'
const dist = 'dist-viewer'
await copyFile(`${dist}/viewer.html`, `${dist}/index.html`)
/**
* Cache rules for the host, written into the output where it looks for them.
*
* The two bundles carry a content hash, so their names change whenever their
* bytes do and a year of immutable caching is exactly right. The HTML must
* NOT be cached that way: it is what points at the current hash, and a stale
* copy of it pins a visitor to a build that no longer exists.
*/
await writeFile(`${dist}/_headers`, `/viewer-*
Cache-Control: public, max-age=31536000, immutable
/*.html
Cache-Control: public, max-age=0, must-revalidate
/
Cache-Control: public, max-age=0, must-revalidate
`, 'utf8')
// Listed rather than named. The bundles are hashed, so writing `viewer.js`
// here would be a lie the moment the bytes change — which is the same mistake
// this script's own first version made, in this very line.
const names = (await readdir(dist)).filter((name) => !name.startsWith('_'))
const sizes = await Promise.all(names.sort().map(async (name) => {
const info = await stat(`${dist}/${name}`)
return `${name} ${Math.round(info.size / 1024)} KB`
}))
console.log(`${dist}/ ready — ${sizes.join(' · ')}`)