medieval-kit/scripts/build-site.ts
CRT 0dd4ea6001 Give the published page a card
A link to the site showed a URL and a blank square. It now carries a title, a
description, a canonical URL, an icon and a full set of OpenGraph and Twitter
tags, so a page nobody has opened yet arrives as something you can look at.

Two of those tags have to be ABSOLUTE and it is not a style question: a
scraper fetching the page has no base to resolve `/cover.png` against and
drops it silently. `build-site.ts` now copies the images into the output as
well, because an `og:image` pointing at this host has to have the file ON this
host — a card that 404s shows nothing, and nothing about the page itself looks
wrong while it happens.

The cover is a real 1200x630 render rather than a crop of a square one, which
took a change to the renderer worth having anyway. `newFrame` and
`frameCamera` both took a width and a height already; only `renderOne` was
insisting they be equal. And with a non-square frame the old camera fit showed
its flaw: it fitted the bounding SPHERE, which is one number and wrong for
anything not roughly round. The kit scene is wide and flat, so its sphere is
far taller than the scene is, and fitting that into the vertical field left the
models in a band across the middle with empty air above and below — most of a
1.91:1 card. It now fits the BOX, iteratively, because perspective is not
linear in distance: place the camera, project the eight corners, move by how
far the worst one lands past the edge. Two passes is inside a pixel.

That change improves every render, not just the card: on the contact sheet
every model is visibly larger in its cell and nothing crops.

`check-docs` now watches `viewer.html` too. Its description quotes the model
count and the triangle total, which is the same sentence that went stale three
times in the READMEs before that check existed — and a stale number in a card
is worse than one in a README, because nobody reading the card can see the kit
to know it is wrong. The description says "37" rather than "thirty-seven" for
exactly that reason: a word is not a number the check can find.

Also repointing the npm README's two images from `raw.githubusercontent.com`
to the live site. They were pointing into a private repository and would have
404'd on npmjs.com; now they do not depend on the repository being readable at
all.
2026-08-27 07:34:59 +00:00

66 lines
2.8 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')
/**
* The images the page's own meta tags point at.
*
* `og:image` is an absolute URL to this host, so the file has to BE on this
* host: a card that 404s is a card that shows nothing, and nothing about the
* page itself would look wrong. `kit.png` and `models.png` come too, because
* the published README links to them and a reader of that page should not
* depend on a private repository being readable.
*/
for (const image of ['cover.png', 'icon.png', 'kit.png', 'models.png']) {
await copyFile(`media/${image}`, `${dist}/${image}`)
}
// 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(' · ')}`)