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:
12 files changed
+154
-19
No files matched your search
@@ -35,6 +35,7 @@ No version has been tagged yet. What programs mean is pplus-core's, and its chan
|
||||
|
||||
### Fixed
|
||||
|
||||
- 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: first paint came after 1.3 seconds on a repeat visit and about 3 on a first one. The assembled site now carries the home page already drawn (`scripts/assemble-page.mjs`, `src/ui/prerender.ts`), painted with the HTML; the app takes its place when it has loaded, without running the fuzzer and the prover again. Measured on the assembled site served locally: first paint 0.1 to 0.2 seconds, the page with its controls at 0.2 to 0.3 (it was 1.2 to 1.4 for both). The inline script of `index.html` changed, and its hash in `docs/Caddyfile` with it.
|
||||
- The deploy job publishes to `/srv/www/pplus`, the directory the site is served from, unless `SITE_SLUG` says otherwise. Its default was the repository's name, so the job published to `/srv/www/pplus-web` and the live site did not change.
|
||||
- The deploy job's smoke test always runs on `main`: it asks the live site for its version after publishing, at `https://pplus.scient.ing` unless `SITE_URL` names another address. It ran only when that variable was set, and it was not.
|
||||
- `docs/Caddyfile` served the page in place at a missing nested path, where its own files could not be found (a blank page). A missing page is sent to `/`; a missing file is a plain 404.
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@
|
||||
# X-Frame-Options here; framing is allowed from the course
|
||||
# origins, and from a local `quarto preview` of them.
|
||||
header {
|
||||
Content-Security-Policy "default-src 'self'; script-src 'self' 'sha256-I0FTUDEvZjevwnUsTxL/Wv6yjSk4+kJk+rHFqkJdfzU=' 'sha256-RDS2ylVnVHGfhqa7CxiLHKiZwFxVxLSwfQQ6NYsJmuM='; style-src 'self' 'unsafe-inline'; font-src 'self'; img-src 'self' data: blob:; connect-src 'self' https://godbolt.org; frame-src 'self'; worker-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'self' https://scient.ing https://*.scient.ing http://localhost:* http://127.0.0.1:*"
|
||||
Content-Security-Policy "default-src 'self'; script-src 'self' 'sha256-H7kdvxp0TUI4FRNjpz2GbeB4zUUFSA0SVJWlzJL2bb8=' 'sha256-RDS2ylVnVHGfhqa7CxiLHKiZwFxVxLSwfQQ6NYsJmuM='; style-src 'self' 'unsafe-inline'; font-src 'self'; img-src 'self' data: blob:; connect-src 'self' https://godbolt.org; frame-src 'self'; worker-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'self' https://scient.ing https://*.scient.ing http://localhost:* http://127.0.0.1:*"
|
||||
Referrer-Policy "no-referrer"
|
||||
X-Content-Type-Options "nosniff"
|
||||
Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=(), clipboard-write=(self)"
|
||||
|
||||
@@ -105,6 +105,8 @@ A student can save the state of their work under a name and come back to any sav
|
||||
|
||||
**Work that takes the page down.** The engines stop a program that runs too long or makes values too large (core's [limits.md](../core/docs/limits.md)), but should a program still use up the tab's memory first, the browser kills the page. Saved work runs again as the page opens, so before it runs its save is marked busy in this tab (`src/core/guard.ts`, in `sessionStorage`), and the mark is cleared once the page has been idle for a moment and when it closes normally. A reload that finds the mark puts that save aside as a named version, "When the page stopped (time)", opens the problem as written, and says so; an embed drops the reader's edits instead. If a saved state makes the page fail in any other way, the page that says so offers the same: keep the work as a version and open the problem as written.
|
||||
|
||||
The home page is the one page that is drawn before the app starts: the assembled site carries it prerendered (`docs/deploy.md`), and `homeView` then takes its place. A part of it that is costly to work out and holds no control is wrapped in `kept(key, build)` (`src/ui/prerender.ts`), which copies it from the prerendered page; give a new part of that kind the same wrapper, and never one with a listener, since a copy has none.
|
||||
|
||||
Nothing else is a route: `#/p/<problem>`, `#/s/<state>` and the other earlier forms open the home page, with a note at the foot of the page that there is no page at that address (`isUnknownRoute` in `src/core/state.ts`). A problem or library routine that does not exist, in the app or in an embed, is a page that says so (in the app it keeps the site's navigation and links the problem list); so is a link that does not open (below). `<view>` is `playground` (the whole workspace; `?area=dev` picks the area, Bridge by default or the problem's own), `trace`, `table`, `flow`, `data`, `life`, `proof`, `tests`, `problems`, `compiler`, `asm` (the machine numbers), or a pane name (`lingo`, `pseudo`, `speco`, `mojo`, `asm`) to show just that editor with its problems under it. Query parameters on an embed: `?t=12` parks the stepper, `?ro=1` makes the editor read-only; `area`, `panel`, `lang`, `build`, `machine`, `dock`, `docktab`, `editor`, `side`, `dockheight` and `theme` arrange the example as the playground's controls do (`applyEmbedParams` in `src/core/state.ts`).
|
||||
|
||||
A reader's edits in an embed are saved in their browser, in that embed's own save (one per embedded example, shared by pages that embed the same one), never in the app's save for the problem. Once they have changed something, **Reset** appears beside "open in Programming+" and puts the author's version back. An embed keeps a save only while it holds edits, so a reader who has not changed an example always sees the author's current version. A read-only embed (`?ro=1`) saves nothing.
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ The site builds against pplus-core, the git submodule `core/`, at the commit thi
|
||||
|
||||
`pixi run site` tests the app and assembles `site/`; `pixi run deploy [slug] [user@host]` does that and publishes it. CI (`.gitea/workflows/deploy.yml`) runs `pixi run ci` (the site's tests and core's `mojo-status-strict`) on every push and pull request, and on a push to `main` checks the chain of pointers (`pixi run release-check`), assembles and publishes.
|
||||
|
||||
The site is seven things: `index.html`, `styles.css`, `fonts/` (IBM Plex, served by the site so a visit asks no font host for anything; `fonts/README.md` says where the files came from), `styles/` (the course's widget styles, which the Hardware area's machine views use), `dist/`, `core/` (pplus-core's built `core/dist/`, which `index.html`'s import map names as `#core/`), and the Quarto extension (`quarto/_extensions/pplus`, without an engine copy), which the With Quarto page loads (`pplus.css`, `pplus.js`) and links to file by file. `scripts/assemble.sh` copies them into `site/` with a `VERSION` file holding the commit, a `build.json` naming the three commits the site was built from (below), and the license and notices (`LICENSE.md`, `NOTICE.md`, `THIRD_PARTY.md`, which the footer links), then `scripts/assemble-page.mjs` adds two things to `site/index.html`: the commit as `<meta name="pplus-version">`, which the footer shows, and a `<link rel="modulepreload">` for every module the page's imports reach, after the import map, so a first visit asks for all of them at once instead of one level of imports at a time (`tests/load.test.mjs` holds the list to the import graph and the inline scripts to their hashes). And `scripts/deploy.sh <slug> [user@host]` publishes `site/` to `/srv/www/<slug>` as an atomic release:
|
||||
The site is seven things: `index.html`, `styles.css`, `fonts/` (IBM Plex, served by the site so a visit asks no font host for anything; `fonts/README.md` says where the files came from), `styles/` (the course's widget styles, which the Hardware area's machine views use), `dist/`, `core/` (pplus-core's built `core/dist/`, which `index.html`'s import map names as `#core/`), and the Quarto extension (`quarto/_extensions/pplus`, without an engine copy), which the With Quarto page loads (`pplus.css`, `pplus.js`) and links to file by file. `scripts/assemble.sh` copies them into `site/` with a `VERSION` file holding the commit, a `build.json` naming the three commits the site was built from (below), and the license and notices (`LICENSE.md`, `NOTICE.md`, `THIRD_PARTY.md`, which the footer links), then `scripts/assemble-page.mjs` adds three things to `site/index.html`: the commit as `<meta name="pplus-version">`, which the footer shows; a `<link rel="modulepreload">` for every module the page's imports reach, after the import map, so a first visit asks for all of them at once instead of one level of imports at a time; and the home page already drawn, inside `#app` (`tests/load.test.mjs` holds the list to the import graph, the inline scripts to their hashes, and the prerendered page to its place). **The home page is prerendered** because drawing it runs the engines on its own examples, a fuzzer's report among them, about a second of work that came after 150 modules had loaded: a visitor saw nothing for 1.3 to 3 seconds. The assembler draws it once in Node with the app's own `homeView` (on the small document the Quarto views use), so the browser paints it with the HTML and the stylesheet, in about a tenth of a second; when the app has loaded it draws the page again, with its controls, and puts it in the prerendered one's place, copying the two costly parts from it instead of working them out again (`src/ui/prerender.ts`). 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 the home page first. A checkout served as it is (`pixi run dev`) has no prerendered page and draws the home page as before. And `scripts/deploy.sh <slug> [user@host]` publishes `site/` to `/srv/www/<slug>` as an atomic release:
|
||||
|
||||
```
|
||||
/srv/www/pplus -> symlink to the live release
|
||||
|
||||
+4
-1
@@ -18,7 +18,9 @@
|
||||
<link rel="stylesheet" href="./styles/course/tokens.css">
|
||||
<link rel="stylesheet" href="./styles/course/kit.css">
|
||||
<link rel="stylesheet" href="./styles/course/asm86.css">
|
||||
<script>try { var t = localStorage.getItem("pplus.theme"); if (t === "light" || t === "dark") document.documentElement.setAttribute("data-theme", t); } catch (e) { }</script>
|
||||
<!-- before the first paint: whether the address is the home page, whose prerendered copy (scripts/assemble-page.mjs)
|
||||
shows only then, and the reader's theme -->
|
||||
<script>try { if (/^(#\/?(#.*)?)?$/.test(location.hash)) document.documentElement.setAttribute("data-home", ""); var t = localStorage.getItem("pplus.theme"); if (t === "light" || t === "dark") document.documentElement.setAttribute("data-theme", t); } catch (e) { }</script>
|
||||
<link rel="icon"
|
||||
href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><style>rect{fill:%23f6c453}text{fill:%2316191f}@media (prefers-color-scheme:dark){rect{fill:%230e1117}text{fill:%23f0c05a}}</style><rect width='16' height='16' rx='3'/><text x='8' y='12' font-size='8' text-anchor='middle' font-family='monospace' font-weight='700'>P+</text></svg>">
|
||||
</head>
|
||||
@@ -28,6 +30,7 @@
|
||||
<!-- replaced as soon as the app starts; shown only if it has not after a few seconds (styles.css, .boot-note) -->
|
||||
<p class="boot-note">Programming+ did not start. It has to be served by a web server (<code>pixi run dev</code>,
|
||||
or the site), not opened as a file, and its scripts must be allowed to run. Reload to try again.</p>
|
||||
<!--prerendered-home-->
|
||||
</div>
|
||||
<!-- pplus-core's modules (core/, a git submodule built into core/dist): the app imports them as #core/... -->
|
||||
<script type="importmap">{ "imports": { "#core/": "./core/dist/" } }</script>
|
||||
|
||||
@@ -3,16 +3,19 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// The assembled site's index.html, from the one in the repository: scripts/assemble.sh runs this on site/.
|
||||
// node scripts/assemble-page.mjs <site-dir>
|
||||
// It adds two things a checkout served as it is (pixi run dev) does without:
|
||||
// It adds three things a checkout served as it is (pixi run dev) does without:
|
||||
// - <meta name="pplus-version">, the commit in site/VERSION, which the footer shows (no request needed);
|
||||
// - a <link rel="modulepreload"> for every module the page's static import graph reaches. Without a bundler the
|
||||
// browser finds the modules one level of imports at a time, so a first visit waits for a chain of round trips
|
||||
// (six levels today); with the list it asks for them all at once. The links go after the import map, which a
|
||||
// browser must have read before it loads any module.
|
||||
// browser must have read before it loads any module;
|
||||
// - the home page, already drawn, inside #app (src/ui/prerender.ts): a visitor sees it with the HTML and the
|
||||
// stylesheet, not after every module has loaded and the page's examples have been run (about a second of work).
|
||||
// It is drawn here by the app's own homeView, in Node, on the small document of src/quarto/dom.ts.
|
||||
// Nothing else in the page changes, so the hashes of its inline scripts (docs/Caddyfile's CSP) still hold.
|
||||
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
|
||||
import { resolve, dirname, relative, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
|
||||
const IMPORT = /(?:^|[;\n}])\s*(?:import|export)\s*(?:[\w*{}\s,$]*?\sfrom\s*)?['"]([^'"]+)['"]/g;
|
||||
|
||||
@@ -43,14 +46,41 @@ export function modulePreloads(root, entry = 'dist/main.js') {
|
||||
return [...depth].filter(([f]) => f !== start).sort((a, b) => a[1] - b[1] || (a[0] < b[0] ? -1 : 1)).map(([f]) => `./${relative(root, f).split('\\').join('/')}`);
|
||||
}
|
||||
|
||||
/** Where index.html takes the prerendered home page: a comment inside #app. */
|
||||
export const HOME_MARK = '<!--prerendered-home-->';
|
||||
|
||||
/**
|
||||
* The page with the version and the preloads in it.
|
||||
* The home page as the app draws it, as HTML: the built app's own homeView, run here on the small document.
|
||||
* @param {string} dist the built app's dist/ (it imports core through the package's #core/ mapping)
|
||||
* @param {string | null} [version] the commit the footer names
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
export async function prerenderHome(dist, version = null) {
|
||||
const at = (p) => pathToFileURL(join(dist, p)).href;
|
||||
const { installDocument } = await import(at('quarto/dom.js'));
|
||||
installDocument();
|
||||
(await import(at('ui/footer.js'))).givePageVersion(version);
|
||||
const { homeView } = await import(at('ui/home.js'));
|
||||
return homeView().outerHTML;
|
||||
}
|
||||
|
||||
/**
|
||||
* The page with the version, the preloads and the prerendered home page in it.
|
||||
* @param {string} html index.html as the repository has it
|
||||
* @param {{ version?: string | null, preloads?: string[] }} parts
|
||||
* @param {{ version?: string | null, preloads?: string[], home?: string | null }} parts
|
||||
* @returns {string}
|
||||
*/
|
||||
export function assemblePage(html, { version = null, preloads = [] } = {}) {
|
||||
export function assemblePage(html, { version = null, preloads = [], home = null } = {}) {
|
||||
let out = html;
|
||||
if (home) {
|
||||
if (!out.includes(HOME_MARK)) throw new Error(`index.html has no ${HOME_MARK}: the prerendered home page goes there`);
|
||||
// a function, so a $ in the page's text is not read as a replacement pattern
|
||||
out = out.replace(HOME_MARK, () => `<div class="home-prerendered">${home}</div>`);
|
||||
// the home page's Quarto example is styled by the extension's stylesheet, which the app adds when it draws the
|
||||
// page (src/ui/quarto.ts, marked the same way so it is not added twice): without it here the prerendered page
|
||||
// is a few lines taller than the one that takes its place
|
||||
out = out.replace(/\n<\/head>/, '\n <link rel="stylesheet" href="./quarto/_extensions/pplus/pplus.css" data-pplus-ext="1">\n</head>');
|
||||
}
|
||||
if (version && /^[0-9a-f]{7,40}$/.test(version)) out = out.replace(/\n<\/head>/, `\n <meta name="pplus-version" content="${version}">\n</head>`);
|
||||
const map = /<script type="importmap">[^<]*<\/script>\n/.exec(out);
|
||||
if (!map) throw new Error('index.html has no import map: the preloads go after it');
|
||||
@@ -64,6 +94,8 @@ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.ur
|
||||
const versionFile = join(site, 'VERSION');
|
||||
const version = existsSync(versionFile) ? readFileSync(versionFile, 'utf8').trim() : null;
|
||||
const preloads = modulePreloads(site);
|
||||
writeFileSync(page, assemblePage(readFileSync(page, 'utf8'), { version, preloads }));
|
||||
console.log(`index.html: ${preloads.length} module preloads${version ? `, version ${version.slice(0, 12)}` : ''}`);
|
||||
// drawn by the checkout's own build, which is what assemble.sh copied into the site
|
||||
const home = await prerenderHome(resolve(dirname(fileURLToPath(import.meta.url)), '..', 'dist'), version);
|
||||
writeFileSync(page, assemblePage(readFileSync(page, 'utf8'), { version, preloads, home }));
|
||||
console.log(`index.html: ${preloads.length} module preloads, the home page prerendered (${Math.round(home.length / 1024)} kB)${version ? `, version ${version.slice(0, 12)}` : ''}`);
|
||||
}
|
||||
+7
-2
@@ -12,6 +12,7 @@ import { parseRoute, decodeState, loadLocal, migrateLocal, type PlayState, embed
|
||||
import { takeCrashed, settle, setAside, markBusy } from './core/guard.js';
|
||||
import { notice, messagePage } from './ui/notice.js';
|
||||
import { homeView } from './ui/home.js';
|
||||
import { prerenderedHome } from './ui/prerender.js';
|
||||
import { siteNav } from './ui/nav.js';
|
||||
import { quartoView } from './ui/quarto.js';
|
||||
import { libraryView } from './ui/library.js';
|
||||
@@ -133,7 +134,9 @@ async function show(): Promise<void> {
|
||||
// the same static page with only the anchor changed: scroll, do not rebuild
|
||||
if (!shell && anchorOnly(location.hash, app.firstElementChild ? [...app.firstElementChild.classList] : null)) { scrollToAnchor(); return; }
|
||||
shell?.destroy(); shell = null;
|
||||
clear(app);
|
||||
// the home page drawn by the assembler stays on screen until the one with its controls is ready to take its place
|
||||
const prerendered = route.kind === 'home' ? prerenderedHome() : null;
|
||||
if (!prerendered) clear(app);
|
||||
document.body.classList.remove('is-embed');
|
||||
let resolved: Resolved;
|
||||
try { resolved = await stateForRoute(); } catch (e: any) {
|
||||
@@ -150,7 +153,9 @@ async function show(): Promise<void> {
|
||||
if (!resolved && route.kind === 'library') { add(app, libraryView(route.id)); document.title = `${route.id ? `${route.id} · ` : ''}Library · Programming+`; window.scrollTo(0, 0); return; }
|
||||
if (!resolved && route.kind === 'quarto') { add(app, quartoView()); document.title = 'With Quarto · Programming+'; if (route.anchor) scrollToAnchor(true); else window.scrollTo(0, 0); return; }
|
||||
if (!resolved) {
|
||||
add(app, homeView()); document.title = HOME_TITLE; scrollToAnchor(true);
|
||||
const home = homeView();
|
||||
if (prerendered) clear(app);
|
||||
add(app, home); document.title = HOME_TITLE; scrollToAnchor(true);
|
||||
if (isUnknownRoute(location.hash)) notice('There is no page at that address, so this is the home page. Links from before the three areas open here too.');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,13 @@ import { AREAS, AREA_ORDER } from '../core/areas.js';
|
||||
import { startFor } from './nav.js';
|
||||
import { DISCLAIMER, LICENSE_FILES, SITE_DATA, SOURCE_URL, siteVersion } from '../core/site.js';
|
||||
|
||||
/** The commit, said by whoever draws the page outside a browser (the assembler, for the prerendered home page). */
|
||||
let given: string | null = null;
|
||||
export function givePageVersion(v: string | null): void { given = siteVersion(v); }
|
||||
|
||||
/** The commit the assembled page names (scripts/assemble-page.mjs), or null for a checkout served as it is. */
|
||||
function pageVersion(): string | null {
|
||||
if (given) return given;
|
||||
try { return siteVersion(document.querySelector('meta[name="pplus-version"]')?.getAttribute('content')); }
|
||||
catch { return null; } // outside a browser (the small document of src/quarto/dom.ts knows no attribute selectors)
|
||||
}
|
||||
|
||||
+12
-5
@@ -31,6 +31,7 @@ import { lineView } from './hw/lines.js';
|
||||
import { quartoTeaser } from './quarto.js';
|
||||
import { siteFooter } from './footer.js';
|
||||
import { DESCRIPTION } from '../core/site.js';
|
||||
import { kept } from './prerender.js';
|
||||
/** A register's value as short hex: what changed reads at a glance, without sixteen digits of zeros. */
|
||||
const hex64 = (v: bigint | number): string => `0x${BigInt.asUintN(64, BigInt(v)).toString(16)}`;
|
||||
|
||||
@@ -199,6 +200,10 @@ function whyMojo(): HTMLElement {
|
||||
|
||||
/** Two real reports: count_base's Mojo, and a pseudo exercise with a bug the fuzzer finds. */
|
||||
function testsCard(): HTMLElement {
|
||||
// the fuzzer's two reports take about a second: worked out once, when the site is assembled (prerender.ts)
|
||||
return kept('tests-card', testsCardBuilt);
|
||||
}
|
||||
function testsCardBuilt(): HTMLElement {
|
||||
const report = (id: string, lang: LangId) => { const st = stateForProblem(problemById(id)!); st.lang = lang; return new Session(st).tests()[0]; };
|
||||
const ok = report('count-base', 'mojo');
|
||||
const bad = report('match-off-by-one', 'pseudo');
|
||||
@@ -257,10 +262,12 @@ function specs(): HTMLElement {
|
||||
const proved = problemById('count-base-proved')!;
|
||||
// the site's own example, proved with no time on the clock: its work is small and counted, and what the home
|
||||
// page says of it must not depend on how fast or busy the reader's device is
|
||||
const session = new Session(stateForProblem(proved));
|
||||
session.proofBudgetMs = Infinity;
|
||||
const pr = session.proof();
|
||||
const c = pr?.report.counts ?? { proved: 0, broken: 0, open: 0, total: 0 };
|
||||
const counts = kept('proof-counts', () => {
|
||||
const session = new Session(stateForProblem(proved));
|
||||
session.proofBudgetMs = Infinity;
|
||||
const c = session.proof()?.report.counts ?? { proved: 0, broken: 0, open: 0, total: 0 };
|
||||
return el('div', { class: 'proof-counts' }, el('span', { class: 'badge badge-proved' }, `${c.proved} proved`), el('span', { class: 'badge badge-broken' }, `${c.broken} broken`), el('span', { class: 'badge badge-open' }, `${c.open} open`));
|
||||
});
|
||||
return el('section', { class: 'spec-section' },
|
||||
el('div', { class: 'spec-text' },
|
||||
el('h2', {}, 'Say what must be true, then check it'),
|
||||
@@ -269,7 +276,7 @@ function specs(): HTMLElement {
|
||||
el('div', { class: 'way way-speco' },
|
||||
el('h3', {}, 'speco'),
|
||||
sample(proved.sources.speco || '', 'speco', 8),
|
||||
el('div', { class: 'proof-counts' }, el('span', { class: 'badge badge-proved' }, `${c.proved} proved`), el('span', { class: 'badge badge-broken' }, `${c.broken} broken`), el('span', { class: 'badge badge-open' }, `${c.open} open`))));
|
||||
counts));
|
||||
}
|
||||
|
||||
export function homeView(): HTMLElement {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
/**
|
||||
* The home page, drawn when the site is assembled. Drawing it runs the engines on its own examples (a fuzzer's
|
||||
* report alone takes about a second), and until the page's 150 modules had loaded and that work was done a visitor
|
||||
* saw nothing. So `scripts/assemble-page.mjs` draws the home page once, in Node, and puts the result inside
|
||||
* `#app` as `.home-prerendered`: the browser paints it with the HTML and the stylesheet. When the app starts it
|
||||
* draws the home page again and puts it in the prerendered one's place (src/main.ts), which gives the page its
|
||||
* controls; the parts that are costly to work out and hold no control are taken from the prerendered page rather
|
||||
* than worked out again (`kept`). The prerendered page shows only on the home address: the inline script in
|
||||
* index.html marks it (`data-home`) before the first paint, and without the mark it stays hidden, so an embed or
|
||||
* a problem's address never shows the home page first.
|
||||
*/
|
||||
export const PRERENDERED = 'home-prerendered';
|
||||
|
||||
/** The prerendered home page still in the document, or null (a checkout served as it is, a later visit to the home page). */
|
||||
export function prerenderedHome(): Element | null {
|
||||
try { return typeof document === 'undefined' ? null : document.querySelector(`.${PRERENDERED}`); } catch { return null; }
|
||||
}
|
||||
|
||||
/**
|
||||
* A part of the home page that is costly to work out and has nothing to press in it: built by `build` where the page
|
||||
* is drawn first (the assembler, or a browser with no prerendered page), and copied from the prerendered page where
|
||||
* there is one. The copy is what the same build's assembler worked out from the same catalog.
|
||||
*/
|
||||
export function kept<T extends HTMLElement>(key: string, build: () => T): T {
|
||||
let pre: Element | null = null;
|
||||
try { pre = prerenderedHome()?.querySelector(`[data-kept="${key}"]`) ?? null; } catch { pre = null; }
|
||||
if (pre) return pre.cloneNode(true) as T;
|
||||
const node = build();
|
||||
node.setAttribute('data-kept', key);
|
||||
return node;
|
||||
}
|
||||
@@ -4343,6 +4343,13 @@ button.asm-src:hover {
|
||||
.compile-input-note { max-width: 70ch; }
|
||||
.compile-send-note { max-width: 70ch; }
|
||||
/* index.html's note for a page whose scripts never ran (opened as a file, scripts blocked, offline on first load) */
|
||||
/* the home page as the assembler drew it (src/ui/prerender.ts): shown at once on the home address (the inline script
|
||||
of index.html says so before the first paint), and never on another address or where that script did not run */
|
||||
.home-prerendered { display: none; }
|
||||
html[data-home] .home-prerendered { display: block; }
|
||||
/* with it on screen the note waits longer (the page is there, and its scripts may still be on their way) and takes
|
||||
no room above the page: it comes up as a bar at the foot of the window */
|
||||
html[data-home] #app:has(> .home-prerendered) .boot-note { animation-delay: 15s; position: fixed; left: 16px; right: 16px; bottom: 16px; z-index: 20; margin: 0 auto; padding: 10px 14px; border-radius: var(--r); background: var(--warn-bg); pointer-events: none; }
|
||||
.boot-note { max-width: 640px; margin: 64px auto; padding: 0 16px; color: var(--ink-2); font: 16px/1.5 var(--sans); opacity: 0; animation: boot-note-show 0s 4s forwards; }
|
||||
@keyframes boot-note-show { to { opacity: 1; } }
|
||||
|
||||
|
||||
+41
-1
@@ -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\)/);
|
||||
});
|
||||
Reference in new issue
Block a user