Skip to content

web gallery: cache each demo's wasm, data and glue by content hash - #1363

Merged
transfix merged 3 commits into
masterfrom
feat/gallery-cache
Nov 19, 2026
Merged

transfix merged 3 commits into
masterfrom
feat/gallery-cache

Conversation

@transfix

@transfix transfix commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

The web gallery (https://transfix.github.io/libcvc/) downloaded its large files again on nearly every load.

Cause

  • GitHub Pages serves every file with Cache-Control: max-age=600 and an ETag made from the file's timestamp (for example "6afe3a7c-2e28b2"). We can't change either.
  • deploy pages runs on every push to master, plus a nightly cron, and each deploy rewrites every file. So every ETag changes even when the bytes don't, and a revalidation downloads the whole file. About 20 pushes landed on 2026-11-18 alone.
  • demo.html.in fetched <demo>.wasm?v=<hash> with cache: 'no-cache', which revalidates on every load. arigl.wasm is 59 MB (17 MB gzipped). arigl.data (about 3 MB) and arigl.js (0.4 MB) went through the same HTTP cache: the .data was fetched by Emscripten's loader with no content hash in its URL.

Fix

Content-addressed caching in the page (examples/web/build-pages.py, examples/web/templates/demo.html.in):

  • build-pages.py computes each template page's large files and writes them into the page as ASSETS = {wasm, data, js}, each with {name, hash, size}. hash is the first 16 hex digits of the file's SHA-256. .data is listed only when the demo has one. {{ASSET_V}} still renders, with the same value as before, for downstream templates that use it.
  • The loader keeps these files in Cache Storage (cvcgl-demo-assets), keyed by <name>?v=<hash>.
    • Hit: the cached bytes are used with no network request at all.
    • Miss: the missing files are downloaded together, with one progress line, revalidated as before. Each download is hashed, stored under the hash of the bytes that actually arrived, and the cache entries for that file's other hashes are deleted, so old versions don't pile up.
  • The .data goes to Emscripten through Module.getPreloadedPackage(name, size), which returns an ArrayBuffer, so its loader doesn't fetch it.
  • The glue runs from a blob URL made from the cached bytes. Its script directory is then empty, so it resolves files next to the page, which is where they are. Module.mainScriptUrlOrBlob points a threaded build's workers at the real URL.
  • The page is the correctness guarantee. Its hashes name the files. The page itself is small, and Pages lets a browser reuse it for at most 10 minutes. If a stale copy of the page (from before a deploy) still holds its files in the cache, it loads that older set, which is consistent. If it misses, the server sends the new bytes. Those hash differently, so they are stored under their real hash, and the page reloads itself once to get the current page, which then finds them in the cache. A sessionStorage key per page version allows only that one reload, so a CDN that is still mid-deploy can't loop it. After that the page carries on as it used to.
  • Fallback: where Cache Storage or WebCrypto is unavailable (insecure context, some private windows, caches.open rejecting), or with ?nocache for debugging, the page loads as before: the wasm with no-cache, the glue by <demo>.js?v=<hash>, and the .data by Emscripten itself. A failed put (for example a quota error) costs only the caching.

Fewer pointless deploys (.github/workflows/deploy-pages.yml): the push trigger now has a paths filter. It leaves out docs/**, top-level *.md (but keeps THIRD_PARTY_NOTICES.md, which ships in the gallery), .github/** (but keeps this workflow and the pin script it runs), and bindings/** (the gallery is built with CVC_BUILD_PYCVC off). Other .md files still trigger a deploy: arigl.data packs examples/data including its README, and arigl's wasm embeds src/cvcGL/fonts including README.md. The schedule and workflow_dispatch triggers are unchanged. Over 2026-11-11..18 the filter would have skipped 17 of 185 master pushes. Most pushes touch the library, so the cache does most of the work here.

Pre-JX1 wasm pin in the deploy (.github/workflows/deploy-pages.yml, second commit): every deploy since 2026-11-18T23:13 has failed at link with libfreetype.a(ftbase.c.o): undefined symbol: __wasm_longjmp (run 44437115573). The cause is cy-pca/cvcpkg#624 (JX1), which republished wasm packages built with -fwasm-exceptions. #1360 adds .github/scripts/pin-wasm-pre-jx1.sh and calls it in ci.yml only. This PR calls it at the end of the deploy's "Install fresh wasm dep closure + scene bundles" step, as bash .github/scripts/pin-wasm-pre-jx1.sh "$CVC_WASM_DEPS" wasm. It runs after every wasm install, so none of those installs can move a pinned package back. The paths filter keeps that script in, so a change to it redeploys. I checked that the pinned versions exist on cvcpkg.org as wasm/wasm32 static release bundles (cvcpkg search).

Depends on #1360: the script exists only on that branch. Once #1360 lands, origin/master gets merged into this branch (a merge commit, not a rebase). Don't merge this PR before that.

Expected effect

An unchanged file is never downloaded again. A changed one is downloaded once per change. A redeploy of identical bytes, such as a docs-only push that still deploys or the nightly cron with no source change, costs a page revalidation and nothing more. arigl.wasm links most of the library, so a push that changes code reaching arigl still changes its hash, and that costs one download of the wasm only. The .data and the glue change far less often.

Tested

  • build-pages.py on a fake bin/ (arigl with .wasm/.data/.js, lsystem_forest without .data, ari with its own page) using the real demos.json and gallery assets:
    • the hashes and sizes in the pages match the files' SHA-256;
    • ASSET_V equals the old script's value;
    • output is identical to master's script except the two template pages;
    • own pages and document redirects are unchanged.
  • The page logic in the desktop app's Chromium pane, against a local server that answers like Pages (max-age=600, ETag from mtime and size), with valid wasm modules and a fake glue that reports how it got its files:
    • First load: three downloads, three entries cached. The .data arrived through getPreloadedPackage as an ArrayBuffer, the glue ran from a blob URL, and the wasm instantiated.
    • Reload: no requests for wasm, data or js.
    • Deploy that changes only the wasm and rewrites every file: the reload fetched only the new wasm, and the old wasm entry was deleted.
    • Stale page, entries cached: it loaded with no asset requests and nothing was pruned.
    • Stale page, wasm missing: one download, stored under the new hash, then one reload. The current page used the cached bytes with no second download.
    • Page that stays stale: one reload, then it carried on. No loop.
    • ?nocache: same requests as master's page.
    • Demo without .data: works.
    • put rejecting, match throwing, caches.open rejecting: each falls back as intended.
  • The workflow YAML parses. The filter was simulated over the last week's master pushes.

Not verified

  • The pin step in a real deploy run. The deploy only runs on master, and the script isn't on master until ci: pin the pre-JX1 wasm bundles until JX2 #1360 lands.
  • A real deploy and the real Emscripten glue in a browser on the Pages site. The fake glue mirrors the loader code in the live arigl.js (getPreloadedPackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE), the ArrayBuffer check, scriptDirectory from new URL('.', currentScript.src)), but no wasm was built here.
  • The threaded (wasm-mt) gallery and mainScriptUrlOrBlob.
  • Firefox, Safari and iOS. Cache Storage quotas and private-window behaviour differ there; the fallback covers a refusal.
  • Whether two builds of the same source give byte-identical files. That is needed for an unchanged nightly rebuild to cost nothing. No commit hash or build date is embedded in the wasm demos.
  • The pages with their own loaders (ari, ari_tvision via ari-web.js, and arigl_term) are unchanged and still revalidate on each load. ari-web.js runs under script-src 'self', which rules out the blob-URL glue, so they need their own change.

… content hash

GitHub Pages serves every file with max-age=600 and an ETag made from the file's time, and deploy
pages rewrites every file on each push to master, so after any deploy the browser revalidated and
downloaded arigl.wasm (59 MB, 17 MB on the wire), arigl.data and arigl.js again, changed or not.
The page also fetched the wasm with cache: 'no-cache'.

build-pages.py now bakes each template page's large files into it as ASSETS ({wasm, data, js},
each name, the first 16 hex digits of its SHA-256 and size). demo.html.in keeps them in Cache
Storage keyed by name + hash: a hit is used with no request at all; a miss is downloaded (one
progress line), kept under the hash of the bytes that came, and that asset's other versions are
deleted. The .data goes to Emscripten through Module.getPreloadedPackage and the glue runs from a
blob URL (Module.mainScriptUrlOrBlob keeps a threaded build's workers on the real URL). A copy of
the page older than the deploy, whose download hashes differently, reloads itself once per page
version. Without Cache Storage (insecure context, some private windows, storage errors) or with
?nocache the page loads as before.

deploy-pages.yml: a push touching only docs/, top-level notes (THIRD_PARTY_NOTICES.md excepted,
it ships in the gallery), .github/ other than this workflow, or bindings/ no longer redeploys.
Every deploy since 2026-11-18T23:13 fails at link with libfreetype.a(ftbase.c.o): undefined
symbol: __wasm_longjmp (run 44437115573): cy-pca/cvcpkg#624 (JX1) republished wasm packages
built with -fwasm-exceptions, and libcvc still builds with JS exceptions until JX2. #1360 adds
.github/scripts/pin-wasm-pre-jx1.sh and calls it in ci.yml; this calls it at the end of the
deploy's install step, after every wasm install, and lets a change to the script redeploy.
@transfix
transfix enabled auto-merge (squash) November 19, 2026 08:22
@transfix
transfix merged commit f373c05 into master Nov 19, 2026
29 checks passed
@transfix
transfix deleted the feat/gallery-cache branch November 19, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant