Skip to content

Commit 0c8f070

Browse files
authored
fix(gateway): anchor Control UI assets on deep routes (#114061)
1 parent 8684e7c commit 0c8f070

2 files changed

Lines changed: 58 additions & 7 deletions

File tree

‎src/gateway/control-ui.http.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,56 @@ describe("handleControlUiHttpRequest", () => {
10901090
});
10911091
});
10921092

1093+
it.each([
1094+
{
1095+
name: "root-mounted nested routes",
1096+
requestPath: "/settings/approvals",
1097+
basePath: undefined,
1098+
expectedPrefix: "",
1099+
},
1100+
{
1101+
name: "base-mounted nested routes",
1102+
requestPath: "/openclaw/settings/approvals",
1103+
basePath: "/openclaw",
1104+
expectedPrefix: "/openclaw",
1105+
},
1106+
])(
1107+
"anchors Vite-relative public asset hrefs for $name",
1108+
async ({ requestPath, basePath, expectedPrefix }) => {
1109+
const assets = [
1110+
"favicon.svg",
1111+
"favicon-32.png",
1112+
"apple-touch-icon.png",
1113+
"manifest.webmanifest",
1114+
];
1115+
const html = `<html><head>${assets
1116+
.map((asset) => `<link href="./${asset}" />`)
1117+
.join("")}</head><body></body></html>\n`;
1118+
1119+
await withControlUiRoot({
1120+
indexHtml: html,
1121+
fn: async (tmp) => {
1122+
const { res, end } = makeMockHttpResponse();
1123+
const handled = await handleControlUiHttpRequest(
1124+
{ url: requestPath, method: "GET" } as IncomingMessage,
1125+
res,
1126+
{
1127+
...(basePath ? { basePath } : {}),
1128+
root: { kind: "resolved", path: tmp },
1129+
},
1130+
);
1131+
1132+
expect(handled).toBe(true);
1133+
const body = String(end.mock.calls[0]?.[0] ?? "");
1134+
for (const asset of assets) {
1135+
expect(body).toContain(`href="${expectedPrefix}/${asset}"`);
1136+
expect(body).not.toContain(`href="./${asset}"`);
1137+
}
1138+
},
1139+
});
1140+
},
1141+
);
1142+
10931143
it("serves bootstrap config JSON", async () => {
10941144
await withControlUiRoot({
10951145
fn: async (tmp) => {

‎src/gateway/control-ui.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,18 @@ const CONTROL_UI_ROOT_PUBLIC_ASSETS = new Set([
125125
"sw.js",
126126
]);
127127

128-
/** Rewrites root-absolute Control UI public asset hrefs for configured base paths. */
128+
/** Anchors bundled public assets before deep-linked documents begin preloading. */
129129
function rewriteControlUiIndexHtmlPublicAssetHrefs(html: string, basePath: string): string {
130130
const normalized = normalizeControlUiBasePath(basePath);
131-
if (!normalized) {
132-
return html;
133-
}
134131
let next = html;
135132
for (const asset of CONTROL_UI_ROOT_PUBLIC_ASSETS) {
136-
const rootHref = `href="/${asset}"`;
137-
const baseHref = `href="${normalized}/${asset}"`;
138-
next = next.split(rootHref).join(baseHref);
133+
const assetHref = `href="${normalized}/${asset}"`;
134+
// Vite's portable ./ base emits relative hrefs, which the browser starts
135+
// resolving against a nested route before the UI can correct them.
136+
next = next.replaceAll(`href="./${asset}"`, assetHref);
137+
if (normalized) {
138+
next = next.replaceAll(`href="/${asset}"`, assetHref);
139+
}
139140
}
140141
return next;
141142
}

0 commit comments

Comments
 (0)