Skip to content

Commit eadde4a

Browse files
authored
fix(ui): show recovery when Control UI does not render (#124861)
* fix(ui): retire bootstrap fallback after render * test(ui): avoid returning from Promise executor
1 parent 358c06e commit eadde4a

6 files changed

Lines changed: 235 additions & 36 deletions

File tree

‎docs/web/control-ui.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ Then point the UI at your Gateway WS URL (e.g. `ws://127.0.0.1:18789`).
754754

755755
## Blank Control UI page
756756

757-
If the browser loads a blank dashboard and DevTools shows no useful error, an extension or early content script may have prevented the JavaScript module app from evaluating. The static page includes a plain HTML recovery panel that appears when `<openclaw-app>` is not registered after startup.
757+
If the browser loads a blank dashboard and DevTools shows no useful error, an extension or early content script may have prevented the JavaScript module app from evaluating. The static page includes a plain HTML recovery panel that appears when `<openclaw-app>` does not complete its first render after startup.
758758

759759
Use the panel's **Try again** action after changing the browser environment, or reload manually after these checks:
760760

‎ui/index.html‎

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ <h1 id="openclaw-mount-fallback-title">Control UI did not start</h1>
313313
</section>
314314
<script>
315315
(function () {
316-
var tagName = "openclaw-app";
317-
var app = document.querySelector(tagName);
316+
var app = document.querySelector("openclaw-app");
318317
var fallback = document.getElementById("openclaw-mount-fallback");
319318
if (!app || !fallback) return;
320319

@@ -330,6 +329,7 @@ <h1 id="openclaw-mount-fallback-title">Control UI did not start</h1>
330329
var recoveryAttempt = 0;
331330
var recoveryInFlight = false;
332331
var recoveryNavigation = false;
332+
var appStarted = false;
333333

334334
try {
335335
var initialUrl = new URL(window.location.href);
@@ -340,19 +340,6 @@ <h1 id="openclaw-mount-fallback-title">Control UI did not start</h1>
340340
}
341341
} catch (e) {}
342342

343-
function appMounted() {
344-
try {
345-
return Boolean(
346-
app.isConnected &&
347-
window.customElements &&
348-
typeof window.customElements.get === "function" &&
349-
window.customElements.get(tagName),
350-
);
351-
} catch (e) {
352-
return false;
353-
}
354-
}
355-
356343
function hideFallback() {
357344
fallback.hidden = true;
358345
document.body.classList.remove("openclaw-mount-fallback-active");
@@ -364,14 +351,14 @@ <h1 id="openclaw-mount-fallback-title">Control UI did not start</h1>
364351

365352
function scheduleRecovery() {
366353
window.clearTimeout(recoveryTimer);
367-
if (appMounted() || recoveryAttempt >= maxRecoveryAttempts) return;
354+
if (appStarted || recoveryAttempt >= maxRecoveryAttempts) return;
368355
var retryDelay = Math.min(delay, 1000 * Math.pow(2, recoveryAttempt));
369356
recoveryTimer = window.setTimeout(retryCurrentDocument, retryDelay);
370357
}
371358

372359
function finishRecoveryAttempt() {
373360
recoveryInFlight = false;
374-
if (appMounted()) return;
361+
if (appStarted) return;
375362
if (recoveryAttempt >= maxRecoveryAttempts) {
376363
setSummary(
377364
"The gateway is still unavailable. Try again, then check the troubleshooting guide if the problem persists.",
@@ -386,7 +373,7 @@ <h1 id="openclaw-mount-fallback-title">Control UI did not start</h1>
386373

387374
function retryCurrentDocument() {
388375
if (
389-
appMounted() ||
376+
appStarted ||
390377
recoveryInFlight ||
391378
recoveryAttempt >= maxRecoveryAttempts ||
392379
typeof window.fetch !== "function"
@@ -414,7 +401,7 @@ <h1 id="openclaw-mount-fallback-title">Control UI did not start</h1>
414401
})
415402
.then(function (response) {
416403
if (!response.ok) throw new Error("gateway unavailable");
417-
window.location.replace(documentUrl.href);
404+
if (!appStarted) window.location.replace(documentUrl.href);
418405
})
419406
.catch(function () {
420407
finishRecoveryAttempt();
@@ -425,7 +412,7 @@ <h1 id="openclaw-mount-fallback-title">Control UI did not start</h1>
425412
}
426413

427414
function showFallback() {
428-
if (appMounted()) return;
415+
if (appStarted) return;
429416
retryCurrentDocument();
430417
fallback.hidden = false;
431418
document.body.classList.add("openclaw-mount-fallback-active");
@@ -445,16 +432,16 @@ <h1 id="openclaw-mount-fallback-title">Control UI did not start</h1>
445432

446433
armFallbackTimer();
447434

448-
if (window.customElements && typeof window.customElements.whenDefined === "function") {
449-
window.customElements.whenDefined(tagName).then(
450-
function () {
451-
window.clearTimeout(timer);
452-
window.clearTimeout(recoveryTimer);
453-
hideFallback();
454-
},
455-
function () {},
456-
);
457-
}
435+
window.addEventListener(
436+
"openclaw-control-ui-rendered",
437+
function () {
438+
appStarted = true;
439+
window.clearTimeout(timer);
440+
window.clearTimeout(recoveryTimer);
441+
hideFallback();
442+
},
443+
{ once: true },
444+
);
458445

459446
if (retry) {
460447
retry.addEventListener("click", function () {

‎ui/src/app/app-root.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ export class OpenClawApp extends OpenClawLightDomElement {
151151
super.disconnectedCallback();
152152
}
153153

154+
protected override firstUpdated(): void {
155+
if (this.runtime) {
156+
globalThis.dispatchEvent(new Event("openclaw-control-ui-rendered"));
157+
}
158+
}
159+
154160
private synchronizeGateway(gateway: ApplicationContext["gateway"]) {
155161
const sourceChanged = gateway !== this.loginGatewaySource;
156162
if (sourceChanged) {

‎ui/src/app/mount-fallback.test.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ describe("Control UI mount fallback", () => {
114114
},
115115
);
116116

117-
it("shows the static troubleshooting panel when the app element is never registered", async () => {
117+
it("shows the static troubleshooting panel when the app never renders", async () => {
118118
const frameWindow = createIsolatedWindow();
119-
expect(frameWindow.customElements.get("openclaw-app")).toBeUndefined();
120119
installFallbackShell(frameWindow, await readIndexHtmlWithDelay(1));
121120
await waitForWindowTimeout(frameWindow, 10);
122121

@@ -147,20 +146,25 @@ describe("Control UI mount fallback", () => {
147146
expect(fallback.hidden).toBe(false);
148147
});
149148

150-
it("keeps the fallback hidden when the app element registers before the timeout", async () => {
149+
it("keeps the fallback visible until the app completes its first render", async () => {
151150
const frameWindow = createIsolatedWindow();
152-
installFallbackShell(frameWindow, await readIndexHtmlWithDelay(25));
151+
installFallbackShell(frameWindow, await readIndexHtmlWithDelay(1));
153152
if (!frameWindow.customElements.get("openclaw-app")) {
154153
frameWindow.customElements.define("openclaw-app", class extends frameWindow.HTMLElement {});
155154
}
156155
await frameWindow.customElements.whenDefined("openclaw-app");
157-
await waitForWindowTimeout(frameWindow, 35);
156+
await waitForWindowTimeout(frameWindow, 10);
158157

159158
const fallback = requireElementById(
160159
frameWindow,
161160
"openclaw-mount-fallback",
162161
frameWindow.HTMLElement,
163162
);
163+
expect(fallback.hidden).toBe(false);
164+
expect([...frameWindow.document.body.classList]).toEqual(["openclaw-mount-fallback-active"]);
165+
166+
frameWindow.dispatchEvent(new frameWindow.Event("openclaw-control-ui-rendered"));
167+
164168
expect(fallback.hidden).toBe(true);
165169
expect([...frameWindow.document.body.classList]).toEqual([]);
166170
});

‎ui/src/e2e/login-gate.e2e.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,45 @@ suite.define(() => {
322322
}
323323
});
324324

325+
it("retires the static startup fallback after rendering auth-required guidance", async () => {
326+
const context = await suite.browser.newContext({ viewport: { height: 900, width: 1280 } });
327+
const page = await context.newPage();
328+
await page.addInitScript(() => {
329+
window.addEventListener("openclaw-control-ui-rendered", () => {
330+
const key = "openclaw.control-ui-e2e.render-count";
331+
const count = Number.parseInt(sessionStorage.getItem(key) ?? "0", 10);
332+
sessionStorage.setItem(key, String(count + 1));
333+
});
334+
});
335+
await page.clock.install();
336+
const gateway = await installMockGateway(page, { deferredMethods: ["connect"] });
337+
338+
try {
339+
await page.goto(suite.server.baseUrl);
340+
await gateway.waitForRequest("connect");
341+
await gateway.rejectDeferred("connect", {
342+
code: "INVALID_REQUEST",
343+
message: "token missing",
344+
details: { code: ConnectErrorDetailCodes.AUTH_TOKEN_MISSING },
345+
});
346+
347+
const authRequired = page.locator('.login-gate__failure[data-kind="auth-required"]');
348+
await authRequired.waitFor({ timeout: 10_000 });
349+
await page.clock.runFor(12_001);
350+
351+
expect(await authRequired.isVisible()).toBe(true);
352+
expect(await page.locator("#openclaw-mount-fallback").isHidden()).toBe(true);
353+
expect((await page.locator("body").getAttribute("class")) ?? "").not.toContain(
354+
"openclaw-mount-fallback-active",
355+
);
356+
expect(
357+
await page.evaluate(() => sessionStorage.getItem("openclaw.control-ui-e2e.render-count")),
358+
).toBe("1");
359+
} finally {
360+
await closeContext(context);
361+
}
362+
});
363+
325364
it("keeps mobile controls compact, touchable, and keyboard-friendly", async () => {
326365
const context = await suite.browser.newContext({
327366
hasTouch: true,
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import { readFile } from "node:fs/promises";
2+
import { createServer } from "node:http";
3+
import path from "node:path";
4+
import type { Page } from "playwright";
5+
import { expect, it } from "vitest";
6+
import { pauseVirtualClock, type ControlUiE2eServer } from "../test-helpers/control-ui-e2e.ts";
7+
import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts";
8+
9+
const indexHtmlPath = path.resolve(
10+
process.cwd(),
11+
path.basename(process.cwd()) === "ui" ? "index.html" : "ui/index.html",
12+
);
13+
const renderEvent = "openclaw-control-ui-rendered";
14+
const loadCountKey = "openclaw.control-ui-e2e.mount-fallback-loads";
15+
const renderCountKey = "openclaw.control-ui-e2e.mount-fallback-renders";
16+
let syntheticModuleRenders = false;
17+
18+
async function startRegisteredElementFixture(): Promise<ControlUiE2eServer> {
19+
const indexHtml = await readFile(indexHtmlPath, "utf8");
20+
const server = createServer((request, response) => {
21+
const requestUrl = new URL(request.url ?? "/", "http://127.0.0.1");
22+
if (requestUrl.pathname === "/src/main.ts") {
23+
response.setHeader("content-type", "text/javascript; charset=utf-8");
24+
response.end(
25+
syntheticModuleRenders
26+
? `customElements.define("openclaw-app", class extends HTMLElement {
27+
connectedCallback() {
28+
this.textContent = "Application rendered";
29+
window.dispatchEvent(new Event(${JSON.stringify(renderEvent)}));
30+
}
31+
});`
32+
: 'customElements.define("openclaw-app", class extends HTMLElement {});',
33+
);
34+
return;
35+
}
36+
if (requestUrl.pathname === "/") {
37+
response.setHeader("content-type", "text/html; charset=utf-8");
38+
response.end(indexHtml);
39+
return;
40+
}
41+
response.statusCode = 404;
42+
response.end();
43+
});
44+
await new Promise<void>((resolve, reject) => {
45+
server.once("error", reject);
46+
server.listen(0, "127.0.0.1", resolve);
47+
});
48+
const address = server.address();
49+
if (!address || typeof address === "string") {
50+
throw new Error("Registered-element fixture did not acquire a loopback port");
51+
}
52+
return {
53+
baseUrl: `http://127.0.0.1:${address.port}/`,
54+
close: () =>
55+
new Promise<void>((resolve, reject) => {
56+
server.close((error) => (error ? reject(error) : resolve()));
57+
}),
58+
};
59+
}
60+
61+
async function addLifecycleCounters(page: Page): Promise<void> {
62+
await page.addInitScript(
63+
({ loadKey, renderedEvent, renderKey }) => {
64+
const loadCount = Number.parseInt(sessionStorage.getItem(loadKey) ?? "0", 10);
65+
sessionStorage.setItem(loadKey, String(loadCount + 1));
66+
window.addEventListener(renderedEvent, () => {
67+
const renderCount = Number.parseInt(sessionStorage.getItem(renderKey) ?? "0", 10);
68+
sessionStorage.setItem(renderKey, String(renderCount + 1));
69+
});
70+
},
71+
{ loadKey: loadCountKey, renderedEvent: renderEvent, renderKey: renderCountKey },
72+
);
73+
}
74+
75+
const registeredElementSuite = createControlUiE2eSuite({
76+
name: "Control UI static mount fallback E2E",
77+
startServer: startRegisteredElementFixture,
78+
startServerBeforeBrowser: true,
79+
});
80+
81+
registeredElementSuite.define(() => {
82+
it("shows the fallback when registration never produces an application render", async () => {
83+
syntheticModuleRenders = false;
84+
await registeredElementSuite.withPage(
85+
{ serviceWorkers: "block", viewport: { height: 900, width: 1280 } },
86+
async ({ page }) => {
87+
await addLifecycleCounters(page);
88+
await page.clock.install();
89+
await pauseVirtualClock(page);
90+
await page.goto(registeredElementSuite.server.baseUrl, { waitUntil: "domcontentloaded" });
91+
await page.waitForFunction(() => customElements.get("openclaw-app") !== undefined);
92+
93+
expect(await page.locator("openclaw-app").textContent()).toBe("");
94+
expect(
95+
await page.evaluate((key) => sessionStorage.getItem(key), renderCountKey),
96+
).toBeNull();
97+
98+
await page.clock.runFor(12_001);
99+
await page.waitForFunction((key) => sessionStorage.getItem(key) === "2", loadCountKey);
100+
await page.clock.runFor(12_001);
101+
102+
await page.getByRole("heading", { name: "Control UI did not start" }).waitFor();
103+
expect(await page.getByRole("button", { name: "Try again" }).isVisible()).toBe(true);
104+
expect(await page.getByRole("button", { name: "Keep waiting" }).isVisible()).toBe(true);
105+
expect(
106+
await page.evaluate((key) => sessionStorage.getItem(key), renderCountKey),
107+
).toBeNull();
108+
109+
syntheticModuleRenders = true;
110+
await Promise.all([
111+
page.waitForNavigation({ waitUntil: "domcontentloaded" }),
112+
page.getByRole("button", { name: "Try again" }).click(),
113+
]);
114+
await page.getByText("Application rendered", { exact: true }).waitFor();
115+
await page.clock.runFor(12_001);
116+
117+
expect(await page.locator("#openclaw-mount-fallback").isHidden()).toBe(true);
118+
expect(await page.evaluate((key) => sessionStorage.getItem(key), renderCountKey)).toBe("1");
119+
},
120+
);
121+
});
122+
});
123+
124+
const runtimeFailureSuite = createControlUiE2eSuite({
125+
name: "Control UI failed runtime mount E2E",
126+
startServerBeforeBrowser: true,
127+
});
128+
129+
runtimeFailureSuite.define(() => {
130+
it("does not complete startup when application runtime creation throws", async () => {
131+
await runtimeFailureSuite.withPage(
132+
{ serviceWorkers: "block", viewport: { height: 900, width: 1280 } },
133+
async ({ page }) => {
134+
await addLifecycleCounters(page);
135+
await page.addInitScript(() => {
136+
const browserGetComputedStyle = globalThis.getComputedStyle.bind(globalThis);
137+
Object.defineProperty(globalThis, "getComputedStyle", {
138+
configurable: true,
139+
value: (element: Element, pseudoElement?: string | null) => {
140+
if (customElements.get("openclaw-app")) {
141+
throw new Error("forced application runtime creation failure");
142+
}
143+
return browserGetComputedStyle(element, pseudoElement);
144+
},
145+
});
146+
});
147+
await page.clock.install();
148+
await pauseVirtualClock(page);
149+
await page.goto(runtimeFailureSuite.server.baseUrl, { waitUntil: "domcontentloaded" });
150+
await page.waitForFunction(() => customElements.get("openclaw-app") !== undefined);
151+
152+
await page.clock.runFor(12_001);
153+
await page.waitForFunction((key) => sessionStorage.getItem(key) === "2", loadCountKey);
154+
await page.clock.runFor(12_001);
155+
156+
await page.getByRole("heading", { name: "Control UI did not start" }).waitFor();
157+
expect(
158+
await page.evaluate((key) => sessionStorage.getItem(key), renderCountKey),
159+
).toBeNull();
160+
},
161+
);
162+
});
163+
});

0 commit comments

Comments
 (0)