@devries-cameron20 / t3code Mirror · read-only / commits / 3f3c1b9bcf
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 15:45 UTC

refactor(web): HTML previews handle frame messages in one listener (#25456)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 26, 2026 at 05:31 UTC 3f3c1b9bcfd497eea2647e7a570a07e52b7c8872
2 files changed +74 -71
apps/web/src/components/files/BrowserDocumentFrame.browser.test.tsx new
+36
@@ -0,0 +1,36 @@
1 +import { describe, expect, it, vi } from "vite-plus/test";
2 +
3 +import "~/index.css";
4 +import { render } from "../../../test/render";
5 +
6 +import { HtmlRenderDocument } from "./BrowserDocumentFrame";
7 +
8 +const sizeChanged = (height: number) => ({
9 + jsonrpc: "2.0",
10 + method: "ui/notifications/size-changed",
11 + params: { height },
12 +});
13 +
14 +describe("HtmlRenderDocument", () => {
15 + it("reports the page's first height and ignores messages from other windows", async () => {
16 + using pageUrl = (() => {
17 + const page = `<script>parent.postMessage(${JSON.stringify(sizeChanged(321))}, "*");</script>`;
18 + const url = URL.createObjectURL(new Blob([page], { type: "text/html" }));
19 + return { url, [Symbol.dispose]: () => URL.revokeObjectURL(url) };
20 + })();
21 + const onContentHeight = vi.fn();
22 + await using _screen = await render(
23 + <HtmlRenderDocument src={pageUrl.url} title="Report" onContentHeight={onContentHeight} />,
24 + );
25 +
26 + await vi.waitFor(() => expect(onContentHeight).toHaveBeenCalledWith(321));
27 +
28 + // Listeners run in registration order, so once this one sees the message the frame's has too.
29 + const delivered = new Promise<void>((resolve) =>
30 + window.addEventListener("message", () => resolve(), { once: true }),
31 + );
32 + window.postMessage(sizeChanged(999), "*");
33 + await delivered;
34 + expect(onContentHeight).toHaveBeenCalledTimes(1);
35 + });
36 +});
apps/web/src/components/files/BrowserDocumentFrame.tsx
+38 -71
@@ -229,85 +229,52 @@ export function HtmlRenderDocument(props: {
229 }
230 void openLink(url, { event: modifiers }).catch((cause: unknown) => console.error(cause));
231 });
232 - // Require frame focus and recent app use to block opens on load; scripts can still wait for the reader's
233 - // next input.
234 - useEffect(() => {
235 - const openLink = (event: MessageEvent) => {
236 - const frame = frameRef.current;
237 - const request = readHtmlRenderLinkRequest(event.data);
238 - if (
239 - request === undefined ||
240 - frame === null ||
241 - event.source !== frame.contentWindow ||
242 - document.activeElement !== frame ||
243 - navigator.userActivation?.isActive === false
244 - ) {
245 - return;
246 - }
247 - open(request.url, request);
248 - frame.contentWindow?.postMessage(htmlRenderResult(request.id), "*");
249 - };
250 - window.addEventListener("message", openLink);
251 - return () => window.removeEventListener("message", openLink);
252 - }, []);
253 - useEffect(() => {
254 - const replayKey = (event: MessageEvent) => {
255 - const frame = frameRef.current;
256 - const key = readHtmlRenderKeyDown(event.data);
257 - if (
258 - key === undefined ||
259 - frame === null ||
260 - event.source !== frame.contentWindow ||
261 - document.activeElement !== frame ||
262 - navigator.userActivation?.isActive === false
263 - ) {
264 - return;
265 - }
266 - if (!isShortcut(key)) return;
267 - frame.dispatchEvent(
268 - new KeyboardEvent("keydown", { ...key, bubbles: true, cancelable: true }),
269 - );
270 - };
271 - window.addEventListener("message", replayKey);
272 - return () => window.removeEventListener("message", replayKey);
273 - }, []);
274 - useEffect(() => {
275 - const openImage = (event: MessageEvent) => {
276 - const frame = frameRef.current;
277 - const request = readHtmlRenderImageRequest(event.data);
278 - if (
279 - request === undefined ||
280 - frame === null ||
281 - event.source !== frame.contentWindow ||
282 - document.activeElement !== frame ||
283 - navigator.userActivation?.isActive === false
284 - ) {
285 - return;
286 - }
287 - setExpandedImage({ images: [{ src: request.src, name: request.name }], index: 0 });
288 - };
289 - window.addEventListener("message", openImage);
290 - return () => window.removeEventListener("message", openImage);
291 - }, []);
232 const { onFullscreenChange } = props;
233 useEffect(() => {
234 if (expandedImage === null) return;
235 onFullscreenChange?.(true);
236 return () => onFullscreenChange?.(false);
237 }, [expandedImage, onFullscreenChange]);
298 - const { onContentHeight } = props;
299 - // Listen at commit because a fast page may post its initial height before passive effects run.
300 - useLayoutEffect(() => {
301 - if (onContentHeight === undefined) return;
302 - const resize = (event: MessageEvent) => {
303 - const height = readHtmlRenderContentHeight(event.data);
304 - if (height !== undefined && event.source === frameRef.current?.contentWindow) {
305 - onContentHeight(height);
238 + // Links, keys and images require frame focus and recent reader activity: a page can take
239 + // focus by script, so this blocks opens on load, though not a page that waits for the
240 + // reader's next input.
241 + const onFrameMessage = useEffectEvent((frame: HTMLIFrameElement, event: MessageEvent) => {
242 + const height = readHtmlRenderContentHeight(event.data);
243 + if (height !== undefined) {
244 + props.onContentHeight?.(height);
245 + return;
246 + }
247 + if (document.activeElement !== frame || navigator.userActivation?.isActive === false) return;
248 + const linkRequest = readHtmlRenderLinkRequest(event.data);
249 + if (linkRequest !== undefined) {
250 + open(linkRequest.url, linkRequest);
251 + frame.contentWindow?.postMessage(htmlRenderResult(linkRequest.id), "*");
252 + return;
253 + }
254 + const key = readHtmlRenderKeyDown(event.data);
255 + if (key !== undefined) {
256 + if (isShortcut(key)) {
257 + frame.dispatchEvent(
258 + new KeyboardEvent("keydown", { ...key, bubbles: true, cancelable: true }),
259 + );
260 }
261 + return;
262 + }
263 + const imageRequest = readHtmlRenderImageRequest(event.data);
264 + if (imageRequest !== undefined) {
265 + setExpandedImage({ images: [{ src: imageRequest.src, name: imageRequest.name }], index: 0 });
266 + }
267 + });
268 + // Attach at commit because a fast page may post its initial height before passive effects run.
269 + useLayoutEffect(() => {
270 + const frame = frameRef.current;
271 + if (frame === null) return;
272 + const onMessage = (event: MessageEvent) => {
273 + if (event.source === frame.contentWindow) onFrameMessage(frame, event);
274 };
308 - window.addEventListener("message", resize);
309 - return () => window.removeEventListener("message", resize);
310 - }, [onContentHeight]);
275 + window.addEventListener("message", onMessage);
276 + return () => window.removeEventListener("message", onMessage);
277 + }, []);
278 return (
279 <>
280 <iframe