Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

Route change: the view transition's update callback no longer waits for requestAnimationFrame (#3673)

Niko1221642f5120a7f1
diff --git a/apps/host/src/lib/router.ts b/apps/host/src/lib/router.ts
index be48fa4e7..18c3f04c2 100644
--- a/apps/host/src/lib/router.ts
+++ b/apps/host/src/lib/router.ts
@@ -74,7 +74,7 @@ function normalise(loc: Location): void {
* Navigate, morphing between screens when the browser can.
*
* A view transition needs the DOM update to happen inside its callback, and
- * React commits asynchronously — so we wait two frames before resolving. It is
+ * React commits asynchronously — so we wait for the commit (never for a frame: see afterRouteUpdate). It is
* skipped while a response is streaming (the transcript is being appended to on
* every token, and snapshotting it mid-stream would fight the SSE updates) and
* when the user has asked for reduced motion.
@@ -100,6 +100,34 @@ export function isWebKitGtk(ua: string = navigator.userAgent): boolean {
return /AppleWebKit/.test(ua) && /Linux|X11/.test(ua) && !/Chrome|Chromium|CriOS|Edg|OPR|Android/.test(ua);
}
+/**
+ * Resolve once the new route has been drawn into the page, or after a short deadline, whichever comes first.
+ *
+ * The update callback of a view transition must not wait for requestAnimationFrame: while the callback is pending the browser holds
+ * rendering (Chromium and Firefox both do), so a frame callback never runs, and every click waits for the browser's own guard,
+ * about 4 s in Chromium and the WebView2 window, about 5 s in Firefox (#3673). A DOM change after the hash flipped means React has
+ * committed the new screen; the deadline covers a route that changes nothing visible.
+ */
+export function afterRouteUpdate(done: () => void, timeoutMs = 150): void {
+ let settled = false;
+ let observer: MutationObserver | undefined;
+ const finish = () => {
+ if (settled) return;
+ settled = true;
+ observer?.disconnect();
+ done();
+ };
+ try {
+ if (typeof MutationObserver === 'function' && typeof document !== 'undefined' && document.body) {
+ observer = new MutationObserver(() => setTimeout(finish, 0));
+ observer.observe(document.body, { childList: true, subtree: true });
+ }
+ } catch {
+ /* the deadline alone is enough */
+ }
+ setTimeout(finish, timeoutMs);
+}
+
function navigateHash(next: string, morph = true) {
if (window.location.hash === next) return;
@@ -121,7 +149,7 @@ function navigateHash(next: string, morph = true) {
() =>
new Promise<void>((resolve) => {
window.location.hash = next;
- requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
+ afterRouteUpdate(resolve);
}),
);
// A transition is *expected* to be skipped when one is already running (rapid
diff --git a/apps/host/tests/router-view-transition.test.mjs b/apps/host/tests/router-view-transition.test.mjs
new file mode 100644
index 000000000..4ccbc938c
--- /dev/null
+++ b/apps/host/tests/router-view-transition.test.mjs
@@ -0,0 +1,50 @@
+/** #3673: the route change froze every click for 4-5 s (Chromium and the WebView2 window about 4 s, Firefox about 5 s) because the view
+ transition's update callback waited for requestAnimationFrame, which the browser holds while that callback is pending. */
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { build } from 'esbuild';
+import { fileURLToPath } from 'node:url';
+
+const result = await build({ entryPoints: [fileURLToPath(new URL('../src/lib/router.ts', import.meta.url))], bundle: true, format: 'esm', platform: 'node', write: false, logLevel: 'silent', alias: { react: fileURLToPath(new URL('./helpers/react-stub.mjs', import.meta.url)) } });
+globalThis.window ??= globalThis;
+const { afterRouteUpdate } = await import(`data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString('base64')}`);
+
+test('a held requestAnimationFrame cannot hold the route update: it resolves on the deadline', (t) => {
+ t.mock.timers.enable({ apis: ['setTimeout'] });
+ globalThis.requestAnimationFrame = () => 1; // the browser never calls back while the transition is pending
+ globalThis.document = { body: {} };
+ globalThis.MutationObserver = undefined; // nothing draws: only the deadline is left
+ let done = 0;
+ afterRouteUpdate(() => { done++; }, 150);
+ t.mock.timers.tick(149);
+ assert.equal(done, 0);
+ t.mock.timers.tick(1);
+ assert.equal(done, 1, 'resolved at the deadline, with no frame ever delivered');
+ t.mock.timers.tick(1000);
+ assert.equal(done, 1, 'resolves once');
+});
+
+test('the first change to the page after the hash flipped resolves it at once, and the observer is released', (t) => {
+ t.mock.timers.enable({ apis: ['setTimeout'] });
+ globalThis.requestAnimationFrame = () => 1;
+ let notify; let disconnected = false;
+ globalThis.document = { body: {} };
+ globalThis.MutationObserver = class { constructor(fn) { notify = fn; } observe() {} disconnect() { disconnected = true; } };
+ let done = 0;
+ afterRouteUpdate(() => { done++; }, 150);
+ notify(); // React committed the new screen
+ t.mock.timers.tick(0);
+ assert.equal(done, 1, 'well before the 150 ms deadline');
+ assert.equal(disconnected, true);
+ t.mock.timers.tick(500);
+ assert.equal(done, 1);
+});
+
+test('the transition callback no longer waits on a frame', async () => {
+ const { readFileSync } = await import('node:fs');
+ const text = readFileSync(new URL('../src/lib/router.ts', import.meta.url), 'utf8');
+ const callback = text.slice(text.indexOf('const transition = document.startViewTransition'), text.indexOf('transition.ready.catch'));
+ assert.ok(callback.length > 40);
+ assert.match(callback, /afterRouteUpdate\(resolve\)/);
+ assert.doesNotMatch(callback, /requestAnimationFrame/);
+});