From c5fe779643f9127821c4b090c8c36b99956f43bd Mon Sep 17 00:00:00 2001
From: Kevin Longe <273747162+kvlonge@users.noreply.github.com>
Date: Sat, 14 Nov 2026 12:28:00 +0000
Subject: [PATCH] fix(website): start the UI demo's engine in Firefox
chdb-wasm's worker hands Emscripten the downloaded wasm as bytes, and Firefox
then never finishes starting the engine. The website build now has the worker
stream the same blob URL, which starts in about a second in Firefox and
Chrome, and names the engine directory after the patched worker too, since
the engine's files are cached for good. The build fails if a chdb-wasm
upgrade changes the code it replaces.
The live engine panel keys its order rows by position, so an order the
engine holds twice no longer breaks the list, and the cross-origin isolation
test only rewrites the page's own headers instead of refetching every
request.
---
.../lib/demo/components/engine-panel.svelte | 3 ++-
website/scripts/build.mjs | 21 +++++++++++++++----
website/tests/ui-demo.test.mjs | 2 ++
3 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/ui/src/lib/demo/components/engine-panel.svelte b/ui/src/lib/demo/components/engine-panel.svelte
index c26974ac..6cd108de 100644
--- a/ui/src/lib/demo/components/engine-panel.svelte
+++ b/ui/src/lib/demo/components/engine-panel.svelte
@@ -106,7 +106,8 @@
- {#each snapshot.orders as order (order.order_id)}
+
+ {#each snapshot.orders as order, index (`${index}:${order.order_id}`)}
| #{order.order_id} | {order.status} | {dollars(order.total_cents)} |
{/each}
diff --git a/website/scripts/build.mjs b/website/scripts/build.mjs
index b09050af..0f6e7bf7 100644
--- a/website/scripts/build.mjs
+++ b/website/scripts/build.mjs
@@ -23,11 +23,23 @@ for (const [from, to] of Object.entries(redirects)) {
const ui = new URL('../ui/', root);
const chdb = new URL('node_modules/chdb-wasm/', root);
const { version: engineVersion } = JSON.parse(await readFile(new URL('package.json', chdb), 'utf8'));
-// The parts are cached for good, so their path names the exact bytes: gzip output can differ
-// between builds of the same version, and a half-cached old part must never join a new one.
+// The engine's files are cached for good, so their path names the exact bytes: gzip output can
+// differ between builds of the same version, and a half-cached old part must never join a new one.
const wasm = await readFile(new URL('dist/chdb.wasm', chdb));
const packed = gzipSync(wasm, { level: 9 });
-const engineBase = `/demo/engine/${engineVersion}-${createHash('sha256').update(packed).digest('hex').slice(0, 12)}/`;
+// The engine's worker hands Emscripten the downloaded wasm as bytes (wasmBinary), and Firefox then
+// never finishes starting it; streaming the same blob URL starts in about a second in every browser.
+const workerSource = await readFile(new URL('dist/worker.js', chdb), 'utf8');
+const wasmAsBytes = ` const bytes = await fetchWithProgress(payload.wasmUrl, id);
+ if (bytes)
+ moduleOpts.wasmBinary = bytes;`;
+if (!workerSource.includes(wasmAsBytes)) throw new Error(`chdb-wasm ${engineVersion}'s worker no longer loads the wasm as bytes; recheck the Firefox start.`);
+const worker = workerSource.replace(
+ wasmAsBytes,
+ ` moduleOpts.locateFile = (path, prefix) => (path.endsWith('.wasm') ? payload.wasmUrl : prefix + path);`,
+);
+const engineHash = createHash('sha256').update(packed).update(worker).digest('hex').slice(0, 12);
+const engineBase = `/demo/engine/${engineVersion}-${engineHash}/`;
if (!existsSync(new URL('node_modules/', ui))) {
// The UI's lockfile needs the npm it pins, as `make ui-install` uses.
const { packageManager } = JSON.parse(await readFile(new URL('package.json', ui), 'utf8'));
@@ -44,9 +56,10 @@ await cp(new URL('build/', ui), demo, { recursive: true });
// at most 25 MiB, and the page joins and gunzips them.
const engine = new URL(engineBase.slice(1), output);
await mkdir(engine, { recursive: true });
-for (const file of ['index.js', 'async.js', 'bindings.js', 'platform.js', 'protocol.js', 'status.js', 'worker.js', 'chdb.mjs']) {
+for (const file of ['index.js', 'async.js', 'bindings.js', 'platform.js', 'protocol.js', 'status.js', 'chdb.mjs']) {
await copyFile(new URL(`dist/${file}`, chdb), new URL(file, engine));
}
+await writeFile(new URL('worker.js', engine), worker);
await copyFile(new URL('LICENSE', chdb), new URL('LICENSE', engine));
const half = Math.ceil(packed.length / 2);
const parts = [packed.subarray(0, half), packed.subarray(half)];
diff --git a/website/tests/ui-demo.test.mjs b/website/tests/ui-demo.test.mjs
index 7c23f0f0..5cca7f40 100644
--- a/website/tests/ui-demo.test.mjs
+++ b/website/tests/ui-demo.test.mjs
@@ -126,7 +126,9 @@ test('on the real engine Execute, Promote and Roll back run, and every page read
});
test('without cross-origin isolation the demo says so and plays the recording', async ({ page }) => {
+ // Only the page's own headers decide isolation; other requests the page may drop mid-flight.
await page.route('**/demo/**', async (route) => {
+ if (route.request().resourceType() !== 'document') return route.continue();
const response = await route.fetch();
const headers = response.headers();
delete headers['cross-origin-embedder-policy'];