The home page shows at once

It was blank until every module had loaded and the page's examples had
been run, a fuzzer's report among them (about a second of work): first
paint came 1.3 seconds into a repeat visit and about 3 into a first one.

- The assembler draws the home page once, in Node, with the app's own
  homeView, and puts it inside #app. The browser paints it with the
  HTML and the stylesheet.
- When the app has loaded it draws the page again, with its controls,
  and puts it in the prerendered one's place; the fuzzer's reports and
  the proof's counts are copied from the prerendered page, not worked
  out again.
- The prerendered page shows only on the home address: the page's first
  inline script marks it before the first paint, and without the mark it
  stays hidden, so an embed or a problem's address never shows it. That
  script changed, and its hash in docs/Caddyfile with it.

Served locally, first paint is at 0.1 to 0.2 seconds and the page with
its controls at 0.2 to 0.3; both were at 1.2 to 1.4.
This commit is contained in:
alexm committed 2026-11-13 07:28:38 -05:00
1 parent 7000662539
commit 5202c96318
12 files changed
+154 -19

No files matched your search

+41 -1
View File
@@ -8,7 +8,7 @@ import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import { modulePreloads, assemblePage } from '../scripts/assemble-page.mjs';
import { modulePreloads, assemblePage, prerenderHome, HOME_MARK } from '../scripts/assemble-page.mjs';
const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const read = (p) => fs.readFileSync(path.join(ROOT, p), 'utf8');
@@ -60,3 +60,43 @@ test('the Caddy block caches nothing as immutable, revalidates the page and the
assert.match(caddy, /header \/VERSION Cache-Control "no-store"/);
assert.doesNotMatch(read('src/main.ts') + html, /serviceWorker/, 'no service worker (offline reload is a later decision)');
});
test('the assembled page holds the home page already drawn, shown only on the home address', async () => {
// the app's own homeView, drawn in Node: what a visitor sees before any module has loaded
const home = await prerenderHome(path.join(ROOT, 'dist'), '0123456789abcdef0123456789abcdef01234567');
assert.match(home, /^<div class="home home-page">/);
assert.match(home, /<h1[ >]/);
assert.match(home, /id="problems"/);
// no script that runs (the CSP allows inline scripts by hash only); the Quarto example's stepper carries its steps as a JSON data block
assert.doesNotMatch(home, /<script(?![^>]*\btype="application\/json")/i);
assert.match(home, /0123456789ab/, 'the footer names the commit');
// the two parts that take the time, marked so the browser copies them instead of running the fuzzer and the prover again
assert.match(home, /data-kept="tests-card"[^>]*>.*no failures.*way/s);
assert.match(home, /data-kept="proof-counts"[^>]*>.*[1-9]\d* proved.*0 broken.*0 open/s);
// it goes inside #app, where the app puts the page it draws, and changes no inline script
assert.ok(html.includes(HOME_MARK), 'index.html has the place for it');
const out = assemblePage(html, { version: '0123456789abcdef0123456789abcdef01234567', preloads: ['./dist/a.js'], home: 'a $& b <p>home</p>' });
assert.match(out, /<div id="app">[\s\S]*<div class="home-prerendered">a \$& b <p>home<\/p><\/div>\s*<\/div>\s*<!-- pplus-core's modules/);
assert.deepEqual(inlineHashes(out), inlineHashes(html));
assert.throws(() => assemblePage(html.replace(HOME_MARK, ''), { home: 'x' }), /prerendered home page goes there/);
// shown only where the page's first script marked the home address: hidden by default, so an embed, a problem's
// address, or a page whose inline script did not run never shows the home page first
const css = read('styles.css');
assert.match(css, /\n\.home-prerendered \{ display: none; \}/);
assert.match(css, /\nhtml\[data-home\] \.home-prerendered \{ display: block; \}/);
const mark = /if \((\/.*?\/)\.test\(location\.hash\)\) document\.documentElement\.setAttribute\("data-home", ""\)/.exec(html);
assert.ok(mark, 'the inline script that marks the home address is not where this test looks');
const isHome = new Function(`return ${mark[1]}`)();
const { parseRoute, isUnknownRoute } = await import('../dist/core/state.js');
for (const hash of ['', '#', '#/', '#/#problems', '#/#reference', '#/#quarto-teaser']) {
assert.ok(isHome.test(hash), `${JSON.stringify(hash)} is the home page`);
assert.equal(parseRoute(hash).kind, 'home', hash);
assert.ok(!isUnknownRoute(hash), hash);
}
for (const hash of ['#/bridge/p/count-base', '#/dev/p/demo', '#/hw/p/asm-first-instructions', '#/library', '#/quarto', '#/quarto#install', '#/embed/trace/p/count-base', '#/bridge/s/abc', '#/p/count-base']) {
assert.ok(!isHome.test(hash), `${JSON.stringify(hash)} is not the home page`);
}
// and the app takes the prerendered page's place only once the page with its controls is built
const main = read('src/main.ts');
assert.match(main, /const home = homeView\(\);\s*if \(prerendered\) clear\(app\);\s*add\(app, home\)/);
});