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.
This commit is contained in:
parent
7157c928c8
commit
f62975f948
5 changed files with 61 additions and 53 deletions
27
README.md
27
README.md
|
|
@ -161,32 +161,15 @@ static.
|
|||
bun run site:build # → dist-viewer/, ready for any static host
|
||||
```
|
||||
|
||||
The output is the viewer at a domain root: an `index.html`, a `viewer.js` and a
|
||||
`viewer.css` it loads by absolute path. Nothing is fetched at run time — the
|
||||
registry address in the sidebar is a label, not a request — so there is no API
|
||||
to stand up and nothing to 404.
|
||||
The output is the viewer at a domain root: an `index.html`, and a bundle and a
|
||||
stylesheet it loads by absolute path, both content-hashed so they can be cached
|
||||
for a year. Nothing is fetched at run time — the registry address in the
|
||||
sidebar is a label, not a request — so there is no API to stand up and nothing
|
||||
to 404.
|
||||
|
||||
Beside it is `artifact.html`, the same viewer collapsed into one self-contained
|
||||
file. That is the one to hand to anyone who wants it without a server.
|
||||
|
||||
| Host | Free with a PRIVATE repo | Build command | Output |
|
||||
| --- | :-: | --- | --- |
|
||||
| Vercel | yes | `bun run site:build` | `dist-viewer` |
|
||||
| Cloudflare Pages | yes | `bun run site:build` | `dist-viewer` |
|
||||
| GitHub Pages | **no** — needs Pro | — | — |
|
||||
|
||||
`vercel.json` already carries the first row, so Vercel needs the repository
|
||||
imported and nothing else. GitHub Pages is in the table because it is the
|
||||
obvious guess and the answer is no: on the free plan Pages serves public
|
||||
repositories only, so it costs either a subscription or making the source
|
||||
public.
|
||||
|
||||
One thing worth being deliberate about rather than discovering later:
|
||||
publishing the viewer publishes the MODELS. The bundle carries every builder
|
||||
in it, minified. That is a different thing from the npm package, which ships
|
||||
the source as source — but it is not nothing, and a page anyone can open is a
|
||||
page anyone can read.
|
||||
|
||||
## Layout
|
||||
|
||||
```text
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
* bunx vite build --config vite.viewer.config.ts
|
||||
* bun scripts/build-artifact.ts
|
||||
*/
|
||||
import { readFile, writeFile } from 'node:fs/promises'
|
||||
import { readdir, readFile, writeFile } from 'node:fs/promises'
|
||||
import { dirname, join, resolve } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
|
|
@ -21,8 +21,22 @@ const FONTS =
|
|||
'https://fonts.googleapis.com/css2?family=Archivo:wght@400;700' +
|
||||
'&family=IBM+Plex+Mono:wght@400;500;600&display=swap'
|
||||
|
||||
const css = await readFile(join(distRoot, 'viewer.css'), 'utf8')
|
||||
const js = await readFile(join(distRoot, 'viewer.js'), 'utf8')
|
||||
/**
|
||||
* By pattern, because the built names carry a content hash.
|
||||
*
|
||||
* They are hashed so the published site can serve them with a year of
|
||||
* immutable caching, which is only correct if the name changes when the bytes
|
||||
* do. The cost is exactly this: nothing downstream may name them literally.
|
||||
*/
|
||||
const built = await readdir(distRoot)
|
||||
const find = (extension: string): string => {
|
||||
const hit = built.find((name) => name.startsWith('viewer-') && name.endsWith(extension))
|
||||
if (!hit) throw new Error(`no viewer-*${extension} in dist-viewer — did the vite build run?`)
|
||||
return join(distRoot, hit)
|
||||
}
|
||||
|
||||
const css = await readFile(find('.css'), 'utf8')
|
||||
const js = await readFile(find('.js'), 'utf8')
|
||||
|
||||
// A `</script` sequence inside the inlined script would close the HTML parser
|
||||
// early. This escape does not change the JavaScript meaning.
|
||||
|
|
|
|||
|
|
@ -18,18 +18,36 @@
|
|||
*
|
||||
* bun run site:build → dist-viewer/, ready for any static host
|
||||
*/
|
||||
import { copyFile, stat } from 'node:fs/promises'
|
||||
import { copyFile, readdir, stat, writeFile } from 'node:fs/promises'
|
||||
|
||||
const from = 'dist-viewer/viewer.html'
|
||||
const to = 'dist-viewer/index.html'
|
||||
const dist = 'dist-viewer'
|
||||
await copyFile(`${dist}/viewer.html`, `${dist}/index.html`)
|
||||
|
||||
await copyFile(from, to)
|
||||
/**
|
||||
* 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
|
||||
|
||||
const sizes = await Promise.all(
|
||||
['index.html', 'viewer.js', 'viewer.css', 'artifact.html'].map(async (name) => {
|
||||
const info = await stat(`dist-viewer/${name}`)
|
||||
return `${name} ${Math.round(info.size / 1024)} KB`
|
||||
}),
|
||||
)
|
||||
/*.html
|
||||
Cache-Control: public, max-age=0, must-revalidate
|
||||
|
||||
console.log(`dist-viewer/ ready — ${sizes.join(' · ')}`)
|
||||
/
|
||||
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(' · ')}`)
|
||||
|
|
|
|||
16
vercel.json
16
vercel.json
|
|
@ -1,16 +0,0 @@
|
|||
{
|
||||
"$schema": "https://openapi.vercel.sh/vercel.json",
|
||||
"buildCommand": "bun run site:build",
|
||||
"outputDirectory": "dist-viewer",
|
||||
"installCommand": "bun install --frozen-lockfile",
|
||||
"framework": null,
|
||||
"cleanUrls": true,
|
||||
"headers": [
|
||||
{
|
||||
"source": "/viewer.(js|css)",
|
||||
"headers": [
|
||||
{ "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -22,7 +22,16 @@ export default defineConfig({
|
|||
cssCodeSplit: false,
|
||||
rollupOptions: {
|
||||
input: fileURLToPath(new URL('./viewer.html', import.meta.url)),
|
||||
output: { inlineDynamicImports: true, entryFileNames: 'viewer.js', assetFileNames: 'viewer[extname]' },
|
||||
output: {
|
||||
inlineDynamicImports: true,
|
||||
// CONTENT HASHED, and not for tidiness. The published site serves
|
||||
// these with a year of immutable caching, which is only ever correct
|
||||
// if the name changes when the bytes do: at the fixed `viewer.js` a
|
||||
// returning visitor would have been handed a year-old bundle after
|
||||
// every future deploy. `build-artifact.ts` finds them by pattern.
|
||||
entryFileNames: 'viewer-[hash].js',
|
||||
assetFileNames: 'viewer-[hash][extname]',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue